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

This commit is contained in:
Dmitry Gozman 2024-10-15 02:08:27 -07:00 committed by GitHub
parent 8a275e5a5b
commit 17837e564d
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;
try {
this.url = new URL(url).href;
this._origin = new URL(url).origin;
} catch {
}

View File

@ -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']);
});