import { describe, it, expect } from 'vitest'; import WebSocket from 'ws'; import { channel } from '../src/channel'; import { createHttpServer, type WsRouteDef } from '../src/http'; import { Unauthorized } from '../src/signals'; const url = (server: import('http').Server, path: string) => { const a = server.address(); if (a && typeof a === 'object') return `ws://127.0.0.1:${a.port}${path}`; throw new Error('no address'); }; describe('ws upgrade', () => { it('echoes inbound to outbound and aborts on close', async () => { let aborted = false; const wsRoutes: WsRouteDef[] = [{ pattern: '/echo', open: async ({ inbound, abort }) => { abort.addEventListener('abort', () => { aborted = true; }); const out = channel(); (async () => { for await (const m of inbound) out.push(`echo:${m}`); out.close(); })(); return out; }, }]; const server = createHttpServer([], wsRoutes); await new Promise((r) => server.listen(0, r)); const client = new WebSocket(url(server, '/echo')); await new Promise((r) => client.on('open', r)); const got = new Promise((r) => client.on('message', (d) => r(d.toString()))); client.send('hi'); expect(await got).toBe('echo:hi'); client.close(); await new Promise((r) => setTimeout(r, 50)); expect(aborted).toBe(true); server.close(); }); it('closes with a 4000+status code when the WS open() throws an HttpError', async () => { const wsRoutes = [{ pattern: '/secure', open: async () => { throw new Unauthorized('no token'); } }]; const server = createHttpServer([], wsRoutes as any); await new Promise((r) => server.listen(0, r)); const port = (server.address() as any).port; const WebSocket = (await import('ws')).default; const client = new WebSocket(`ws://127.0.0.1:${port}/secure`); const { code, reason } = await new Promise<{ code: number; reason: string }>((res) => { client.on('close', (code: number, reason: Buffer) => res({ code, reason: reason.toString() })); }); expect(code).toBe(4401); expect(reason).toContain('no token'); server.close(); }); });