Skip to content

Commit 3d7ef3c

Browse files
authored
cherry-pick(#33095): fix(routeWebSocket): make sure ws url without trailing slash is supported (#33112)
1 parent 78c43bc commit 3d7ef3c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

packages/playwright-core/src/server/injected/webSocketMock.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ export function inject(globalThis: GlobalThis) {
143143

144144
this.url = typeof url === 'string' ? url : url.href;
145145
try {
146+
this.url = new URL(url).href;
146147
this._origin = new URL(url).origin;
147148
} catch {
148149
}

tests/library/route-web-socket.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,3 +508,27 @@ test('should throw when connecting twice', async ({ page, server }) => {
508508
const error = await promise;
509509
expect(error.message).toContain('Already connected to the server');
510510
});
511+
512+
test('should work with no trailing slash', async ({ page, server }) => {
513+
const log: string[] = [];
514+
// No trailing slash!
515+
await page.routeWebSocket('ws://localhost:' + server.PORT, ws => {
516+
ws.onMessage(message => {
517+
log.push(message as string);
518+
ws.send('response');
519+
});
520+
});
521+
522+
await page.goto('about:blank');
523+
await page.evaluate(({ port }) => {
524+
window.log = [];
525+
// No trailing slash!
526+
window.ws = new WebSocket('ws://localhost:' + port);
527+
window.ws.addEventListener('message', event => window.log.push(event.data));
528+
}, { port: server.PORT });
529+
530+
await expect.poll(() => page.evaluate(() => window.ws.readyState)).toBe(1);
531+
await page.evaluate(() => window.ws.send('query'));
532+
await expect.poll(() => log).toEqual(['query']);
533+
expect(await page.evaluate(() => window.log)).toEqual(['response']);
534+
});

0 commit comments

Comments
 (0)