Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(workerd): add tests for WebSocket #2891

Merged
merged 1 commit into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@
"vite-plugin-fastly-js-compute": "^0.4.2",
"vitest": "^1.2.2",
"wrangler": "^3.58.0",
"ws": "^8.17.0",
"zod": "^3.20.2"
},
"engines": {
Expand Down
38 changes: 38 additions & 0 deletions runtime_tests/workerd/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { unstable_dev } from 'wrangler'
import type { UnstableDevWorker } from 'wrangler'
import { WebSocket } from 'ws'

describe('workerd', () => {
let worker: UnstableDevWorker
Expand Down Expand Up @@ -29,3 +30,40 @@ describe('workerd', () => {
expect(await res.text()).toBe('Hono')
})
})

describe('workerd with WebSocket', () => {
// worker.fetch does not support WebSocket:
// https://github.com/cloudflare/workers-sdk/issues/4573#issuecomment-1850420973
it('Should handle the WebSocket connection correctly', async () => {
const worker = await unstable_dev('./runtime_tests/workerd/index.ts', {
experimental: { disableExperimentalWarning: true },
})
const ws = new WebSocket(`ws://${worker.address}:${worker.port}/ws`)

const openHandler = vi.fn()
const messageHandler = vi.fn()
const closeHandler = vi.fn()

const waitForOpen = new Promise((resolve) => {
ws.addEventListener('open', () => {
openHandler()
ws.send('Hello')
resolve(undefined)
})
ws.addEventListener('close', async () => {
closeHandler()
})
ws.addEventListener('message', async (event) => {
messageHandler(event.data)
ws.close()
})
})

await waitForOpen
await worker.stop()

expect(openHandler).toHaveBeenCalled()
expect(messageHandler).toHaveBeenCalledWith('Hello')
expect(closeHandler).toHaveBeenCalled()
})
})
12 changes: 12 additions & 0 deletions runtime_tests/workerd/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { env, getRuntimeKey } from '../../src/helper/adapter'
import { upgradeWebSocket } from '../../src/adapter/cloudflare-workers'
import { Hono } from '../../src/hono'

const app = new Hono()
Expand All @@ -10,4 +11,15 @@ app.get('/env', (c) => {
return c.text(NAME)
})

app.get(
'/ws',
upgradeWebSocket(() => {
return {
onMessage(event, ws) {
ws.send(event.data as string)
},
}
})
)

export default app
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
# bun ./bun.lockb --hash: BAD784DA2D3E8912-7bb272adaca1c26f-B77747473A8BC735-f20e673ef1d530e3
# bun ./bun.lockb --hash: 733DAF3BDF9F84ED-89e11d5f0e9dc6ef-7974BC9F6F5A02AD-41e5e99e9db5457f


"@aashutoshrathi/word-wrap@^1.2.3":
Expand Down Expand Up @@ -6105,6 +6105,11 @@ ws@^8.11.0, ws@^8.13.0:
resolved "https://registry.npmjs.org/ws/-/ws-8.16.0.tgz"
integrity sha512-HS0c//TP7Ina87TfiPUz1rQzMhHrl/SG2guqRcTOIUYD2q8uhUdNHZYJUaQ8aTGPzCh+c6oawMKW35nFl1dxyQ==

ws@^8.17.0:
version "8.17.0"
resolved "https://registry.npmjs.org/ws/-/ws-8.17.0.tgz"
integrity sha512-uJq6108EgZMAl20KagGkzCKfMEjxmKvZHG7Tlq0Z6nOky7YF7aq4mOx6xK8TJ/i1LeK4Qus7INktacctDgY8Ow==

xdg-basedir@^4.0.0:
version "4.0.0"
resolved "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-4.0.0.tgz"
Expand Down