Skip to content

Commit

Permalink
fix(routeWebSocket): make it work with http(s) baseURL (#33457)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman authored Nov 5, 2024
1 parent 1003f34 commit 697d7a4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/playwright-core/src/utils/isomorphic/urlMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
23 changes: 23 additions & 0 deletions tests/library/route-web-socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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=`,
]);
});

0 comments on commit 697d7a4

Please sign in to comment.