Skip to content

Commit 0cd81c0

Browse files
committed
Add tests for proxy with unix socket
1 parent 354f049 commit 0cd81c0

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

bin/configurable-http-proxy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ options.storageBackend = args.storageBackend;
325325

326326
var proxy = new ConfigurableProxy(options);
327327

328-
var listen = parseListenOptions(args);
328+
var listen = parseListenOptions(args, log);
329329

330330
proxy.proxyServer.listen(...listen.proxyTarget);
331331
proxy.apiServer.listen(...listen.apiTarget);

lib/configproxy.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ const require = createRequire(import.meta.url);
2424

2525
const __dirname = path.dirname(fileURLToPath(import.meta.url));
2626

27-
export function parseListenOptions(args) {
27+
export function parseListenOptions(args, logger) {
2828
var listen = {};
2929

3030
if (args.socket) {
3131
listen.proxyTarget = [args.socket];
32-
log.warn(
32+
logger.warn(
3333
"Proxy will listen on UNIX domain socket, --ip and --port options will be ignored."
3434
);
3535
} else {
3636
listen.port = parseInt(args.port) || 8000;
3737
if (args.ip === "*") {
3838
// handle ip=* alias for all interfaces
39-
log.warn(
39+
logger.warn(
4040
"Interpreting ip='*' as all-interfaces. Preferred usage is 0.0.0.0 for all IPv4 or '' for all-interfaces."
4141
);
4242
args.ip = "";
@@ -47,7 +47,7 @@ export function parseListenOptions(args) {
4747

4848
if (args.apiSocket) {
4949
listen.apiSocket = [args.apiSocket];
50-
log.warn(
50+
logger.warn(
5151
"API server will listen on UNIX domain socket, --api-ip and --api-port options will be ignored."
5252
);
5353
} else {
@@ -57,7 +57,7 @@ export function parseListenOptions(args) {
5757

5858
if (args.metricsSocket) {
5959
listen.metricsSocket = [args.metricsSocket];
60-
log.warn(
60+
logger.warn(
6161
"Metrics server will listen on UNIX domain socket, --metrics-ip and --metrics-port options will be ignored."
6262
);
6363
} else if (args.metricsPort) {

lib/testutil.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function setupProxy(listenOptions, options, paths) {
9191
options = options || {};
9292
options.authToken = "secret";
9393
options.log = defaultLogger({ level: "error" });
94-
var listen = parseListenOptions(listenOptions);
94+
var listen = parseListenOptions(listenOptions, options.log);
9595
var port = listen.port || 8000;
9696
var ip = listen.ip;
9797
var proxy = new ConfigurableProxy(options);

test/proxy_spec.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import WebSocket from "ws";
66

77
import { ConfigurableProxy } from "../lib/configproxy.js";
88
import * as util from "../lib/testutil.js";
9+
import http from "node:http";
910

1011
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
1112

@@ -525,3 +526,84 @@ describe("Proxy Tests", function () {
525526
.then(done);
526527
});
527528
});
529+
530+
531+
532+
describe("Proxy Tests with Unix socket", function () {
533+
var listenOptions = {
534+
socket: '/tmp/test.sock',
535+
};
536+
var requestOptions = new URL('http://localhost')
537+
requestOptions.socketPath = listenOptions.socket;
538+
var proxy;
539+
540+
beforeEach(function (callback) {
541+
util.setupProxy(listenOptions).then(function (newProxy) {
542+
proxy = newProxy;
543+
callback();
544+
});
545+
});
546+
547+
afterEach(function (callback) {
548+
util.teardownServers(callback);
549+
});
550+
551+
it("unix socket HTTP request", function (done) {
552+
http.request(requestOptions, res => {
553+
let recvData = [];
554+
res.on('data', chunk => {
555+
recvData.push(chunk);
556+
});
557+
res.on('end', () => {
558+
const body = JSON.parse(Buffer.concat(recvData).toString());
559+
expect(body).toEqual(
560+
jasmine.objectContaining({
561+
path: "/",
562+
})
563+
);
564+
});
565+
return proxy._routes.get("/").then((route) => {
566+
expect(route.last_activity).toBeGreaterThan(proxy._setup_timestamp);
567+
done();
568+
});
569+
}).on('error', err => {
570+
console.log('Error: ', err.message);
571+
expect("error").toEqual("ok");
572+
}).end();
573+
});
574+
575+
it("unix socket WebSocket request", function (done) {
576+
var ws = new WebSocket("ws+unix:/tmp/test.sock");
577+
ws.on("error", function () {
578+
// jasmine fail is only in master
579+
expect("error").toEqual("ok");
580+
done();
581+
});
582+
var nmsgs = 0;
583+
ws.on("message", function (msg) {
584+
msg = msg.toString();
585+
if (nmsgs === 0) {
586+
expect(msg).toEqual("connected");
587+
} else {
588+
msg = JSON.parse(msg);
589+
expect(msg).toEqual(
590+
jasmine.objectContaining({
591+
path: "/",
592+
message: "hi",
593+
})
594+
);
595+
// check last_activity was updated
596+
return proxy._routes.get("/").then((route) => {
597+
expect(route.last_activity).toBeGreaterThan(proxy._setup_timestamp);
598+
ws.close();
599+
done();
600+
});
601+
}
602+
nmsgs++;
603+
});
604+
ws.on("open", function () {
605+
ws.send("hi");
606+
});
607+
});
608+
609+
});

0 commit comments

Comments
 (0)