cherry-pick(#33095): fix(routeWebSocket): make sure ws url without trailing slash is supported (#33112)

This commit is contained in:
Dmitry Gozman 2024-10-15 04:56:29 -07:00 committed by GitHub
parent 78c43bc5d3
commit 3d7ef3c062
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 0 deletions

View file

@ -143,6 +143,7 @@ export function inject(globalThis: GlobalThis) {
this.url = typeof url === 'string' ? url : url.href; this.url = typeof url === 'string' ? url : url.href;
try { try {
this.url = new URL(url).href;
this._origin = new URL(url).origin; this._origin = new URL(url).origin;
} catch { } catch {
} }

View file

@ -508,3 +508,27 @@ test('should throw when connecting twice', async ({ page, server }) => {
const error = await promise; const error = await promise;
expect(error.message).toContain('Already connected to the server'); 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']);
});