Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–67why, one step back
1import { Context } from '../../context'
2import type { BunWebSocketData, BunServerWebSocket } from './websocket'3import { createWSContext, websocket, upgradeWebSocket, createBunWebSocket } from './websocket'45describe('createWSContext()', () => {6 it('Should send() and close() works', () => {7 const send = vi.fn()8 const close = vi.fn()9 const ws = createWSContext({10 send(data) {11 send(data)12 },13 close(code, reason) {14 close(code, reason)15 },16 data: {},17 } as BunServerWebSocket<BunWebSocketData>)18 ws.send('message')19 expect(send).toBeCalled()20 ws.close()21 expect(close).toBeCalled()22 })23})24describe('upgradeWebSocket()', () => {25 beforeAll(() => {26 // @ts-expect-error patch global27 globalThis.CloseEvent = Event28 })29 afterAll(() => {30 // @ts-expect-error patch global31 delete globalThis.CloseEvent32 })33 it('Should throw error when server is null', async () => {34 const run = async () =>35 await upgradeWebSocket(() => ({}))(36 new Context(new Request('http://localhost'), {37 env: {38 server: null,39 },40 }),41 () => Promise.resolve()42 )4344 await expect(run).rejects.toThrowError(/env has/)45 })46 it('Should response null when upgraded', async () => {47 const upgraded = await upgradeWebSocket(() => ({}))(48 new Context(new Request('http://localhost'), {49 env: {50 upgrade: () => true,51 },52 }),53 () => Promise.resolve()54 )55 expect(upgraded).toBeTruthy()56 })57 it('Should response undefined when upgrade failed', async () => {58 const upgraded = await upgradeWebSocket(() => ({}))(59 new Context(new Request('http://localhost'), {60 env: {61 upgrade: () => undefined,62 },63 }),64 () => Promise.resolve()65 )66 expect(upgraded).toBeFalsy()67 })

fix(bun): report the requested subprotocol on WSContext.protocol (#5059)greymothbefore adoption872997d

lines 68–106why, one step back
68 it('Should expose the requested subprotocol on WSContext.protocol', async () => {
69 let data: BunWebSocketData | undefined70 await upgradeWebSocket(() => ({}))(71 new Context(72 new Request('http://localhost/ws', {73 headers: { 'sec-websocket-protocol': 'chat, superchat' },74 }),75 {76 env: {77 upgrade(_req: Request, options: { data: BunWebSocketData }) {78 data = options.data79 return true80 },81 },82 }83 ),84 () => Promise.resolve()85 )8687 const ws = createWSContext({ data, readyState: 1 } as BunServerWebSocket<BunWebSocketData>)88 expect(ws.protocol).toBe('chat')89 })90 it('Should set protocol to an empty string when no subprotocol is requested', async () => {91 let data: BunWebSocketData | undefined92 await upgradeWebSocket(() => ({}))(93 new Context(new Request('http://localhost/ws'), {94 env: {95 upgrade(_req: Request, options: { data: BunWebSocketData }) {96 data = options.data97 return true98 },99 },100 }),101 () => Promise.resolve()102 )103104 const ws = createWSContext({ data, readyState: 1 } as BunServerWebSocket<BunWebSocketData>)105 expect(ws.protocol).toBe('')106 })

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 107–166why, one step back
107 it('Should events are called', async () => {
108 const open = vi.fn()109 const message = vi.fn()110 const close = vi.fn()111112 const ws = {113 data: {114 events: {115 // eslint-disable-next-line @typescript-eslint/no-unused-vars116 onOpen(evt, ws) {117 open()118 },119 // eslint-disable-next-line @typescript-eslint/no-unused-vars120 onMessage(evt, ws) {121 message()122 if (evt.data instanceof ArrayBuffer) {123 receivedArrayBuffer = evt.data124 }125 },126 // eslint-disable-next-line @typescript-eslint/no-unused-vars127 onClose(evt, ws) {128 close()129 },130 },131 },132 } as BunServerWebSocket<BunWebSocketData>133134 let receivedArrayBuffer: ArrayBuffer | undefined = undefined135 await upgradeWebSocket(() => ({}))(136 new Context(new Request('http://localhost'), {137 env: {138 upgrade() {139 return true140 },141 },142 }),143 () => Promise.resolve()144 )145146 websocket.open(ws)147 expect(open).toBeCalled()148149 websocket.message(ws, 'message')150 expect(message).toBeCalled()151152 websocket.message(ws, new Uint8Array(16))153 expect(receivedArrayBuffer).toBeInstanceOf(ArrayBuffer)154 expect(receivedArrayBuffer!.byteLength).toBe(16)155 websocket.close(ws)156 expect(close).toBeCalled()157 })158})159160describe('createBunWebSocket()', () => {161 it('Should return upgradeWebSocket and websocket', () => {162 const result = createBunWebSocket()163 expect(result.upgradeWebSocket).toBe(upgradeWebSocket)164 expect(result.websocket).toBe(websocket)165 })166})