Skip to content

Commit 1a045c8

Browse files
committed
fix more library unit tests
1 parent 12b6c74 commit 1a045c8

File tree

1 file changed

+57
-65
lines changed

1 file changed

+57
-65
lines changed

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

Lines changed: 57 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ function address(port: number) {
9090

9191
describe("#createProxyServer.web() using own http server", () => {
9292
it("gets some ports", async () => {
93-
ports["8080"] = await getPort();
94-
ports["8081"] = await getPort();
95-
ports["8082"] = await getPort();
93+
for (let n = 8080; n < 8090; n++) {
94+
ports[`${n}`] = await getPort();
95+
}
9696
});
9797

9898
it("should proxy the request using the web proxy handler", (done) => {
@@ -227,19 +227,17 @@ describe("#createProxyServer.web() using own http server", () => {
227227
client.end();
228228
});
229229

230-
/*
231-
232230
it("should proxy the request and handle error via event listener", (done) => {
233231
const proxy = httpProxy.createProxyServer({
234232
target: address(8080),
233+
timeout: 100,
235234
});
236235

237236
const proxyServer = http.createServer(requestHandler);
238237

239238
function requestHandler(req, res) {
240239
proxy.once("error", (err, errReq, errRes) => {
241240
proxyServer.close();
242-
expect(err).toBeInstanceOf(Error);
243241
expect(errReq).toEqual(req);
244242
expect(errRes).toEqual(res);
245243
expect(err.code).toEqual("ECONNREFUSED");
@@ -249,31 +247,31 @@ describe("#createProxyServer.web() using own http server", () => {
249247
proxy.web(req, res);
250248
}
251249

252-
proxyServer.listen("8083");
250+
proxyServer.listen(ports["8083"]);
253251

254-
http
255-
.request(
256-
{
257-
hostname: "127.0.0.1",
258-
port: "8083",
259-
method: "GET",
260-
},
261-
() => {},
262-
)
263-
.end();
252+
const client = http.request(
253+
{
254+
hostname: "127.0.0.1",
255+
port: ports["8083"],
256+
method: "GET",
257+
},
258+
() => {},
259+
);
260+
client.on("error", () => {});
261+
client.end();
264262
});
265263

266264
it("should forward the request and handle error via event listener", (done) => {
267265
const proxy = httpProxy.createProxyServer({
268266
forward: "http://127.0.0.1:8080",
267+
timeout: 100,
269268
});
270269

271270
const proxyServer = http.createServer(requestHandler);
272271

273272
function requestHandler(req, res) {
274273
proxy.once("error", (err, errReq, errRes) => {
275274
proxyServer.close();
276-
expect(err).toBeInstanceOf(Error);
277275
expect(errReq).toEqual(req);
278276
expect(errRes).toEqual(res);
279277
expect(err.code).toEqual("ECONNREFUSED");
@@ -283,66 +281,66 @@ describe("#createProxyServer.web() using own http server", () => {
283281
proxy.web(req, res);
284282
}
285283

286-
proxyServer.listen("8083");
284+
proxyServer.listen(ports["8083"]);
287285

288-
http
289-
.request(
290-
{
291-
hostname: "127.0.0.1",
292-
port: "8083",
293-
method: "GET",
294-
},
295-
() => {},
296-
)
297-
.end();
286+
const client = http.request(
287+
{
288+
hostname: "127.0.0.1",
289+
port: ports["8083"],
290+
method: "GET",
291+
},
292+
() => {},
293+
);
294+
client.on("error", () => {});
295+
client.end();
298296
});
299297

300298
it("should proxy the request and handle timeout error (proxyTimeout)", (done) => {
301299
const proxy = httpProxy.createProxyServer({
302-
target: "http://127.0.0.1:45000",
300+
target: address(8083),
303301
proxyTimeout: 100,
302+
timeout: 150, // so client exits and isn't left handing the test.
304303
});
305304

306-
require("net").createServer().listen(45000);
305+
const server = require("net").createServer().listen(ports["8083"]);
307306

308-
const proxyServer = http.createServer(requestHandler);
309-
310-
const started = new Date().getTime();
307+
const started = Date.now();
311308
function requestHandler(req, res) {
312309
proxy.once("error", (err, errReq, errRes) => {
313310
proxyServer.close();
314-
expect(err).toBeInstanceOf(Error);
311+
server.close();
315312
expect(errReq).toEqual(req);
316313
expect(errRes).toEqual(res);
317-
expect(new Date().getTime() - started).toBeGreaterThan(99);
314+
expect(Date.now() - started).toBeGreaterThan(99);
318315
expect(err.code).toEqual("ECONNRESET");
319316
done();
320317
});
321318

322319
proxy.web(req, res);
323320
}
324321

325-
proxyServer.listen("8084");
322+
const proxyServer = http.createServer(requestHandler);
323+
proxyServer.listen(ports["8084"]);
326324

327-
http
328-
.request(
329-
{
330-
hostname: "127.0.0.1",
331-
port: "8084",
332-
method: "GET",
333-
},
334-
() => {},
335-
)
336-
.end();
325+
const client = http.request(
326+
{
327+
hostname: "127.0.0.1",
328+
port: ports["8084"],
329+
method: "GET",
330+
},
331+
() => {},
332+
);
333+
client.on("error", () => {});
334+
client.end();
337335
});
338336

339337
it("should proxy the request and handle timeout error", (done) => {
340338
const proxy = httpProxy.createProxyServer({
341-
target: "http://127.0.0.1:45001",
339+
target: address(8083),
342340
timeout: 100,
343341
});
344342

345-
require("net").createServer().listen(45001);
343+
const server = require("net").createServer().listen(ports["8083"]);
346344

347345
const proxyServer = http.createServer(requestHandler);
348346

@@ -352,11 +350,11 @@ describe("#createProxyServer.web() using own http server", () => {
352350
if (cnt === 2) done();
353351
};
354352

355-
const started = new Date().getTime();
353+
const started = Date.now();
356354
function requestHandler(req, res) {
357355
proxy.once("econnreset", (err, errReq, errRes) => {
358356
proxyServer.close();
359-
expect(err).toBeInstanceOf(Error);
357+
server.close();
360358
expect(errReq).toEqual(req);
361359
expect(errRes).toEqual(res);
362360
expect(err.code).toEqual("ECONNRESET");
@@ -366,26 +364,26 @@ describe("#createProxyServer.web() using own http server", () => {
366364
proxy.web(req, res);
367365
}
368366

369-
proxyServer.listen("8085");
367+
proxyServer.listen(ports["8085"]);
370368

371369
const req = http.request(
372370
{
373371
hostname: "127.0.0.1",
374-
port: "8085",
372+
port: ports["8085"],
375373
method: "GET",
376374
},
377375
() => {},
378376
);
379377

380378
req.on("error", (err) => {
381-
expect(err).toBeInstanceOf(Error);
382379
// @ts-ignore
383380
expect(err.code).toEqual("ECONNRESET");
384-
expect(new Date().getTime() - started).toBeGreaterThan(99);
381+
expect(Date.now() - started).toBeGreaterThan(99);
385382
doneOne();
386383
});
387384
req.end();
388385
});
386+
/*
389387
390388
it("should proxy the request and provide a proxyRes event with the request and response parameters", (done) => {
391389
const proxy = httpProxy.createProxyServer({
@@ -788,7 +786,6 @@ describe("#createProxyServer.web() using own http server", () => {
788786
function requestHandler(req, res) {
789787
proxy.web(req, res, function (err) {
790788
proxyServer.close();
791-
expect(err).toBeInstanceOf(Error);
792789
expect(err.code).toEqual("ECONNREFUSED");
793790
done();
794791
});
@@ -818,7 +815,6 @@ describe("#createProxyServer.web() using own http server", () => {
818815
function requestHandler(req, res) {
819816
proxy.once("error", function (err, errReq, errRes) {
820817
proxyServer.close();
821-
expect(err).toBeInstanceOf(Error);
822818
expect(errReq).toEqual(req);
823819
expect(errRes).toEqual(res);
824820
expect(err.code).toEqual("ECONNREFUSED");
@@ -852,7 +848,6 @@ describe("#createProxyServer.web() using own http server", () => {
852848
function requestHandler(req, res) {
853849
proxy.once("error", function (err, errReq, errRes) {
854850
proxyServer.close();
855-
expect(err).toBeInstanceOf(Error);
856851
expect(errReq).toEqual(req);
857852
expect(errRes).toEqual(res);
858853
expect(err.code).toEqual("ECONNREFUSED");
@@ -886,14 +881,13 @@ describe("#createProxyServer.web() using own http server", () => {
886881
887882
const proxyServer = http.createServer(requestHandler);
888883
889-
const started = new Date().getTime();
884+
const started = Date.now();
890885
function requestHandler(req, res) {
891886
proxy.once("error", function (err, errReq, errRes) {
892887
proxyServer.close();
893-
expect(err).toBeInstanceOf(Error);
894888
expect(errReq).toEqual(req);
895889
expect(errRes).toEqual(res);
896-
expect(new Date().getTime() - started).to.be.greaterThan(99);
890+
expect(Date.now() - started).to.be.greaterThan(99);
897891
expect(err.code).toEqual("ECONNRESET");
898892
done();
899893
});
@@ -931,11 +925,10 @@ describe("#createProxyServer.web() using own http server", () => {
931925
if (cnt === 2) done();
932926
};
933927
934-
const started = new Date().getTime();
928+
const started = Date.now();
935929
function requestHandler(req, res) {
936930
proxy.once("econnreset", function (err, errReq, errRes) {
937931
proxyServer.close();
938-
expect(err).toBeInstanceOf(Error);
939932
expect(errReq).toEqual(req);
940933
expect(errRes).toEqual(res);
941934
expect(err.code).toEqual("ECONNRESET");
@@ -957,9 +950,8 @@ describe("#createProxyServer.web() using own http server", () => {
957950
);
958951
959952
req.on("error", function (err) {
960-
expect(err).toBeInstanceOf(Error);
961953
expect(err.code).toEqual("ECONNRESET");
962-
expect(new Date().getTime() - started).to.be.greaterThan(99);
954+
expect(Date.now() - started).to.be.greaterThan(99);
963955
doneOne();
964956
});
965957
req.end();

0 commit comments

Comments
 (0)