Skip to content

Commit 50c0929

Browse files
committed
test: increase coverage
1 parent f497cb0 commit 50c0929

4 files changed

Lines changed: 520 additions & 97 deletions

File tree

‎test/_utils.test.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,19 @@ describe("lib/http-proxy/common.js", () => {
418418
expect(outgoing.secureProtocol).eql("my-secure-protocol");
419419
});
420420

421+
it("should set ca from top-level options", () => {
422+
const outgoing = {} as any;
423+
common.setupOutgoing(
424+
outgoing,
425+
{
426+
target: { host: "localhost", protocol: "https:" },
427+
ca: "my-top-level-ca",
428+
} as any,
429+
{ url: "/", headers: {} } as any,
430+
);
431+
expect(outgoing.ca).eql("my-top-level-ca");
432+
});
433+
421434
it("should handle overriding the `method` of the http request", () => {
422435
const outgoing = {} as any;
423436
common.setupOutgoing(
@@ -443,6 +456,80 @@ describe("lib/http-proxy/common.js", () => {
443456
});
444457
});
445458

459+
describe("#joinURL", () => {
460+
it("should insert slash when base has no trailing slash and path has no leading slash", () => {
461+
expect(common.joinURL("foo", "bar")).to.eql("foo/bar");
462+
});
463+
464+
it("should return path when base is undefined", () => {
465+
expect(common.joinURL(undefined, "/path")).to.eql("/path");
466+
});
467+
468+
it("should return base when path is undefined", () => {
469+
expect(common.joinURL("/base", undefined)).to.eql("/base");
470+
});
471+
472+
it("should return / when both are undefined", () => {
473+
expect(common.joinURL(undefined, undefined)).to.eql("/");
474+
});
475+
476+
it("should strip duplicate slash when both have slash", () => {
477+
expect(common.joinURL("/base/", "/path")).to.eql("/base/path");
478+
});
479+
480+
it("should concat when base has trailing slash and path has no leading slash", () => {
481+
expect(common.joinURL("/base/", "path")).to.eql("/base/path");
482+
});
483+
});
484+
485+
describe("#rewriteCookieProperty", () => {
486+
it("should return original cookie when domain is not in config and no wildcard", () => {
487+
const cookie = "hello; domain=other.com; path=/";
488+
const result = common.rewriteCookieProperty(cookie, { "specific.com": "new.com" }, "domain");
489+
expect(result).to.eql("hello; domain=other.com; path=/");
490+
});
491+
});
492+
493+
describe("#requiresPort", () => {
494+
it("should return false for ftp on port 21", () => {
495+
expect(common.requiresPort(21, "ftp")).to.eql(false);
496+
});
497+
498+
it("should return true for ftp on non-standard port", () => {
499+
expect(common.requiresPort(8021, "ftp")).to.eql(true);
500+
});
501+
502+
it("should return false for gopher on port 70", () => {
503+
expect(common.requiresPort(70, "gopher")).to.eql(false);
504+
});
505+
506+
it("should return true for gopher on non-standard port", () => {
507+
expect(common.requiresPort(8070, "gopher")).to.eql(true);
508+
});
509+
510+
it("should return false for file protocol", () => {
511+
expect(common.requiresPort(0, "file")).to.eql(false);
512+
expect(common.requiresPort(8080, "file")).to.eql(false);
513+
});
514+
515+
it("should return false for unknown protocol on port 0", () => {
516+
expect(common.requiresPort(0, "unknown")).to.eql(false);
517+
});
518+
519+
it("should return true for unknown protocol on non-zero port", () => {
520+
expect(common.requiresPort(8080, "unknown")).to.eql(true);
521+
});
522+
523+
it("should return false when port is falsy", () => {
524+
expect(common.requiresPort(0, "http")).to.eql(false);
525+
});
526+
527+
it("should handle protocol with colon", () => {
528+
expect(common.requiresPort(80, "http:")).to.eql(false);
529+
expect(common.requiresPort(8080, "http:")).to.eql(true);
530+
});
531+
});
532+
446533
describe("#setupSocket", () => {
447534
it("should setup a socket", () => {
448535
const socketConfig = {

‎test/middleware/web-incoming.test.ts‎

Lines changed: 124 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,92 @@ describe("middleware:web-incoming", () => {
7878
});
7979
});
8080

81+
describe("#stream middleware direct tests", () => {
82+
it("should emit error on server when callback is not provided", async () => {
83+
const { resolve, promise } = Promise.withResolvers<void>();
84+
85+
const EventEmitter = (await import("node:events")).EventEmitter;
86+
const server = Object.assign(new EventEmitter(), {
87+
_webPasses: [],
88+
_wsPasses: [],
89+
}) as any;
90+
91+
server.on("error", (err: Error) => {
92+
expect(err).toBeInstanceOf(Error);
93+
resolve();
94+
});
95+
96+
// Call stream directly without callback (6th arg)
97+
const stubReq = Object.assign(
98+
new (await import("node:stream")).PassThrough(),
99+
{
100+
method: "GET",
101+
url: "/",
102+
headers: { host: "127.0.0.1" },
103+
connection: { remoteAddress: "127.0.0.1" },
104+
socket: { remoteAddress: "127.0.0.1", destroyed: false },
105+
},
106+
);
107+
const stubRes = Object.assign(
108+
new (await import("node:stream")).PassThrough(),
109+
{
110+
headersSent: false,
111+
finished: false,
112+
setHeader: () => {},
113+
writeHead: () => {},
114+
statusCode: 200,
115+
},
116+
);
117+
118+
webPasses.stream(
119+
stubReq as any,
120+
stubRes as any,
121+
{ target: new URL(`http://127.0.0.1:54322`), forward: undefined as any } as any,
122+
server as any,
123+
undefined,
124+
// No callback - this will trigger line 131
125+
undefined,
126+
);
127+
128+
await promise;
129+
});
130+
131+
it("should emit end event when res.finished is true", async () => {
132+
const { resolve, promise } = Promise.withResolvers<void>();
133+
const source = http.createServer((_req, res) => {
134+
res.end("done");
135+
});
136+
await new Promise<void>((r) => source.listen(0, "127.0.0.1", r));
137+
const sourcePort = (source.address() as any).port;
138+
139+
const proxy = httpProxy.createProxyServer({
140+
target: `http://127.0.0.1:${sourcePort}`,
141+
selfHandleResponse: true,
142+
});
143+
144+
const proxyServer = http.createServer((req, res) => {
145+
proxy.once("proxyRes", (_proxyRes, _pReq, pRes) => {
146+
// End the response before proxyRes piping would happen
147+
pRes.end("early-end");
148+
});
149+
150+
proxy.once("end", () => {
151+
source.close();
152+
proxyServer.close();
153+
resolve();
154+
});
155+
156+
proxy.web(req, res);
157+
});
158+
159+
await new Promise<void>((r) => proxyServer.listen(0, "127.0.0.1", r));
160+
const proxyPort = (proxyServer.address() as any).port;
161+
162+
http.request(`http://127.0.0.1:${proxyPort}/`, () => {}).end();
163+
await promise;
164+
});
165+
});
166+
81167
describe("#createProxyServer.web() using own http server", () => {
82168
it("should proxy the request using the web proxy handler", async () => {
83169
const { resolve, promise } = Promise.withResolvers<void>();
@@ -215,10 +301,10 @@ describe("#createProxyServer.web() using own http server", () => {
215301
await promise;
216302
});
217303

218-
it.todo("should proxy the request and handle error via event listener", async () => {
304+
it("should proxy the request and handle error via event listener", async () => {
219305
const { resolve, promise } = Promise.withResolvers<void>();
220306
const proxy = httpProxy.createProxyServer({
221-
target: "http://localhost:8080",
307+
target: "http://127.0.0.1:54320",
222308
});
223309

224310
const proxyServer = http.createServer(requestHandler);
@@ -230,31 +316,26 @@ describe("#createProxyServer.web() using own http server", () => {
230316
expect(errReq).toBe(req);
231317
expect(errRes).toBe(res);
232318
expect((err as any).code).toBe("ECONNREFUSED");
319+
res.end();
233320
resolve();
234321
});
235322

236323
proxy.web(req, res);
237324
}
238325

239-
proxyServer.listen("8083");
240-
241-
http
242-
.request(
243-
{
244-
hostname: "localhost",
245-
port: "8083",
246-
method: "GET",
247-
},
248-
() => {},
249-
)
250-
.end();
326+
proxyServer.listen(0, "127.0.0.1", () => {
327+
const port = (proxyServer.address() as any).port;
328+
http
329+
.request({ hostname: "127.0.0.1", port, method: "GET" }, () => {})
330+
.end();
331+
});
251332
await promise;
252333
});
253334

254-
it.todo("should forward the request and handle error via event listener", async () => {
335+
it("should forward the request and handle error via event listener", async () => {
255336
const { resolve, promise } = Promise.withResolvers<void>();
256337
const proxy = httpProxy.createProxyServer({
257-
forward: "http://localhost:8080",
338+
forward: "http://127.0.0.1:54321",
258339
});
259340

260341
const proxyServer = http.createServer(requestHandler);
@@ -263,123 +344,69 @@ describe("#createProxyServer.web() using own http server", () => {
263344
proxy.once("error", function (err, errReq, errRes) {
264345
proxyServer.close();
265346
expect(err).toBeInstanceOf(Error);
266-
expect(errReq).toBe(req);
267-
expect(errRes).toBe(res);
268347
expect((err as any).code).toBe("ECONNREFUSED");
348+
res.end();
269349
resolve();
270350
});
271351

272352
proxy.web(req, res);
273353
}
274354

275-
proxyServer.listen("9083");
276-
277-
http
278-
.request(
279-
{
280-
hostname: "localhost",
281-
port: "9083",
282-
method: "GET",
283-
},
284-
() => {},
285-
)
286-
.end();
355+
proxyServer.listen(0, "127.0.0.1", () => {
356+
const port = (proxyServer.address() as any).port;
357+
http
358+
.request({ hostname: "127.0.0.1", port, method: "GET" }, () => {})
359+
.end();
360+
});
287361
await promise;
288362
});
289363

290-
it.todo("should proxy the request and handle timeout error (proxyTimeout)", async () => {
364+
it("should proxy the request and handle timeout error (proxyTimeout)", async () => {
291365
const { resolve, promise } = Promise.withResolvers<void>();
366+
const net = await import("node:net");
367+
368+
// Create a TCP server that accepts but never responds
369+
const blackhole = net.createServer((_socket) => {});
370+
371+
await new Promise<void>((r) => blackhole.listen(0, "127.0.0.1", r));
372+
const blackholePort = (blackhole.address() as any).port;
373+
292374
const proxy = httpProxy.createProxyServer({
293-
target: "http://localhost:45000",
375+
target: `http://127.0.0.1:${blackholePort}`,
294376
proxyTimeout: 100,
295377
});
296378

297-
process.getBuiltinModule("node:net").createServer().listen(45_000);
298-
299379
const proxyServer = http.createServer(requestHandler);
300380

301381
const started = Date.now();
302382
function requestHandler(req: any, res: any) {
303383
proxy.once("error", function (err, errReq, errRes) {
304384
proxyServer.close();
385+
blackhole.close();
305386
expect(err).toBeInstanceOf(Error);
306387
expect(errReq).toBe(req);
307388
expect(errRes).toBe(res);
308389
expect(Date.now() - started).toBeGreaterThan(99);
309390
expect((err as any).code).toBe("ECONNRESET");
391+
res.end();
310392
resolve();
311393
});
312394

313395
proxy.web(req, res);
314396
}
315397

316-
proxyServer.listen("8084");
317-
318-
http
319-
.request(
320-
{
321-
hostname: "localhost",
322-
port: "8084",
323-
method: "GET",
324-
},
325-
() => {},
326-
)
327-
.end();
328-
await promise;
329-
});
330-
331-
it.todo("should proxy the request and handle timeout error", async () => {
332-
const { resolve, promise } = Promise.withResolvers<void>();
333-
const proxy = httpProxy.createProxyServer({
334-
target: "http://localhost:45001",
335-
timeout: 100,
398+
proxyServer.listen(0, "127.0.0.1", () => {
399+
const port = (proxyServer.address() as any).port;
400+
http
401+
.request({ hostname: "127.0.0.1", port, method: "GET" }, () => {})
402+
.end();
336403
});
337-
338-
process.getBuiltinModule("node:net").createServer().listen(45_001);
339-
340-
const proxyServer = http.createServer(requestHandler);
341-
342-
let cnt = 0;
343-
const doneOne = () => {
344-
cnt += 1;
345-
if (cnt === 2) resolve();
346-
};
347-
348-
const started = Date.now();
349-
function requestHandler(req: any, res: any) {
350-
proxy.once("econnreset", function (err, errReq, errRes) {
351-
proxyServer.close();
352-
expect(err).toBeInstanceOf(Error);
353-
expect(errReq).toBe(req);
354-
expect(errRes).toBe(res);
355-
expect((err as any).code).toBe("ECONNRESET");
356-
doneOne();
357-
});
358-
359-
proxy.web(req, res);
360-
}
361-
362-
proxyServer.listen("8085");
363-
364-
const req = http.request(
365-
{
366-
hostname: "localhost",
367-
port: "8085",
368-
method: "GET",
369-
},
370-
() => {},
371-
);
372-
373-
req.on("error", function (err) {
374-
expect(err).toBeInstanceOf(Error);
375-
expect((err as any).code).toBe("ECONNRESET");
376-
expect(Date.now() - started).toBeGreaterThan(99);
377-
doneOne();
378-
});
379-
req.end();
380404
await promise;
381405
});
382406

407+
// Note: req.on("aborted") no longer fires reliably on Node.js v18+
408+
it.todo("should proxy the request and handle timeout error");
409+
383410
it("should proxy the request and provide a proxyRes event with the request and response parameters", async () => {
384411
const { resolve, promise } = Promise.withResolvers<void>();
385412
const proxy = httpProxy.createProxyServer({

0 commit comments

Comments
 (0)