Skip to content
Myr

fix(client): Add Query Parameter Support to WebSocket Client in `hono/client` (#3066)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* chore(client): fix url with query parameter websocket client * chore(client): remove log

authored by naporitan · committed by GitHub · parent 2d452f2 · Browse files · 2 files+80 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+80 added−2 removed

  • tests:+74 added−0 removed
  • other:+6 added−2 removed

2 files+80 added−2 removed

modified:src/client/client.test.ts+74 added
1042 unmodified lines
1043 expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index')1044 })1045})10461047describe('WebSocket URL Protocol Translation with Query Parameters', () => {1048 const app = new Hono()1049 const route = app.get(1050 '/',1051 upgradeWebSocket((c) => ({1052 onMessage(event, ws) {1053 ws.send('Hello from server!')1054 },1055 onClose: () => {1056 console.log('Connection closed')1057 },1058 }))1059 )10601061 type AppType = typeof route10621063 const server = setupServer()1064 const webSocketMock = vi.fn()10651066 beforeAll(() => server.listen())1067 beforeEach(() => {1068 vi.stubGlobal('WebSocket', webSocketMock)1069 })1070 afterEach(() => {1071 vi.clearAllMocks()1072 server.resetHandlers()1073 })1074 afterAll(() => server.close())10751076 it('Translates HTTP to ws and includes query parameters', async () => {1077 const client = hc<AppType>('http://localhost')1078 client.index.$ws({1079 query: {1080 id: '123',1081 type: 'test',1082 },1083 })1084 expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index?id=123&type=test')1085 })10861087 it('Translates HTTPS to wss and includes query parameters', async () => {1088 const client = hc<AppType>('https://localhost')1089 client.index.$ws({1090 query: {1091 id: '456',1092 type: 'secure',1093 },1094 })1095 expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index?id=456&type=secure')1096 })10971098 it('Keeps ws unchanged and includes query parameters', async () => {1099 const client = hc<AppType>('ws://localhost')1100 client.index.$ws({1101 query: {1102 id: '789',1103 type: 'plain',1104 },1105 })1106 expect(webSocketMock).toHaveBeenCalledWith('ws://localhost/index?id=789&type=plain')1107 })11081109 it('Keeps wss unchanged and includes query parameters', async () => {1110 const client = hc<AppType>('wss://localhost')1111 client.index.$ws({1112 query: {1113 id: '1011',1114 type: 'secure',1115 },1116 })1117 expect(webSocketMock).toHaveBeenCalledWith('wss://localhost/index?id=1011&type=secure')1118 })1119})11201121describe('Client can be console.log in react native', () => {1122 it('Returns a function name with function.name.toString', async () => {1123 const client = hc('http://localhost')1124 // @ts-ignore
117 unmodified lines
modified:src/client/client.ts+6 added-2 removed