websocket.test.ts
This file
Path
src/adapter/bun/websocket.test.ts
166 lines 路 4.7 KiB 路 TypeScript
Last changed by
fix(bun): report the requested subprotocol on WSContext.protocol (#5059)greymothbefore adoption872997d
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167import { Context } from '../../context'import type { BunWebSocketData, BunServerWebSocket } from './websocket'import { createWSContext, websocket, upgradeWebSocket, createBunWebSocket } from './websocket'
describe('createWSContext()', () => { it('Should send() and close() works', () => { const send = vi.fn() const close = vi.fn() const ws = createWSContext({ send(data) { send(data) }, close(code, reason) { close(code, reason) }, data: {}, } as BunServerWebSocket<BunWebSocketData>) ws.send('message') expect(send).toBeCalled() ws.close() expect(close).toBeCalled() })})describe('upgradeWebSocket()', () => { beforeAll(() => { // @ts-expect-error patch global globalThis.CloseEvent = Event }) afterAll(() => { // @ts-expect-error patch global delete globalThis.CloseEvent }) it('Should throw error when server is null', async () => { const run = async () => await upgradeWebSocket(() => ({}))( new Context(new Request('http://localhost'), { env: { server: null, }, }), () => Promise.resolve() )
await expect(run).rejects.toThrowError(/env has/) }) it('Should response null when upgraded', async () => { const upgraded = await upgradeWebSocket(() => ({}))( new Context(new Request('http://localhost'), { env: { upgrade: () => true, }, }), () => Promise.resolve() ) expect(upgraded).toBeTruthy() }) it('Should response undefined when upgrade failed', async () => { const upgraded = await upgradeWebSocket(() => ({}))( new Context(new Request('http://localhost'), { env: { upgrade: () => undefined, }, }), () => Promise.resolve() ) expect(upgraded).toBeFalsy() }) it('Should expose the requested subprotocol on WSContext.protocol', async () => { let data: BunWebSocketData | undefined await upgradeWebSocket(() => ({}))( new Context( new Request('http://localhost/ws', { headers: { 'sec-websocket-protocol': 'chat, superchat' }, }), { env: { upgrade(_req: Request, options: { data: BunWebSocketData }) { data = options.data return true }, }, } ), () => Promise.resolve() )
const ws = createWSContext({ data, readyState: 1 } as BunServerWebSocket<BunWebSocketData>) expect(ws.protocol).toBe('chat') }) it('Should set protocol to an empty string when no subprotocol is requested', async () => { let data: BunWebSocketData | undefined await upgradeWebSocket(() => ({}))( new Context(new Request('http://localhost/ws'), { env: { upgrade(_req: Request, options: { data: BunWebSocketData }) { data = options.data return true }, }, }), () => Promise.resolve() )
const ws = createWSContext({ data, readyState: 1 } as BunServerWebSocket<BunWebSocketData>) expect(ws.protocol).toBe('') }) it('Should events are called', async () => { const open = vi.fn() const message = vi.fn() const close = vi.fn()
const ws = { data: { events: { // eslint-disable-next-line @typescript-eslint/no-unused-vars onOpen(evt, ws) { open() }, // eslint-disable-next-line @typescript-eslint/no-unused-vars onMessage(evt, ws) { message() if (evt.data instanceof ArrayBuffer) { receivedArrayBuffer = evt.data } }, // eslint-disable-next-line @typescript-eslint/no-unused-vars onClose(evt, ws) { close() }, }, }, } as BunServerWebSocket<BunWebSocketData>
let receivedArrayBuffer: ArrayBuffer | undefined = undefined await upgradeWebSocket(() => ({}))( new Context(new Request('http://localhost'), { env: { upgrade() { return true }, }, }), () => Promise.resolve() )
websocket.open(ws) expect(open).toBeCalled()
websocket.message(ws, 'message') expect(message).toBeCalled()
websocket.message(ws, new Uint8Array(16)) expect(receivedArrayBuffer).toBeInstanceOf(ArrayBuffer) expect(receivedArrayBuffer!.byteLength).toBe(16) websocket.close(ws) expect(close).toBeCalled() })})
describe('createBunWebSocket()', () => { it('Should return upgradeWebSocket and websocket', () => { const result = createBunWebSocket() expect(result.upgradeWebSocket).toBe(upgradeWebSocket) expect(result.websocket).toBe(websocket) })})