fix: do not change glob pattern when converting to url

Fixes https://github.com/microsoft/playwright/issues/34915
This commit is contained in:
Yury Semikhatsky 2025-02-25 12:33:51 -08:00
parent 08ea36caa2
commit 267f096a53
2 changed files with 30 additions and 2 deletions

View file

@ -101,7 +101,21 @@ export function urlMatches(baseURL: string | undefined, urlString: string, match
// 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 (baseURL) {
// String starting with a dot are treated as explicit relative URL.
if (match.startsWith('.')) {
match = constructURLBasedOnBaseURL(baseURL, match);
} else {
// We cannot pass `match` as the relative URL as regex symbols would be misinterpreted.
const relativeBase = match.startsWith('/') ? '/' : '.';
let prefix = constructURLBasedOnBaseURL(baseURL, relativeBase);
if (prefix !== relativeBase) {
if (match.startsWith('/') && prefix.endsWith('/'))
prefix = prefix.substring(0, prefix.length - 1);
match = prefix + match;
}
}
}
}
if (isString(match))
match = globToRegex(match);

View file

@ -16,7 +16,7 @@
*/
import { test as it, expect } from './pageTest';
import { globToRegex } from '../../packages/playwright-core/lib/utils/isomorphic/urlMatch';
import { globToRegex, urlMatches } from '../../packages/playwright-core/lib/utils/isomorphic/urlMatch';
import vm from 'vm';
it('should work with navigation @smoke', async ({ page, server }) => {
@ -107,6 +107,20 @@ it('should work with glob', async () => {
expect(globToRegex('$^+.\\*()|\\?\\{\\}\\[\\]')).toEqual(/^\$\^\+\.\*\(\)\|\?\{\}\[\]$/);
});
it('should intercept by glob', async function({ page, server, isAndroid }) {
it.skip(isAndroid);
await page.goto(server.EMPTY_PAGE);
await page.route('http://localhos*?/?oo', async route => {
await route.fulfill({
status: 200,
body: 'intercepted',
});
});
const result = await page.evaluate(url => fetch(url).then(r => r.text()), server.PREFIX + '/foo');
expect(result).toBe('intercepted');
});
it('should intercept network activity from worker', async function({ page, server, isAndroid }) {
it.skip(isAndroid);