Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/isomorphic/urlMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,10 @@ export function resolveGlobToRegexPattern(baseURL: string | undefined, glob: str
}

function toWebSocketBaseUrl(baseURL: string | undefined) {
// Allow http(s) baseURL to match ws(s) urls.
if (baseURL && /^https?:\/\//.test(baseURL))
baseURL = baseURL.replace(/^http/, 'ws');
// Allow http(s) baseURL to match ws(s) urls. Schemes are case-insensitive,
// same as elsewhere in this file, so 'HTTP://...' should be rewritten too.
if (baseURL && /^https?:\/\//i.test(baseURL))
baseURL = baseURL.replace(/^https?/i, scheme => scheme.toLowerCase() === 'https' ? 'wss' : 'ws');
return baseURL;
}

Expand Down
24 changes: 24 additions & 0 deletions tests/library/route-web-socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,30 @@ test('should work with baseURL', async ({ contextFactory, server }) => {
]);
});

test('should work with baseURL regardless of scheme casing', async ({ contextFactory, server }) => {
// baseURL schemes are case-insensitive, same as everywhere else in URL matching.
const context = await contextFactory({ baseURL: 'HTTP://' + server.HOST });
const page = await context.newPage();

await page.routeWebSocket('/ws', ws => {
ws.onMessage(message => {
ws.send(message);
});
});

await setupWS(page, server, '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://${server.HOST} lastEventId=`,
]);
});

test('should expose protocols to the route handler', async ({ page, server }) => {
const routes: WebSocketRoute[] = [];
await page.routeWebSocket(/.*/, ws => {
Expand Down
Loading