websocket.test.ts
This file
Path
src/adapter/deno/websocket.test.ts
162 lines · 4.0 KiB · TypeScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–162why, one step back1import { Hono } from '../..'2import { Context } from '../../context'3import { upgradeWebSocket } from './websocket'45globalThis.Deno = {} as typeof Deno67describe('WebSockets', () => {8 let app: Hono9 beforeEach(() => {10 app = new Hono()11 })1213 it('Should receive data is valid', async () => {14 const messagePromise = new Promise((resolve) =>15 app.get(16 '/ws',17 upgradeWebSocket(() => ({18 onMessage: (evt) => resolve(evt.data),19 }))20 )21 )22 const socket = new EventTarget() as WebSocket23 Deno.upgradeWebSocket = () => {24 return {25 response: new Response(),26 socket,27 }28 }29 await app.request('/ws', {30 headers: {31 upgrade: 'websocket',32 },33 })34 const data = Math.random().toString()35 socket.onmessage &&36 socket.onmessage(37 new MessageEvent('message', {38 data,39 })40 )41 expect(await messagePromise).toBe(data)42 })4344 it('Should receive data is valid with Options', async () => {45 const messagePromise = new Promise((resolve) =>46 app.get(47 '/ws',48 upgradeWebSocket(49 () => ({50 onMessage: (evt) => resolve(evt.data),51 }),52 {53 idleTimeout: 5000,54 }55 )56 )57 )58 const socket = new EventTarget() as WebSocket59 Deno.upgradeWebSocket = () => {60 return {61 response: new Response(),62 socket,63 }64 }65 await app.request('/ws', {66 headers: {67 upgrade: 'websocket',68 },69 })70 const data = Math.random().toString()71 socket.onmessage &&72 socket.onmessage(73 new MessageEvent('message', {74 data,75 })76 )77 expect(await messagePromise).toBe(data)78 })7980 it('Should pass the first Sec-WebSocket-Protocol value as the protocol option', async () => {81 app.get(82 '/ws',83 upgradeWebSocket(() => ({}))84 )85 const socket = new EventTarget() as WebSocket86 let passedOptions: Deno.UpgradeWebSocketOptions | undefined87 Deno.upgradeWebSocket = (_req, options) => {88 passedOptions = options89 return {90 response: new Response(),91 socket,92 }93 }94 await app.request('/ws', {95 headers: {96 upgrade: 'websocket',97 'sec-websocket-protocol': 'rivet, rivet_target.actor, rivet_actor.19c4f9038947cdcb',98 },99 })100 expect(passedOptions?.protocol).toBe('rivet')101 })102103 it('Should not set the protocol option when Sec-WebSocket-Protocol is absent', async () => {104 app.get(105 '/ws',106 upgradeWebSocket(() => ({}))107 )108 const socket = new EventTarget() as WebSocket109 let passedOptions: Deno.UpgradeWebSocketOptions | undefined110 Deno.upgradeWebSocket = (_req, options) => {111 passedOptions = options112 return {113 response: new Response(),114 socket,115 }116 }117 await app.request('/ws', {118 headers: {119 upgrade: 'websocket',120 },121 })122 expect(passedOptions?.protocol).toBeUndefined()123 })124125 it('Should let an explicit protocol option take precedence over the request header', async () => {126 app.get(127 '/ws',128 upgradeWebSocket(() => ({}), { protocol: 'user-chosen' })129 )130 const socket = new EventTarget() as WebSocket131 let passedOptions: Deno.UpgradeWebSocketOptions | undefined132 Deno.upgradeWebSocket = (_req, options) => {133 passedOptions = options134 return {135 response: new Response(),136 socket,137 }138 }139 await app.request('/ws', {140 headers: {141 upgrade: 'websocket',142 'sec-websocket-protocol': 'header-value',143 },144 })145 expect(passedOptions?.protocol).toBe('user-chosen')146 })147148 it('Should call next() when header does not have upgrade', async () => {149 const next = vi.fn()150 await upgradeWebSocket(() => ({}))(151 new Context(152 new Request('http://localhost', {153 headers: {154 Upgrade: 'example',155 },156 })157 ),158 next159 )160 expect(next).toBeCalled()161 })162})