fix(routeWebSocket): make it work with http(s) baseURL (#33457)

This commit is contained in:
Dmitry Gozman 2024-11-05 11:46:05 -08:00 committed by GitHub
parent 1003f3429c
commit 697d7a40b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 28 additions and 1 deletions

View File

@ -97,8 +97,12 @@ export function urlMatchesEqual(match1: URLMatch, match2: URLMatch) {
export function urlMatches(baseURL: string | undefined, urlString: string, match: URLMatch | undefined): boolean {
if (match === undefined || match === '')
return true;
if (isString(match) && !match.startsWith('*'))
if (isString(match) && !match.startsWith('*')) {
// Allow http(s) baseURL to match ws(s) urls.
if (baseURL && /^https?:\/\//.test(baseURL) && /^wss?:\/\//.test(urlString))
baseURL = baseURL.replace(/^http/, 'ws');
match = constructURLBasedOnBaseURL(baseURL, match);
}
if (isString(match))
match = globToRegex(match);
if (isRegExp(match))

View File

@ -539,3 +539,26 @@ test('should work with no trailing slash', async ({ page, server }) => {
await expect.poll(() => log).toEqual(['query']);
expect(await page.evaluate(() => window.log)).toEqual(['response']);
});
test('should work with baseURL', async ({ contextFactory, server }) => {
const context = await contextFactory({ baseURL: 'http://localhost:' + server.PORT });
const page = await context.newPage();
await page.routeWebSocket('/ws', ws => {
ws.onMessage(message => {
ws.send(message);
});
});
await setupWS(page, server.PORT, 'blob');
await page.evaluate(async () => {
await window.wsOpened;
window.ws.send('echo');
});
await expect.poll(() => page.evaluate(() => window.log)).toEqual([
'open',
`message: data=echo origin=ws://localhost:${server.PORT} lastEventId=`,
]);
});