Skip to content

Commit 247fe62

Browse files
authored
fix: hostname match is case insensitive (#37018)
1 parent 353f3df commit 247fe62

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,12 @@ function resolveGlobBase(baseURL: string | undefined, match: string): string {
148148
const newSuffix = mapToken(token.substring(questionIndex), `?$_${index}_$`);
149149
return newPrefix + newSuffix;
150150
}).join('/');
151-
let resolved = constructURLBasedOnBaseURL(baseURL, relativePath);
152-
for (const [token, original] of tokenMap)
153-
resolved = resolved.replace(token, original);
151+
const result = resolveBaseURL(baseURL, relativePath);
152+
let resolved = result.resolved;
153+
for (const [token, original] of tokenMap) {
154+
const normalize = result.caseInsensitivePart?.includes(token);
155+
resolved = resolved.replace(token, normalize ? original.toLowerCase() : original);
156+
}
154157
match = resolved;
155158
}
156159
return match;
@@ -166,8 +169,20 @@ function parseURL(url: string): URL | null {
166169

167170
export function constructURLBasedOnBaseURL(baseURL: string | undefined, givenURL: string): string {
168171
try {
169-
return (new URL(givenURL, baseURL)).toString();
172+
return resolveBaseURL(baseURL, givenURL).resolved;
170173
} catch (e) {
171174
return givenURL;
172175
}
173176
}
177+
178+
function resolveBaseURL(baseURL: string | undefined, givenURL: string) {
179+
try {
180+
const url = new URL(givenURL, baseURL);
181+
const resolved = url.toString();
182+
// Schema and domain are case-insensitive.
183+
const caseInsensitivePrefix = url.origin;
184+
return { resolved, caseInsensitivePart: caseInsensitivePrefix };
185+
} catch (e) {
186+
return { resolved: givenURL };
187+
}
188+
}

tests/page/interception.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ it('should work with glob', async () => {
116116
expect(urlMatches('http://playwright.dev', 'http://playwright.dev/?x=y', '?x=y')).toBeTruthy();
117117
expect(urlMatches('http://playwright.dev/foo/', 'http://playwright.dev/foo/bar?x=y', './bar?x=y')).toBeTruthy();
118118

119+
// Case insensitive matching
120+
expect(urlMatches(undefined, 'https://playwright.dev/fooBAR', 'HtTpS://pLaYwRiGhT.dEv/fooBAR')).toBeTruthy();
121+
expect(urlMatches('http://ignored', 'https://playwright.dev/fooBAR', 'HtTpS://pLaYwRiGhT.dEv/fooBAR')).toBeTruthy();
122+
// Path and search query are case-sensitive
123+
expect(urlMatches(undefined, 'https://playwright.dev/foobar', 'https://playwright.dev/fooBAR')).toBeFalsy();
124+
expect(urlMatches(undefined, 'https://playwright.dev/foobar?a=b', 'https://playwright.dev/foobar?A=B')).toBeFalsy();
125+
119126
// This is not supported, we treat ? as a query separator.
120127
expect(globToRegex('http://localhost:8080/?imple/path.js').test('http://localhost:8080/Simple/path.js')).toBeFalsy();
121128
expect(urlMatches(undefined, 'http://playwright.dev/', 'http://playwright.?ev')).toBeFalsy();

0 commit comments

Comments
 (0)