Skip to content

Commit 354f049

Browse files
committed
Refactor tests: let setupProxy take unix socket as well as TCP socket options.
1 parent de6bd23 commit 354f049

File tree

3 files changed

+44
-26
lines changed

3 files changed

+44
-26
lines changed

lib/testutil.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import http from "node:http";
44
import https from "node:https";
55
import { WebSocketServer } from "ws";
6-
import { ConfigurableProxy } from "./configproxy.js";
6+
import { ConfigurableProxy, parseListenOptions } from "./configproxy.js";
77
import { defaultLogger } from "./log.js";
88

99
var servers = [];
@@ -87,14 +87,16 @@ export function addTargets(proxy, paths, port) {
8787
});
8888
}
8989

90-
export function setupProxy(port, options, paths) {
90+
export function setupProxy(listenOptions, options, paths) {
9191
options = options || {};
9292
options.authToken = "secret";
9393
options.log = defaultLogger({ level: "error" });
94-
94+
var listen = parseListenOptions(listenOptions);
95+
var port = listen.port || 8000;
96+
var ip = listen.ip;
9597
var proxy = new ConfigurableProxy(options);
9698
proxy._setup_timestamp = new Date(new Date().getTime() - 60000);
97-
var ip = "127.0.0.1";
99+
98100
var countdown = 2;
99101
var resolvePromise;
100102

@@ -134,10 +136,10 @@ export function setupProxy(port, options, paths) {
134136
proxy.proxyServer.on("listening", onlisten);
135137

136138
addTargets(proxy, paths || ["/"], port + 2).then(function () {
137-
proxy.proxyServer.listen(port, ip);
138-
proxy.apiServer.listen(port + 1, ip);
139+
proxy.proxyServer.listen(...listen.proxyTarget);
140+
proxy.apiServer.listen(...listen.apiTarget);
139141
if (options.enableMetrics) {
140-
proxy.metricsServer.listen(port + 3, ip);
142+
proxy.metricsServer.listen(...listen.metricsTarget);
141143
}
142144
});
143145
return p;

test/api_spec.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ log.remove(log.transports.Console);
88

99
describe("API Tests", function () {
1010
var port = 8902;
11-
var apiPort = port + 1;
11+
var listenOptions = {
12+
port: port,
13+
apiPort: 8903,
14+
ip: '127.0.0.1'
15+
};
1216
var proxy;
13-
var apiUrl = "http://127.0.0.1:" + apiPort + "/api/routes";
17+
var apiUrl = "http://" + listenOptions.ip + ":" + listenOptions.apiPort + "/api/routes";
1418

1519
var r;
1620

1721
beforeEach(function (callback) {
1822
util
19-
.setupProxy(port)
23+
.setupProxy(listenOptions)
2024
.then(function (newProxy) {
2125
proxy = newProxy;
2226
})

test/proxy_spec.js

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
1313

1414
describe("Proxy Tests", function () {
1515
var port = 8902;
16+
var listenOptions = {
17+
port: port,
18+
ip: '127.0.0.1'
19+
};
1620
var testPort = port + 10;
1721
var proxy;
18-
var proxyUrl = "http://127.0.0.1:" + port;
22+
var proxyUrl = "http://" + listenOptions.ip + ":" + port;
1923
var hostTest = "test.localhost.jovyan.org";
2024
var hostUrl = "http://" + hostTest + ":" + port;
2125

2226
beforeEach(function (callback) {
23-
util.setupProxy(port).then(function (newProxy) {
27+
util.setupProxy(listenOptions).then(function (newProxy) {
2428
proxy = newProxy;
2529
callback();
2630
});
@@ -338,10 +342,12 @@ describe("Proxy Tests", function () {
338342
});
339343

340344
it("custom error target", function (done) {
341-
var proxyPort = 55550;
345+
var listenOptions = {
346+
port: 55550
347+
};
342348
util
343-
.setupProxy(proxyPort, { errorTarget: "http://127.0.0.1:55565" }, [])
344-
.then(() => fetch("http://127.0.0.1:" + proxyPort + "/foo/bar"))
349+
.setupProxy(listenOptions, { errorTarget: "http://127.0.0.1:55565" }, [])
350+
.then(() => fetch("http://127.0.0.1:" + listenOptions.port + "/foo/bar"))
345351
.then((res) => {
346352
expect(res.status).toEqual(404);
347353
expect(res.headers.get("content-type")).toEqual("text/plain");
@@ -405,10 +411,12 @@ describe("Proxy Tests", function () {
405411
});
406412

407413
it("backend error", function (done) {
408-
var proxyPort = 55550;
414+
var listenOptions = {
415+
port: 55550
416+
};
409417
util
410-
.setupProxy(proxyPort, { errorTarget: "http://127.0.0.1:55565" }, [])
411-
.then(() => fetch("http://127.0.0.1:" + proxyPort + "/%"))
418+
.setupProxy(listenOptions, { errorTarget: "http://127.0.0.1:55565" }, [])
419+
.then(() => fetch("http://127.0.0.1:" + listenOptions.port + "/%"))
412420
.then((res) => {
413421
expect(res.status).toEqual(500);
414422
expect(res.headers.get("content-type")).toEqual("text/plain");
@@ -433,7 +441,9 @@ describe("Proxy Tests", function () {
433441
});
434442

435443
it("Redirect location with rewriting", function (done) {
436-
var proxyPort = 55556;
444+
var listenOptions = {
445+
port: 55556
446+
};
437447
var options = {
438448
protocolRewrite: "https",
439449
autoRewrite: true,
@@ -442,10 +452,10 @@ describe("Proxy Tests", function () {
442452
// where the backend server redirects us.
443453
// Note that http-proxy requires (logically) the redirection to be to the same (internal) host.
444454
var redirectTo = "https://127.0.0.1:" + testPort + "/whatever";
445-
var expectedRedirect = "https://127.0.0.1:" + proxyPort + "/whatever";
455+
var expectedRedirect = "https://127.0.0.1:" + listenOptions.port + "/whatever";
446456

447457
util
448-
.setupProxy(proxyPort, options, [])
458+
.setupProxy(listenOptions, options, [])
449459
.then((proxy) =>
450460
util.addTargetRedirecting(
451461
proxy,
@@ -456,7 +466,7 @@ describe("Proxy Tests", function () {
456466
)
457467
)
458468
.then(() =>
459-
fetch("http://127.0.0.1:" + proxyPort + "/external/urlpath/", { redirect: "manual" })
469+
fetch("http://127.0.0.1:" + listenOptions.port + "/external/urlpath/", { redirect: "manual" })
460470
)
461471
.then((res) => {
462472
expect(res.status).toEqual(301);
@@ -483,8 +493,10 @@ describe("Proxy Tests", function () {
483493
done();
484494
return;
485495
}
486-
var proxyPort = 55556;
487-
var testPort = proxyPort + 20;
496+
var listenOptions = {
497+
port: 55556
498+
};
499+
var testPort = listenOptions.port + 20;
488500
var options = {
489501
clientSsl: {
490502
key: fs.readFileSync(path.resolve(__dirname, "ssl/proxy-client/proxy-client.key")),
@@ -494,7 +506,7 @@ describe("Proxy Tests", function () {
494506
};
495507

496508
util
497-
.setupProxy(proxyPort, options, [])
509+
.setupProxy(listenOptions, options, [])
498510
.then((proxy) =>
499511
util.addTarget(proxy, "/backend/", testPort, false, null, {
500512
key: fs.readFileSync(path.resolve(__dirname, "ssl/backend/backend.key")),
@@ -503,7 +515,7 @@ describe("Proxy Tests", function () {
503515
requestCert: true,
504516
})
505517
)
506-
.then(() => fetch("http://127.0.0.1:" + proxyPort + "/backend/urlpath/"))
518+
.then(() => fetch("http://127.0.0.1:" + listenOptions.port + "/backend/urlpath/"))
507519
.then((res) => {
508520
expect(res.status).toEqual(200);
509521
})

0 commit comments

Comments
 (0)