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

feat(client): Add WebSocket Provider Integration Tests and Enhance WebSocket Initialization #3213

Merged
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
49 changes: 49 additions & 0 deletions src/client/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1240,3 +1240,52 @@ describe('Redirect response - only types', () => {
}
})
})

describe('WebSocket Provider Integration', () => {
const app = new Hono()
const route = app.get(
'/',
upgradeWebSocket((c) => ({
onMessage(event, ws) {
ws.send('Hello from server!')
},
onClose() {
console.log('Connection closed')
},
}))
)

type AppType = typeof route

const server = setupServer()
beforeAll(() => server.listen())
afterEach(() => {
vi.clearAllMocks()
server.resetHandlers()
})
afterAll(() => server.close())

it.each([
{
description: 'should initialize the WebSocket provider correctly',
url: 'http://localhost',
query: undefined,
expectedUrl: 'ws://localhost/index',
},
{
description: 'should correctly add query parameters to the WebSocket URL',
url: 'http://localhost',
query: { id: '123', type: 'test', tag: ['a', 'b'] },
expectedUrl: 'ws://localhost/index?id=123&type=test&tag=a&tag=b',
},
])('$description', ({ url, expectedUrl, query }) => {
const webSocketMock = vi.fn()
const client = hc<AppType>(url, {
webSocket(url, options) {
return webSocketMock(url, options)
},
})
client.index.$ws({ query })
expect(webSocketMock).toHaveBeenCalledWith(expectedUrl, undefined)
})
})
8 changes: 7 additions & 1 deletion src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,14 @@ export const hc = <T extends Hono<any, any, any>>(
}
})
}
const establishWebSocket = (...args: ConstructorParameters<typeof WebSocket>) => {
if (options?.webSocket !== undefined && typeof options.webSocket === 'function') {
return options.webSocket(...args)
}
return new WebSocket(...args)
}

return new WebSocket(targetUrl.toString())
return establishWebSocket(targetUrl.toString())
}

const req = new ClientRequestImpl(url, method)
Expand Down
1 change: 1 addition & 0 deletions src/client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type HonoRequest = (typeof Hono.prototype)['request']

export type ClientRequestOptions<T = unknown> = {
fetch?: typeof fetch | HonoRequest
webSocket?: (...args: ConstructorParameters<typeof WebSocket>) => WebSocket
/**
* Standard `RequestInit`, caution that this take highest priority
* and could be used to overwrite things that Hono sets for you, like `body | method | headers`.
Expand Down