diff --git a/packages/playwright-core/src/client/browserContext.ts b/packages/playwright-core/src/client/browserContext.ts index 67390a7fdb..7fbb62ddd8 100644 --- a/packages/playwright-core/src/client/browserContext.ts +++ b/packages/playwright-core/src/client/browserContext.ts @@ -28,7 +28,7 @@ import { Events } from './events'; import { TimeoutSettings } from '../common/timeoutSettings'; import { Waiter } from './waiter'; import type { URLMatch, Headers, WaitForEventOptions, BrowserContextOptions, StorageState, LaunchOptions } from './types'; -import { headersObjectToArray, isRegExp, isString } from '../utils'; +import { headersObjectToArray, isRegExp, isString, urlMatchesEqual } from '../utils'; import { mkdirIfNeeded } from '../utils/fileUtils'; import type * as api from '../../types/types'; import type * as structs from '../../types/structs'; @@ -316,7 +316,7 @@ export class BrowserContext extends ChannelOwner } async unroute(url: URLMatch, handler?: network.RouteHandlerCallback): Promise { - this._routes = this._routes.filter(route => route.url !== url || (handler && route.handler !== handler)); + this._routes = this._routes.filter(route => !urlMatchesEqual(route.url, url) || (handler && route.handler !== handler)); await this._updateInterceptionPatterns(); } diff --git a/packages/playwright-core/src/client/page.ts b/packages/playwright-core/src/client/page.ts index fdf6330579..20cb43cf42 100644 --- a/packages/playwright-core/src/client/page.ts +++ b/packages/playwright-core/src/client/page.ts @@ -24,7 +24,7 @@ import { urlMatches } from '../utils/network'; import { TimeoutSettings } from '../common/timeoutSettings'; import type * as channels from '@protocol/channels'; import { parseError, serializeError } from '../protocol/serializers'; -import { assert, headersObjectToArray, isObject, isRegExp, isString, ScopedRace } from '../utils'; +import { assert, headersObjectToArray, isObject, isRegExp, isString, ScopedRace, urlMatchesEqual } from '../utils'; import { mkdirIfNeeded } from '../utils/fileUtils'; import { Accessibility } from './accessibility'; import { Artifact } from './artifact'; @@ -458,7 +458,7 @@ export class Page extends ChannelOwner implements api.Page } async unroute(url: URLMatch, handler?: RouteHandlerCallback): Promise { - this._routes = this._routes.filter(route => route.url !== url || (handler && route.handler !== handler)); + this._routes = this._routes.filter(route => !urlMatchesEqual(route.url, url) || (handler && route.handler !== handler)); await this._updateInterceptionPatterns(); } diff --git a/packages/playwright-core/src/utils/network.ts b/packages/playwright-core/src/utils/network.ts index 08d8029b5e..cb411bf4a3 100644 --- a/packages/playwright-core/src/utils/network.ts +++ b/packages/playwright-core/src/utils/network.ts @@ -109,6 +109,12 @@ export function fetchData(params: HTTPRequestParams, onError?: (params: HTTPRequ }); } +export function urlMatchesEqual(match1: URLMatch, match2: URLMatch) { + if (isRegExp(match1) && isRegExp(match2)) + return match1.source === match2.source && match1.flags === match2.flags; + return match1 === match2; +} + export function urlMatches(baseURL: string | undefined, urlString: string, match: URLMatch | undefined): boolean { if (match === undefined || match === '') return true; diff --git a/tests/library/browsercontext-route.spec.ts b/tests/library/browsercontext-route.spec.ts index 8538957b58..176a40d411 100644 --- a/tests/library/browsercontext-route.spec.ts +++ b/tests/library/browsercontext-route.spec.ts @@ -61,12 +61,12 @@ it('should unroute', async ({ browser, server }) => { intercepted.push(4); route.fallback(); }; - await context.route('**/empty.html', handler4); + await context.route(/empty.html/, handler4); await page.goto(server.EMPTY_PAGE); expect(intercepted).toEqual([4, 3, 2, 1]); intercepted = []; - await context.unroute('**/empty.html', handler4); + await context.unroute(/empty.html/, handler4); await page.goto(server.EMPTY_PAGE); expect(intercepted).toEqual([3, 2, 1]); diff --git a/tests/page/page-route.spec.ts b/tests/page/page-route.spec.ts index f260cdeec5..3c348be70f 100644 --- a/tests/page/page-route.spec.ts +++ b/tests/page/page-route.spec.ts @@ -57,12 +57,12 @@ it('should unroute', async ({ page, server }) => { intercepted.push(4); route.fallback(); }; - await page.route('**/empty.html', handler4); + await page.route(/empty.html/, handler4); await page.goto(server.EMPTY_PAGE); expect(intercepted).toEqual([4, 3, 2, 1]); intercepted = []; - await page.unroute('**/empty.html', handler4); + await page.unroute(/empty.html/, handler4); await page.goto(server.EMPTY_PAGE); expect(intercepted).toEqual([3, 2, 1]);