Skip to content

Commit

Permalink
cherry-pick(#33095): fix(routeWebSocket): make sure ws url without tr…
Browse files Browse the repository at this point in the history
…ailing slash is supported (#33112)
  • Loading branch information
dgozman authored Oct 15, 2024
1 parent 78c43bc commit 3d7ef3c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export function inject(globalThis: GlobalThis) {

this.url = typeof url === 'string' ? url : url.href;
try {
this.url = new URL(url).href;
this._origin = new URL(url).origin;
} catch {
}
Expand Down
24 changes: 24 additions & 0 deletions tests/library/route-web-socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,3 +508,27 @@ test('should throw when connecting twice', async ({ page, server }) => {
const error = await promise;
expect(error.message).toContain('Already connected to the server');
});

test('should work with no trailing slash', async ({ page, server }) => {
const log: string[] = [];
// No trailing slash!
await page.routeWebSocket('ws://localhost:' + server.PORT, ws => {
ws.onMessage(message => {
log.push(message as string);
ws.send('response');
});
});

await page.goto('about:blank');
await page.evaluate(({ port }) => {
window.log = [];
// No trailing slash!
window.ws = new WebSocket('ws://localhost:' + port);
window.ws.addEventListener('message', event => window.log.push(event.data));
}, { port: server.PORT });

await expect.poll(() => page.evaluate(() => window.ws.readyState)).toBe(1);
await page.evaluate(() => window.ws.send('query'));
await expect.poll(() => log).toEqual(['query']);
expect(await page.evaluate(() => window.log)).toEqual(['response']);
});

0 comments on commit 3d7ef3c

Please sign in to comment.