cherry-pick(#33095): fix(routeWebSocket): make sure ws url without trailing slash is supported (#33112)
This commit is contained in:
parent
78c43bc5d3
commit
3d7ef3c062
|
|
@ -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 {
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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']);
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue