Skip to content

Commit 0a3778d

Browse files
committed
got through all web proxy server tests
1 parent 1a045c8 commit 0a3778d

File tree

1 file changed

+59
-56
lines changed

1 file changed

+59
-56
lines changed

test/lib/http-proxy-passes-web-incoming.test.ts

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
} from "../../dist/lib/http-proxy/passes/web-incoming";
1010
import * as httpProxy from "../..";
1111
import * as http from "http";
12-
// import * as concat from "concat-stream";
13-
// import * as async from "async";
12+
import concat from "concat-stream";
13+
import * as async from "async";
1414
import getPort from "../get-port";
1515

1616
describe("#deleteLength", () => {
@@ -81,11 +81,15 @@ describe("#XHeaders", () => {
8181
});
8282

8383
const ports: { [port: string]: number } = {};
84-
function address(port: number) {
85-
if (ports[port] == null) {
86-
throw Error(`invalid port ${port}`);
84+
function address(p: number | string) {
85+
return `http://127.0.0.1:${port(p)}`;
86+
}
87+
function port(p: number | string) {
88+
const x = ports[p];
89+
if (x == null) {
90+
throw Error(`invalid port ${p}`);
8791
}
88-
return `http://127.0.0.1:${ports[port]}`;
92+
return x;
8993
}
9094

9195
describe("#createProxyServer.web() using own http server", () => {
@@ -383,17 +387,17 @@ describe("#createProxyServer.web() using own http server", () => {
383387
});
384388
req.end();
385389
});
386-
/*
387390

388391
it("should proxy the request and provide a proxyRes event with the request and response parameters", (done) => {
389392
const proxy = httpProxy.createProxyServer({
390393
target: address(8080),
391394
});
392395

393396
function requestHandler(req, res) {
394-
proxy.once("proxyRes", (_proxyRes, pReq, pRes) => {
397+
proxy.once("proxyRes", (proxyRes, pReq, pRes) => {
395398
source.close();
396399
proxyServer.close();
400+
expect(proxyRes != null).toBe(true);
397401
expect(pReq).toEqual(req);
398402
expect(pRes).toEqual(res);
399403
done();
@@ -408,9 +412,9 @@ describe("#createProxyServer.web() using own http server", () => {
408412
res.end("Response");
409413
});
410414

411-
proxyServer.listen("8086");
412-
source.listen(ports['8080']);
413-
http.request("http://127.0.0.1:8086", () => {}).end();
415+
proxyServer.listen(port(8086));
416+
source.listen(port(8080));
417+
http.request(address(8086), () => {}).end();
414418
});
415419

416420
it("should proxy the request and provide and respond to manual user response when using modifyResponse", (done) => {
@@ -440,12 +444,12 @@ describe("#createProxyServer.web() using own http server", () => {
440444

441445
async.parallel(
442446
[
443-
(next) => proxyServer.listen(8086, next),
444-
(next) => source.listen(8080, next),
447+
(next) => proxyServer.listen(port(8086), next),
448+
(next) => source.listen(port(8080), next),
445449
],
446450
(_err) => {
447451
http
448-
.get("http://127.0.0.1:8086", (res) => {
452+
.get(address(8086), (res) => {
449453
res.pipe(
450454
concat((body) => {
451455
expect(body.toString("utf8")).toEqual("my-custom-response");
@@ -455,50 +459,47 @@ describe("#createProxyServer.web() using own http server", () => {
455459
}),
456460
);
457461
})
458-
.once("error", done);
462+
.once("error", (err) => {
463+
source.close();
464+
proxyServer.close();
465+
done(err);
466+
});
459467
},
460468
);
461469
});
462470

463471
it("should proxy the request and handle changeOrigin option", (done) => {
464-
const proxy = httpProxy.createProxyServer({
465-
target: address(8080),
466-
changeOrigin: true,
467-
});
468-
469-
function requestHandler(req, res) {
470-
proxy.web(req, res);
471-
}
472-
473-
const proxyServer = http.createServer(requestHandler);
474-
475-
const source = http.createServer((req, _res) => {
476-
source.close();
477-
proxyServer.close();
478-
expect(req.method).toEqual("GET");
479-
expect(req.headers.host?.split(":")[1]).toEqual("8080");
480-
done();
481-
});
472+
const proxy = httpProxy
473+
.createProxyServer({
474+
target: address(8080),
475+
changeOrigin: true,
476+
})
477+
.listen(port(8081));
482478

483-
proxyServer.listen(ports['8081']);
484-
source.listen(ports['8080']);
479+
const source = http
480+
.createServer((req, res) => {
481+
source.close();
482+
proxy.close();
483+
expect(req.method).toEqual("GET");
484+
expect(req.headers.host?.split(":")[1]).toEqual(`${port(8080)}`);
485+
res.end();
486+
done();
487+
})
488+
.listen(port(8080));
485489

486-
http.request(address(8081), () => {}).end();
490+
const client = http.request(address(8081), () => {});
491+
client.on("error", () => {});
492+
client.end();
487493
});
488494

489495
it("should proxy the request with the Authorization header set", (done) => {
490496
const proxy = httpProxy.createProxyServer({
491497
target: address(8080),
492498
auth: "user:pass",
493499
});
500+
const proxyServer = http.createServer(proxy.web);
494501

495-
function requestHandler(req, res) {
496-
proxy.web(req, res);
497-
}
498-
499-
const proxyServer = http.createServer(requestHandler);
500-
501-
const source = http.createServer((req, _res) => {
502+
const source = http.createServer((req, res) => {
502503
source.close();
503504
proxyServer.close();
504505
const auth = new Buffer(
@@ -507,11 +508,12 @@ describe("#createProxyServer.web() using own http server", () => {
507508
);
508509
expect(req.method).toEqual("GET");
509510
expect(auth.toString()).toEqual("user:pass");
511+
res.end();
510512
done();
511513
});
512514

513-
proxyServer.listen(ports['8081']);
514-
source.listen(ports['8080']);
515+
proxyServer.listen(port(8081));
516+
source.listen(port(8080));
515517

516518
http.request(address(8081), () => {}).end();
517519
});
@@ -530,37 +532,38 @@ describe("#createProxyServer.web() using own http server", () => {
530532
});
531533
} else {
532534
proxy.web(req, res, {
533-
target: "http://127.0.0.1:8082",
535+
target: address(8082),
534536
});
535537
}
536538
}
537539

538540
const proxyServer = http.createServer(requestHandler);
539541

540-
const source1 = http.createServer((req, _res) => {
542+
const source1 = http.createServer((req, res) => {
541543
expect(req.method).toEqual("GET");
542-
expect(req.headers.host?.split(":")[1]).toEqual("8080");
544+
expect(req.headers.host?.split(":")[1]).toEqual(`${port(8080)}`);
543545
expect(req.url).toEqual("/test1");
546+
res.end();
544547
});
545548

546-
const source2 = http.createServer((req, _res) => {
549+
const source2 = http.createServer((req, res) => {
547550
source1.close();
548551
source2.close();
549552
proxyServer.close();
550553
expect(req.method).toEqual("GET");
551-
expect(req.headers.host?.split(":")[1]).toEqual("8080");
554+
expect(req.headers.host?.split(":")[1]).toEqual(`${port(8080)}`);
552555
expect(req.url).toEqual("/test2");
556+
res.end();
553557
done();
554558
});
555559

556-
proxyServer.listen(ports['8080']);
557-
source1.listen(ports['8081']);
558-
source2.listen("8082");
560+
proxyServer.listen(port(8080));
561+
source1.listen(port(8081));
562+
source2.listen(port(8082));
559563

560-
http.request("http://127.0.0.1:8080/s1/test1", () => {}).end();
561-
http.request("http://127.0.0.1:8080/test2", () => {}).end();
564+
http.request(`${address(8080)}/s1/test1`, () => {}).end();
565+
http.request(`${address(8080)}/test2`, () => {}).end();
562566
});
563-
*/
564567
});
565568

566569
/*

0 commit comments

Comments
 (0)