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
4 changes: 2 additions & 2 deletions packages/playwright-core/src/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -316,7 +316,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
}

async unroute(url: URLMatch, handler?: network.RouteHandlerCallback): Promise<void> {
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();
}

Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-core/src/client/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -458,7 +458,7 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
}

async unroute(url: URLMatch, handler?: RouteHandlerCallback): Promise<void> {
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();
}

Expand Down
6 changes: 6 additions & 0 deletions packages/playwright-core/src/utils/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/library/browsercontext-route.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down
4 changes: 2 additions & 2 deletions tests/page/page-route.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand Down