Skip to content

Commit 697d7a4

Browse files
authored
fix(routeWebSocket): make it work with http(s) baseURL (#33457)
1 parent 1003f34 commit 697d7a4

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

packages/playwright-core/src/utils/isomorphic/urlMatch.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,12 @@ export function urlMatchesEqual(match1: URLMatch, match2: URLMatch) {
9797
export function urlMatches(baseURL: string | undefined, urlString: string, match: URLMatch | undefined): boolean {
9898
if (match === undefined || match === '')
9999
return true;
100-
if (isString(match) && !match.startsWith('*'))
100+
if (isString(match) && !match.startsWith('*')) {
101+
// Allow http(s) baseURL to match ws(s) urls.
102+
if (baseURL && /^https?:\/\//.test(baseURL) && /^wss?:\/\//.test(urlString))
103+
baseURL = baseURL.replace(/^http/, 'ws');
101104
match = constructURLBasedOnBaseURL(baseURL, match);
105+
}
102106
if (isString(match))
103107
match = globToRegex(match);
104108
if (isRegExp(match))

tests/library/route-web-socket.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,26 @@ test('should work with no trailing slash', async ({ page, server }) => {
539539
await expect.poll(() => log).toEqual(['query']);
540540
expect(await page.evaluate(() => window.log)).toEqual(['response']);
541541
});
542+
543+
test('should work with baseURL', async ({ contextFactory, server }) => {
544+
const context = await contextFactory({ baseURL: 'http://localhost:' + server.PORT });
545+
const page = await context.newPage();
546+
547+
await page.routeWebSocket('/ws', ws => {
548+
ws.onMessage(message => {
549+
ws.send(message);
550+
});
551+
});
552+
553+
await setupWS(page, server.PORT, 'blob');
554+
555+
await page.evaluate(async () => {
556+
await window.wsOpened;
557+
window.ws.send('echo');
558+
});
559+
560+
await expect.poll(() => page.evaluate(() => window.log)).toEqual([
561+
'open',
562+
`message: data=echo origin=ws://localhost:${server.PORT} lastEventId=`,
563+
]);
564+
});

0 commit comments

Comments
 (0)