fix(unroute): do not compare regexp instances (#23101)

References #23092.
This commit is contained in:
Dmitry Gozman 2023-05-17 16:27:32 -07:00 committed by GitHub
parent f1e0aed03c
commit be7984bdc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 14 additions and 8 deletions

View File

@ -28,7 +28,7 @@ import { Events } from './events';
import { TimeoutSettings } from '../common/timeoutSettings'; import { TimeoutSettings } from '../common/timeoutSettings';
import { Waiter } from './waiter'; import { Waiter } from './waiter';
import type { URLMatch, Headers, WaitForEventOptions, BrowserContextOptions, StorageState, LaunchOptions } from './types'; 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 { mkdirIfNeeded } from '../utils/fileUtils';
import type * as api from '../../types/types'; import type * as api from '../../types/types';
import type * as structs from '../../types/structs'; import type * as structs from '../../types/structs';
@ -316,7 +316,7 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
} }
async unroute(url: URLMatch, handler?: network.RouteHandlerCallback): Promise<void> { 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(); await this._updateInterceptionPatterns();
} }

View File

@ -24,7 +24,7 @@ import { urlMatches } from '../utils/network';
import { TimeoutSettings } from '../common/timeoutSettings'; import { TimeoutSettings } from '../common/timeoutSettings';
import type * as channels from '@protocol/channels'; import type * as channels from '@protocol/channels';
import { parseError, serializeError } from '../protocol/serializers'; 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 { mkdirIfNeeded } from '../utils/fileUtils';
import { Accessibility } from './accessibility'; import { Accessibility } from './accessibility';
import { Artifact } from './artifact'; import { Artifact } from './artifact';
@ -458,7 +458,7 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
} }
async unroute(url: URLMatch, handler?: RouteHandlerCallback): Promise<void> { 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(); await this._updateInterceptionPatterns();
} }

View File

@ -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 { export function urlMatches(baseURL: string | undefined, urlString: string, match: URLMatch | undefined): boolean {
if (match === undefined || match === '') if (match === undefined || match === '')
return true; return true;

View File

@ -61,12 +61,12 @@ it('should unroute', async ({ browser, server }) => {
intercepted.push(4); intercepted.push(4);
route.fallback(); route.fallback();
}; };
await context.route('**/empty.html', handler4); await context.route(/empty.html/, handler4);
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
expect(intercepted).toEqual([4, 3, 2, 1]); expect(intercepted).toEqual([4, 3, 2, 1]);
intercepted = []; intercepted = [];
await context.unroute('**/empty.html', handler4); await context.unroute(/empty.html/, handler4);
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
expect(intercepted).toEqual([3, 2, 1]); expect(intercepted).toEqual([3, 2, 1]);

View File

@ -57,12 +57,12 @@ it('should unroute', async ({ page, server }) => {
intercepted.push(4); intercepted.push(4);
route.fallback(); route.fallback();
}; };
await page.route('**/empty.html', handler4); await page.route(/empty.html/, handler4);
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
expect(intercepted).toEqual([4, 3, 2, 1]); expect(intercepted).toEqual([4, 3, 2, 1]);
intercepted = []; intercepted = [];
await page.unroute('**/empty.html', handler4); await page.unroute(/empty.html/, handler4);
await page.goto(server.EMPTY_PAGE); await page.goto(server.EMPTY_PAGE);
expect(intercepted).toEqual([3, 2, 1]); expect(intercepted).toEqual([3, 2, 1]);