Skip to content

Commit be7984b

Browse files
authored
fix(unroute): do not compare regexp instances (#23101)
References #23092.
1 parent f1e0aed commit be7984b

File tree

5 files changed

+14
-8
lines changed

5 files changed

+14
-8
lines changed

packages/playwright-core/src/client/browserContext.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { Events } from './events';
2828
import { TimeoutSettings } from '../common/timeoutSettings';
2929
import { Waiter } from './waiter';
3030
import type { URLMatch, Headers, WaitForEventOptions, BrowserContextOptions, StorageState, LaunchOptions } from './types';
31-
import { headersObjectToArray, isRegExp, isString } from '../utils';
31+
import { headersObjectToArray, isRegExp, isString, urlMatchesEqual } from '../utils';
3232
import { mkdirIfNeeded } from '../utils/fileUtils';
3333
import type * as api from '../../types/types';
3434
import type * as structs from '../../types/structs';
@@ -316,7 +316,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
316316
}
317317

318318
async unroute(url: URLMatch, handler?: network.RouteHandlerCallback): Promise<void> {
319-
this._routes = this._routes.filter(route => route.url !== url || (handler && route.handler !== handler));
319+
this._routes = this._routes.filter(route => !urlMatchesEqual(route.url, url) || (handler && route.handler !== handler));
320320
await this._updateInterceptionPatterns();
321321
}
322322

packages/playwright-core/src/client/page.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { urlMatches } from '../utils/network';
2424
import { TimeoutSettings } from '../common/timeoutSettings';
2525
import type * as channels from '@protocol/channels';
2626
import { parseError, serializeError } from '../protocol/serializers';
27-
import { assert, headersObjectToArray, isObject, isRegExp, isString, ScopedRace } from '../utils';
27+
import { assert, headersObjectToArray, isObject, isRegExp, isString, ScopedRace, urlMatchesEqual } from '../utils';
2828
import { mkdirIfNeeded } from '../utils/fileUtils';
2929
import { Accessibility } from './accessibility';
3030
import { Artifact } from './artifact';
@@ -458,7 +458,7 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
458458
}
459459

460460
async unroute(url: URLMatch, handler?: RouteHandlerCallback): Promise<void> {
461-
this._routes = this._routes.filter(route => route.url !== url || (handler && route.handler !== handler));
461+
this._routes = this._routes.filter(route => !urlMatchesEqual(route.url, url) || (handler && route.handler !== handler));
462462
await this._updateInterceptionPatterns();
463463
}
464464

packages/playwright-core/src/utils/network.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ export function fetchData(params: HTTPRequestParams, onError?: (params: HTTPRequ
109109
});
110110
}
111111

112+
export function urlMatchesEqual(match1: URLMatch, match2: URLMatch) {
113+
if (isRegExp(match1) && isRegExp(match2))
114+
return match1.source === match2.source && match1.flags === match2.flags;
115+
return match1 === match2;
116+
}
117+
112118
export function urlMatches(baseURL: string | undefined, urlString: string, match: URLMatch | undefined): boolean {
113119
if (match === undefined || match === '')
114120
return true;

tests/library/browsercontext-route.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ it('should unroute', async ({ browser, server }) => {
6161
intercepted.push(4);
6262
route.fallback();
6363
};
64-
await context.route('**/empty.html', handler4);
64+
await context.route(/empty.html/, handler4);
6565
await page.goto(server.EMPTY_PAGE);
6666
expect(intercepted).toEqual([4, 3, 2, 1]);
6767

6868
intercepted = [];
69-
await context.unroute('**/empty.html', handler4);
69+
await context.unroute(/empty.html/, handler4);
7070
await page.goto(server.EMPTY_PAGE);
7171
expect(intercepted).toEqual([3, 2, 1]);
7272

tests/page/page-route.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ it('should unroute', async ({ page, server }) => {
5757
intercepted.push(4);
5858
route.fallback();
5959
};
60-
await page.route('**/empty.html', handler4);
60+
await page.route(/empty.html/, handler4);
6161
await page.goto(server.EMPTY_PAGE);
6262
expect(intercepted).toEqual([4, 3, 2, 1]);
6363

6464
intercepted = [];
65-
await page.unroute('**/empty.html', handler4);
65+
await page.unroute(/empty.html/, handler4);
6666
await page.goto(server.EMPTY_PAGE);
6767
expect(intercepted).toEqual([3, 2, 1]);
6868

0 commit comments

Comments
 (0)