Skip to content

Commit 29a9ea5

Browse files
committed
Add test to cover fixed WS crash bug
1 parent 2161a35 commit 29a9ea5

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

test/integration/websockets.spec.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ nodeOnly(() => {
6565
}
6666
});
6767

68-
let wsServer: WebSocket.Server;
68+
let wsServer: WebSocket.Server | http.Server;
6969
let wsErrors: Error[] = [];
7070

7171
let wsPort: number;
@@ -509,10 +509,42 @@ nodeOnly(() => {
509509

510510
beforeEach(async () => {
511511
if (wsServer) wsServer.close();
512-
wsServer = new WebSocket.Server({
513-
port: 9001,
514-
verifyClient: () => false // Reject all clients
512+
wsServer = new http.Server((req, res) => {
513+
res.writeHead(429, 'Not Found').end();
514+
}).listen(9001);
515+
});
516+
517+
it('should mirror the request rejection', async () => {
518+
mockServer.forAnyWebSocket().thenPassThrough();
519+
520+
const ws = new WebSocket(`ws://localhost:9001`, {
521+
agent: new HttpProxyAgent(`http://localhost:${mockServer.port}`)
522+
});
523+
524+
ws.on('open', () => ws.send('test echo'));
525+
526+
const error = await new Promise<Error>((resolve, reject) => {
527+
ws.on('message', reject);
528+
ws.on('error', resolve);
515529
});
530+
ws.close(1000);
531+
532+
expect(error.message).to.equal('Unexpected server response: 429');
533+
});
534+
535+
});
536+
537+
describe("when the websocket server kills the connection", () => {
538+
539+
beforeEach(async () => {
540+
if (wsServer) wsServer.close();
541+
wsServer = new http.Server((req, res) => {
542+
res.writeHead(429, 'Not Found');
543+
res.flushHeaders();
544+
545+
// If the server kills the connection while streaming the body:
546+
setImmediate(() => res.socket?.destroy());
547+
}).listen(9001);
516548
});
517549

518550
it('should mirror the request rejection', async () => {
@@ -530,7 +562,7 @@ nodeOnly(() => {
530562
});
531563
ws.close(1000);
532564

533-
expect(error.message).to.equal('Unexpected server response: 401');
565+
expect(error.message).to.equal('Unexpected server response: 429');
534566
});
535567

536568
});

0 commit comments

Comments
 (0)