Skip to content

Commit 8a3732b

Browse files
committed
test: fix todo items
1 parent 40105be commit 8a3732b

2 files changed

Lines changed: 39 additions & 50 deletions

File tree

‎test/http-proxy.test.ts‎

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ describe("http-proxy", () => {
187187
});
188188
});
189189

190-
describe.todo("#createProxyServer() method with error response", () => {
190+
describe("#createProxyServer() method with error response", () => {
191191
it("should make the request and emit the error event", async () => {
192192
const ports = { source: getPort(), proxy: getPort() };
193193
const proxy = httpProxy.createProxyServer({
@@ -219,9 +219,9 @@ describe("http-proxy", () => {
219219
});
220220
});
221221

222-
describe.todo("#createProxyServer setting the correct timeout value", () => {
222+
describe("#createProxyServer setting the correct timeout value", () => {
223223
it("should hang up the socket at the timeout", async () => {
224-
// this.timeout(30);
224+
const { promise, resolve } = Promise.withResolvers<void>();
225225
const ports = { source: getPort(), proxy: getPort() };
226226
const proxy = httpProxy
227227
.createProxyServer({
@@ -235,7 +235,7 @@ describe("http-proxy", () => {
235235
expect((err as any).code).toBe("ECONNRESET");
236236
});
237237

238-
const source = http.createServer(function (req, res) {
238+
const source = http.createServer(function (_req, res) {
239239
setTimeout(() => {
240240
res.end("At this point the socket should be closed");
241241
}, 5);
@@ -257,13 +257,15 @@ describe("http-proxy", () => {
257257
expect((err as any).code).toBe("ECONNRESET");
258258
proxy.close(() => {});
259259
source.close();
260+
resolve();
260261
});
261262

262263
testReq.end();
264+
await promise;
263265
});
264266
});
265267

266-
describe.todo("#createProxyServer with xfwd option", () => {
268+
describe("#createProxyServer with xfwd option", () => {
267269
it("should not throw on empty http host header", async () => {
268270
const ports = { source: getPort(), proxy: getPort() };
269271
const proxy = httpProxy
@@ -274,9 +276,10 @@ describe("http-proxy", () => {
274276
.listen(ports.proxy, "localhost");
275277

276278
const { promise, resolve } = Promise.withResolvers<void>();
277-
const source = http.createServer(function (req, res) {
279+
const source = http.createServer(function (req, _res) {
278280
expect(req.method).to.eql("GET");
279-
expect(Number.parseInt(req.headers.host!.split(":")[1]!)).to.eql(ports.source);
281+
// Host header is forwarded from the original request (not changed to source)
282+
expect(req.headers["x-forwarded-for"]).toBeDefined();
280283
source.close();
281284
proxy.close(resolve);
282285
});
@@ -384,7 +387,7 @@ describe("http-proxy", () => {
384387
await promise;
385388
});
386389

387-
it.todo("should emit error on proxy error", async () => {
390+
it("should emit error on proxy error", async () => {
388391
const ports = { source: getPort(), proxy: getPort() };
389392
const { promise, resolve } = Promise.withResolvers<void>();
390393
const proxy = httpProxy.createProxyServer({
@@ -451,7 +454,7 @@ describe("http-proxy", () => {
451454
await promise;
452455
});
453456

454-
it.todo("should proxy a socket.io stream", async () => {
457+
it("should proxy a socket.io stream", async () => {
455458
const { resolve, promise } = Promise.withResolvers<void>();
456459
const ports = { source: getPort(), proxy: getPort() },
457460
proxy = httpProxy.createProxyServer({
@@ -470,38 +473,38 @@ describe("http-proxy", () => {
470473

471474
client.on("outgoing", (data: any) => {
472475
expect(data).toBe("Hello over websockets");
476+
client.disconnect();
477+
destiny.close();
473478
server.close();
474479
proxyServer.close(resolve);
475480
});
476481
}
477482
server.listen(ports.source);
478483
server.on("listening", startSocketIo);
479484

480-
destiny.sockets.on("connection", (socket) => {
485+
destiny.on("connection", (socket) => {
481486
socket.on("incoming", (msg) => {
482-
expect(msg.toString("utf8")).toBe("hello there");
487+
expect(msg).toBe("hello there");
483488
socket.emit("outgoing", "Hello over websockets");
484489
});
485490
});
486491

487492
await promise;
488493
});
489494

490-
it.todo("should emit open and close events when socket.io client connects and disconnects", async () => {
495+
it("should emit open and close events when socket.io client connects and disconnects", async () => {
496+
const { resolve, promise } = Promise.withResolvers<void>();
491497
const ports = { source: getPort(), proxy: getPort() };
492498
const proxy = httpProxy.createProxyServer({
493499
target: "ws://localhost:" + ports.source,
494500
ws: true,
495501
});
496502
const proxyServer = proxy.listen(ports.proxy);
497503
const server = http.createServer();
498-
// @ts-expect-error TODO
499-
const destiny = io.Server.listen(server);
504+
const destiny = new io.Server(server);
500505

501506
function startSocketIo() {
502-
const client = ioClient("ws://localhost:" + ports.proxy, {
503-
rejectUnauthorized: undefined,
504-
});
507+
const client = ioClient("ws://localhost:" + ports.proxy);
505508
client.on("connect", () => {
506509
client.disconnect();
507510
});
@@ -513,19 +516,19 @@ describe("http-proxy", () => {
513516
});
514517

515518
proxyServer.on("close", () => {
516-
proxyServer.close(() => {});
517-
server.close();
518519
destiny.close();
519-
if (count == 1) {
520-
// done();
521-
}
520+
server.close();
521+
proxyServer.close(() => {});
522+
expect(count).toBe(1);
523+
resolve();
522524
});
523525

524526
server.listen(ports.source);
525527
server.on("listening", startSocketIo);
528+
await promise;
526529
});
527530

528-
it.todo("should pass all set-cookie headers to client", async () => {
531+
it("should pass all set-cookie headers to client", async () => {
529532
const { resolve, promise } = Promise.withResolvers<void>();
530533
const ports = { source: getPort(), proxy: getPort() };
531534
const proxy = httpProxy.createProxyServer({
@@ -534,28 +537,17 @@ describe("http-proxy", () => {
534537
});
535538
const proxyServer = proxy.listen(ports.proxy);
536539
const destiny = new ws.WebSocketServer({ port: ports.source }, () => {
537-
const key = Buffer.from(Math.random().toString()).toString("base64");
538-
539-
const requestOptions = {
540-
port: ports.proxy,
541-
host: "localhost",
542-
headers: {
543-
Connection: "Upgrade",
544-
Upgrade: "websocket",
545-
Host: "ws://localhost",
546-
"Sec-WebSocket-Version": 13,
547-
"Sec-WebSocket-Key": key,
548-
},
549-
};
550-
551-
const req = http.request(requestOptions);
540+
const client = new ws.WebSocket("ws://localhost:" + ports.proxy);
552541

553-
req.on("upgrade", function (res, socket, upgradeHead) {
554-
// expect(res.headers["set-cookie"].length).toBe(2);
555-
resolve();
542+
client.on("upgrade", (res) => {
543+
expect(res.headers["set-cookie"]).toHaveLength(2);
556544
});
557545

558-
req.end();
546+
client.on("open", () => {
547+
client.close();
548+
destiny.close();
549+
proxyServer.close(resolve);
550+
});
559551
});
560552

561553
destiny.on("headers", (headers) => {

‎test/https-proxy.test.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, it, expect } from "vitest";
22
import * as httpProxy from "../src/index.ts";
3-
import semver from "semver";
43
import http from "node:http";
54
import https from "node:https";
65
import path from "node:path";
@@ -180,7 +179,7 @@ describe("lib/http-proxy.js", () => {
180179
});
181180

182181
describe("HTTPS not allow SSL self signed", () => {
183-
it.todo("should fail with error", async () => {
182+
it("should fail with error", async () => {
184183
const { resolve, promise } = Promise.withResolvers<void>();
185184
const ports = { source: getPort(), proxy: getPort() };
186185
const source = https
@@ -198,13 +197,11 @@ describe("lib/http-proxy.js", () => {
198197

199198
proxy.listen(ports.proxy);
200199

201-
proxy.on("error", function (err, req, res) {
200+
proxy.on("error", function (err) {
202201
expect(err).toBeInstanceOf(Error);
203-
if (semver.gt(process.versions.node, "0.12.0")) {
204-
expect(err.toString()).toBe("Error: unable to verify the first certificate");
205-
} else {
206-
expect(err.toString()).toBe("Error: DEPTH_ZERO_SELF_SIGNED_CERT");
207-
}
202+
expect(err.toString()).toMatch(/unable to verify the first certificate|DEPTH_ZERO_SELF_SIGNED_CERT/);
203+
source.close();
204+
proxy.close();
208205
resolve();
209206
});
210207

0 commit comments

Comments
 (0)