|
| 1 | +import * as net from 'net'; |
| 2 | +import * as http from 'http'; |
| 3 | +import * as crypto from 'crypto'; |
| 4 | +import { expect } from 'chai'; |
| 5 | +import { WebSocket } from 'ws'; |
| 6 | +import { DestroyableServer, makeDestroyable } from 'destroyable-server'; |
| 7 | + |
| 8 | +import { createServer } from '../src/server.js'; |
| 9 | + |
| 10 | +// Raw WebSocket upgrade that skips the ws client's strict subprotocol validation |
| 11 | +// (which rejects mismatched or missing server-selected protocols with an error) |
| 12 | +function rawUpgrade(port: number, path: string, protocols?: string[]): Promise<{ |
| 13 | + statusCode: number; |
| 14 | + headers: http.IncomingHttpHeaders; |
| 15 | +}> { |
| 16 | + return new Promise((resolve, reject) => { |
| 17 | + const key = crypto.randomBytes(16).toString('base64'); |
| 18 | + const headers: Record<string, string> = { |
| 19 | + 'Connection': 'Upgrade', |
| 20 | + 'Upgrade': 'websocket', |
| 21 | + 'Sec-WebSocket-Version': '13', |
| 22 | + 'Sec-WebSocket-Key': key, |
| 23 | + }; |
| 24 | + if (protocols?.length) { |
| 25 | + headers['Sec-WebSocket-Protocol'] = protocols.join(', '); |
| 26 | + } |
| 27 | + |
| 28 | + const req = http.request({ |
| 29 | + host: 'localhost', |
| 30 | + port, |
| 31 | + path, |
| 32 | + headers |
| 33 | + }); |
| 34 | + |
| 35 | + req.on('upgrade', (res, socket) => { |
| 36 | + resolve({ statusCode: 101, headers: res.headers }); |
| 37 | + socket.destroy(); |
| 38 | + }); |
| 39 | + req.on('response', (res) => { |
| 40 | + resolve({ statusCode: res.statusCode!, headers: res.headers }); |
| 41 | + }); |
| 42 | + req.on('error', reject); |
| 43 | + req.end(); |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +describe("WebSocket Subprotocol endpoints", () => { |
| 48 | + |
| 49 | + let server: DestroyableServer; |
| 50 | + let serverPort: number; |
| 51 | + |
| 52 | + beforeEach(async () => { |
| 53 | + server = makeDestroyable(await createServer({ |
| 54 | + domain: 'localhost' |
| 55 | + })); |
| 56 | + await new Promise<void>((resolve) => server.listen(resolve)); |
| 57 | + serverPort = (server.address() as net.AddressInfo).port; |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(async () => { |
| 61 | + await server.destroy(); |
| 62 | + }); |
| 63 | + |
| 64 | + describe("/ws/subprotocol/{name}", () => { |
| 65 | + |
| 66 | + it("selects the specified subprotocol", async () => { |
| 67 | + const ws = new WebSocket(`ws://localhost:${serverPort}/ws/subprotocol/graphql-ws/echo`, ['graphql-ws']); |
| 68 | + |
| 69 | + await new Promise<void>((resolve, reject) => { |
| 70 | + ws.on('open', resolve); |
| 71 | + ws.on('error', reject); |
| 72 | + }); |
| 73 | + |
| 74 | + expect(ws.protocol).to.equal('graphql-ws'); |
| 75 | + ws.close(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("forces the specified protocol even if client offered a different one", async () => { |
| 79 | + const result = await rawUpgrade(serverPort, '/ws/subprotocol/mqtt/echo', ['other-protocol']); |
| 80 | + |
| 81 | + expect(result.statusCode).to.equal(101); |
| 82 | + expect(result.headers['sec-websocket-protocol']).to.equal('mqtt'); |
| 83 | + }); |
| 84 | + |
| 85 | + it("forces the specified protocol even when client sends no protocols", async () => { |
| 86 | + const result = await rawUpgrade(serverPort, '/ws/subprotocol/mqtt/echo'); |
| 87 | + |
| 88 | + expect(result.statusCode).to.equal(101); |
| 89 | + expect(result.headers['sec-websocket-protocol']).to.equal('mqtt'); |
| 90 | + }); |
| 91 | + |
| 92 | + it("works with chained endpoints", async () => { |
| 93 | + const ws = new WebSocket(`ws://localhost:${serverPort}/ws/subprotocol/test-proto/echo`, ['test-proto']); |
| 94 | + |
| 95 | + await new Promise<void>((resolve, reject) => { |
| 96 | + ws.on('open', resolve); |
| 97 | + ws.on('error', reject); |
| 98 | + }); |
| 99 | + |
| 100 | + expect(ws.protocol).to.equal('test-proto'); |
| 101 | + |
| 102 | + ws.send('hello'); |
| 103 | + const msg = await new Promise<string>((resolve, reject) => { |
| 104 | + ws.on('message', (data) => resolve(data.toString())); |
| 105 | + ws.on('error', reject); |
| 106 | + }); |
| 107 | + expect(msg).to.equal('hello'); |
| 108 | + |
| 109 | + ws.close(); |
| 110 | + }); |
| 111 | + |
| 112 | + }); |
| 113 | + |
| 114 | + describe("/ws/no-subprotocol", () => { |
| 115 | + |
| 116 | + it("omits subprotocol header even when client requests one", async () => { |
| 117 | + const result = await rawUpgrade(serverPort, '/ws/no-subprotocol/echo', ['graphql-ws']); |
| 118 | + |
| 119 | + expect(result.statusCode).to.equal(101); |
| 120 | + expect(result.headers['sec-websocket-protocol']).to.equal(undefined); |
| 121 | + }); |
| 122 | + |
| 123 | + }); |
| 124 | + |
| 125 | + describe("multiple subprotocol endpoints", () => { |
| 126 | + |
| 127 | + it("rejects multiple subprotocol endpoints in the same chain", async () => { |
| 128 | + const result = await rawUpgrade( |
| 129 | + serverPort, |
| 130 | + '/ws/subprotocol/proto-a/subprotocol/proto-b/echo' |
| 131 | + ); |
| 132 | + |
| 133 | + expect(result.statusCode).to.equal(400); |
| 134 | + }); |
| 135 | + |
| 136 | + it("rejects mixing subprotocol and no-subprotocol", async () => { |
| 137 | + const result = await rawUpgrade( |
| 138 | + serverPort, |
| 139 | + '/ws/no-subprotocol/subprotocol/mqtt/echo' |
| 140 | + ); |
| 141 | + |
| 142 | + expect(result.statusCode).to.equal(400); |
| 143 | + }); |
| 144 | + |
| 145 | + }); |
| 146 | + |
| 147 | + describe("default behavior", () => { |
| 148 | + |
| 149 | + it("auto-selects first client protocol when no subprotocol endpoint is used", async () => { |
| 150 | + const ws = new WebSocket(`ws://localhost:${serverPort}/ws/echo`, ['proto-a', 'proto-b']); |
| 151 | + |
| 152 | + await new Promise<void>((resolve, reject) => { |
| 153 | + ws.on('open', resolve); |
| 154 | + ws.on('error', reject); |
| 155 | + }); |
| 156 | + |
| 157 | + expect(ws.protocol).to.equal('proto-a'); |
| 158 | + ws.close(); |
| 159 | + }); |
| 160 | + |
| 161 | + it("returns empty protocol when client sends none", async () => { |
| 162 | + const ws = new WebSocket(`ws://localhost:${serverPort}/ws/echo`); |
| 163 | + |
| 164 | + await new Promise<void>((resolve, reject) => { |
| 165 | + ws.on('open', resolve); |
| 166 | + ws.on('error', reject); |
| 167 | + }); |
| 168 | + |
| 169 | + expect(ws.protocol).to.equal(''); |
| 170 | + ws.close(); |
| 171 | + }); |
| 172 | + |
| 173 | + }); |
| 174 | + |
| 175 | +}); |
0 commit comments