Skip to content

Commit 3550ee4

Browse files
author
vhess
committed
test fixes
primarily look for x-forwarded-host instead of host header
1 parent 9b395e6 commit 3550ee4

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

lib/test/lib/http-proxy.test.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Server } from "socket.io";
1313
import { io as socketioClient } from "socket.io-client";
1414
import wait from "../wait";
1515
import { once } from "node:events";
16-
import { describe, it, expect } from "vitest";
16+
import { describe, it, expect, beforeAll } from "vitest";
1717

1818
const ports: { [port: string]: number } = {};
1919
let portIndex = -1;
@@ -25,13 +25,13 @@ Object.defineProperty(gen, "port", {
2525
},
2626
});
2727

28-
describe("setup ports", () => {
29-
it("creates some ports", async () => {
28+
beforeAll(async () => {
29+
//creates some ports
3030
for (let n = 0; n < 50; n++) {
3131
ports[n] = await getPort();
3232
}
33+
3334
});
34-
});
3535

3636
describe("#createProxyServer", () => {
3737
it("should NOT throw without options -- options are only required later when actually using the proxy", () => {
@@ -46,7 +46,7 @@ describe("#createProxyServer", () => {
4646
expect(typeof httpProxy.ProxyServer).toBe("function");
4747
expect(typeof obj).toBe("object");
4848
});
49-
});
49+
5050

5151
describe("#createProxyServer with forward options and using web-incoming passes", () => {
5252
it("should pipe the request using web-incoming#stream method", () =>
@@ -55,14 +55,15 @@ describe("#createProxyServer with forward options and using web-incoming passes"
5555
const proxy = httpProxy
5656
.createProxyServer({
5757
forward: "http://127.0.0.1:" + ports.source,
58+
xfwd: true
5859
})
5960
.listen(ports.proxy);
6061

6162
const source = http
6263
.createServer((req, res) => {
6364
res.end();
6465
expect(req.method).toEqual("GET");
65-
expect(req.headers.host?.split(":")[1]).toEqual(`${ports.proxy}`);
66+
expect((req.headers["x-forwarded-host"] as string).split(":")[1]).toEqual(`${ports.proxy}`);
6667
source.close();
6768
proxy.close();
6869
done();
@@ -82,14 +83,15 @@ describe("#createProxyServer using the web-incoming passes", () => {
8283
const proxy = httpProxy
8384
.createProxyServer({
8485
target: "http://127.0.0.1:" + ports.source,
86+
xfwd: true,
8587
})
8688
.listen(ports.proxy);
8789

8890
const source = http
8991
.createServer((req, res) => {
9092
expect(req.method).toEqual("POST");
91-
expect(req.headers["x-forwarded-for"]).toEqual("127.0.0.1");
92-
expect(req.headers.host?.split(":")[1]).toEqual(`${ports.proxy}`);
93+
expect(req.headers["x-forwarded-for"]).toContain("127.0.0.1");
94+
expect((req.headers["x-forwarded-host"] as string).split(":")[1]).toEqual(`${ports.proxy}`);
9395
res.end();
9496
source.close();
9597
proxy.close();
@@ -644,3 +646,4 @@ describe("#createProxyServer using the ws-incoming passes", () => {
644646
});
645647
}));
646648
});
649+
})

lib/test/lib/https-proxy.test.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as https from "node:https";
44
import getPort from "../get-port";
55
import { join } from "node:path";
66
import { readFileSync } from "node:fs";
7-
import { describe, it, expect } from 'vitest';
7+
import { describe, it, expect, beforeAll } from 'vitest';
88

99
const ports: { [port: string]: number } = {};
1010
let portIndex = -1;
@@ -16,13 +16,15 @@ Object.defineProperty(gen, "port", {
1616
},
1717
});
1818

19-
describe("HTTPS to HTTP", () => {
20-
it("creates some ports", async () => {
19+
beforeAll(async () => {
2120
for (let n = 0; n < 50; n++) {
2221
ports[n] = await getPort();
2322
}
2423
});
2524

25+
describe("HTTPS to HTTP", () => {
26+
27+
2628
it("should proxy the request, then send back the response", () => new Promise<void>(done => {
2729
const ports = { source: gen.port, proxy: gen.port };
2830
const source = http
@@ -200,6 +202,7 @@ describe("HTTPS to HTTPS", () => {
200202

201203
describe("HTTPS not allow SSL self signed", () => {
202204
it("should fail with error", () => new Promise<void>(done => {
205+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "1";
203206
const ports = { source: gen.port, proxy: gen.port };
204207
const source = https
205208
.createServer({
@@ -221,12 +224,13 @@ describe("HTTPS not allow SSL self signed", () => {
221224

222225
proxy.on("error", (err, _req, res) => {
223226
res.end();
224-
expect(err.toString()).toContain(
225-
"Error: unable to verify the first certificate",
227+
expect(err.toString()).toMatch(
228+
/Error: unable to verify the first certificate|TypeError: fetch failed/,
226229
);
227230
source.close();
228231
proxy.close();
229232
done();
233+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
230234
});
231235

232236
const client = http.request({
@@ -244,7 +248,7 @@ describe("HTTPS to HTTP using own server", () => {
244248
const source = http
245249
.createServer((req, res) => {
246250
expect(req.method).toEqual("GET");
247-
expect(req.headers.host?.split(":")[1]).toEqual(`${ports.proxy}`);
251+
expect((req.headers["x-forwarded-host"] as string).split(":")[1]).toEqual(`${ports.proxy}`);
248252
res.writeHead(200, { "Content-Type": "text/plain" });
249253
res.end("Hello from " + ports.source);
250254
})
@@ -268,6 +272,7 @@ describe("HTTPS to HTTP using own server", () => {
268272
(req, res) => {
269273
proxy.web(req, res, {
270274
target: "http://127.0.0.1:" + ports.source,
275+
xfwd: true,
271276
});
272277
},
273278
)

lib/test/middleware/modify-response-middleware.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ describe("Using the connect-gzip middleware from connect with http-proxy-3", ()
3434
const rewrite = (_req: http.IncomingMessage, res: http.ServerResponse, next: NextFunction) => {
3535
const _write = res.write;
3636
res.write = (data) => {
37+
const str = typeof data === "string" ? data : Buffer.from(data).toString();
3738
// @ts-expect-error write allows 2 args
38-
return _write.call(res, data.toString().replace("http-party", "cocalc"))
39+
return _write.call(res, str.replace("http-party", "cocalc"))
3940
};
4041
next();
4142
};

0 commit comments

Comments
 (0)