websocket.test.ts
This file
Path
adapters/bun/src/websocket.test.ts
167 lines · 4.7 KiB · TypeScript
Last changed by
feat(adapters): add @hono/bun as a workspace package (#5447)Yusuke Wadabefore adoptionfc2343d
History of this fileTake it
feat(adapters): add @hono/bun as a workspace package (#5447)Yusuke Wadabefore adoptionfc2343d
lines 1–167why, one step back1import { afterAll, beforeAll, describe, expect, it, mock } from 'bun:test'2import { Context } from 'hono'3import type { BunWebSocketData, BunServerWebSocket } from './websocket'4import { createWSContext, websocket, upgradeWebSocket, createBunWebSocket } from './websocket'56describe('createWSContext()', () => {7 it('Should send() and close() works', () => {8 const send = mock()9 const close = mock()10 const ws = createWSContext({11 send(data) {12 send(data)13 },14 close(code, reason) {15 close(code, reason)16 },17 data: {},18 } as BunServerWebSocket<BunWebSocketData>)19 ws.send('message')20 expect(send).toBeCalled()21 ws.close()22 expect(close).toBeCalled()23 })24})25describe('upgradeWebSocket()', () => {26 beforeAll(() => {27 // @ts-expect-error patch global28 globalThis.CloseEvent = Event29 })30 afterAll(() => {31 // @ts-expect-error patch global32 delete globalThis.CloseEvent33 })34 it('Should throw error when server is null', async () => {35 const run = async () =>36 await upgradeWebSocket(() => ({}))(37 new Context(new Request('http://localhost'), {38 env: {39 server: null,40 },41 }),42 () => Promise.resolve()43 )4445 await expect(run()).rejects.toThrowError(/env has/)46 })47 it('Should response null when upgraded', async () => {48 const upgraded = await upgradeWebSocket(() => ({}))(49 new Context(new Request('http://localhost'), {50 env: {51 upgrade: () => true,52 },53 }),54 () => Promise.resolve()55 )56 expect(upgraded).toBeTruthy()57 })58 it('Should response undefined when upgrade failed', async () => {59 const upgraded = await upgradeWebSocket(() => ({}))(60 new Context(new Request('http://localhost'), {61 env: {62 upgrade: () => undefined,63 },64 }),65 () => Promise.resolve()66 )67 expect(upgraded).toBeFalsy()68 })69 it('Should expose the requested subprotocol on WSContext.protocol', async () => {70 let data: BunWebSocketData | undefined71 await upgradeWebSocket(() => ({}))(72 new Context(73 new Request('http://localhost/ws', {74 headers: { 'sec-websocket-protocol': 'chat, superchat' },75 }),76 {77 env: {78 upgrade(_req: Request, options: { data: BunWebSocketData }) {79 data = options.data80 return true81 },82 },83 }84 ),85 () => Promise.resolve()86 )8788 const ws = createWSContext({ data, readyState: 1 } as BunServerWebSocket<BunWebSocketData>)89 expect(ws.protocol).toBe('chat')90 })91 it('Should set protocol to an empty string when no subprotocol is requested', async () => {92 let data: BunWebSocketData | undefined93 await upgradeWebSocket(() => ({}))(94 new Context(new Request('http://localhost/ws'), {95 env: {96 upgrade(_req: Request, options: { data: BunWebSocketData }) {97 data = options.data98 return true99 },100 },101 }),102 () => Promise.resolve()103 )104105 const ws = createWSContext({ data, readyState: 1 } as BunServerWebSocket<BunWebSocketData>)106 expect(ws.protocol).toBe('')107 })108 it('Should events are called', async () => {109 const open = mock()110 const message = mock()111 const close = mock()112113 const ws = {114 data: {115 events: {116 // eslint-disable-next-line @typescript-eslint/no-unused-vars117 onOpen(evt, ws) {118 open()119 },120 // eslint-disable-next-line @typescript-eslint/no-unused-vars121 onMessage(evt, ws) {122 message()123 if (evt.data instanceof ArrayBuffer) {124 receivedArrayBuffer = evt.data125 }126 },127 // eslint-disable-next-line @typescript-eslint/no-unused-vars128 onClose(evt, ws) {129 close()130 },131 },132 },133 } as BunServerWebSocket<BunWebSocketData>134135 let receivedArrayBuffer: ArrayBuffer | undefined = undefined136 await upgradeWebSocket(() => ({}))(137 new Context(new Request('http://localhost'), {138 env: {139 upgrade() {140 return true141 },142 },143 }),144 () => Promise.resolve()145 )146147 websocket.open(ws)148 expect(open).toBeCalled()149150 websocket.message(ws, 'message')151 expect(message).toBeCalled()152153 websocket.message(ws, new Uint8Array(16))154 expect(receivedArrayBuffer).toBeInstanceOf(ArrayBuffer)155 expect(receivedArrayBuffer!.byteLength).toBe(16)156 websocket.close(ws)157 expect(close).toBeCalled()158 })159})160161describe('createBunWebSocket()', () => {162 it('Should return upgradeWebSocket and websocket', () => {163 const result = createBunWebSocket()164 expect(result.upgradeWebSocket).toBe(upgradeWebSocket)165 expect(result.websocket).toBe(websocket)166 })167})