fix(route): match against updated url while chaining (#15112)

This commit is contained in:
Pavel Feldman 2022-06-24 10:48:16 -07:00 committed by GitHub
parent eba2bdffb9
commit ae6f48c4b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 3 deletions

View File

@ -145,8 +145,10 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
}
async _onRoute(route: network.Route, request: network.Request) {
const routeHandlers = this._routes.filter(r => r.matches(request.url()));
const routeHandlers = this._routes.slice();
for (const routeHandler of routeHandlers) {
if (!routeHandler.matches(request.url()))
continue;
if (routeHandler.willExpire())
this._routes.splice(this._routes.indexOf(routeHandler), 1);
const handled = await routeHandler.handle(route, request);

View File

@ -179,8 +179,10 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
}
private async _onRoute(route: Route, request: Request) {
const routeHandlers = this._routes.filter(r => r.matches(request.url()));
const routeHandlers = this._routes.slice();
for (const routeHandler of routeHandlers) {
if (!routeHandler.matches(request.url()))
continue;
if (routeHandler.willExpire())
this._routes.splice(this._routes.indexOf(routeHandler), 1);
const handled = await routeHandler.handle(route, request);

View File

@ -268,6 +268,26 @@ it('should chain fallback', async ({ context, page, server }) => {
expect(intercepted).toEqual([3, 2, 1]);
});
it('should chain fallback w/ dynamic URL', async ({ context, page, server }) => {
const intercepted = [];
await context.route('**/bar', route => {
intercepted.push(1);
route.fallback({ url: server.EMPTY_PAGE });
});
await context.route('**/foo', route => {
intercepted.push(2);
route.fallback({ url: 'http://localhost/bar' });
});
await context.route('**/empty.html', route => {
intercepted.push(3);
route.fallback({ url: 'http://localhost/foo' });
});
await page.goto(server.EMPTY_PAGE);
expect(intercepted).toEqual([3, 2, 1]);
});
it('should not chain fulfill', async ({ context, page, server }) => {
let failed = false;
await context.route('**/empty.html', route => {

View File

@ -205,7 +205,7 @@ it('should override request url', async ({ page, server }) => {
const request = server.waitForRequest('/global-var.html');
let url: string;
await page.route('**/foo', route => {
await page.route('**/global-var.html', route => {
url = route.request().url();
route.continue();
});

View File

@ -321,6 +321,26 @@ it('should not work with redirects', async ({ page, server }) => {
expect(chain[i].redirectedTo()).toBe(i ? chain[i - 1] : null);
});
it('should chain fallback w/ dynamic URL', async ({ page, server }) => {
const intercepted = [];
await page.route('**/bar', route => {
intercepted.push(1);
route.fallback({ url: server.EMPTY_PAGE });
});
await page.route('**/foo', route => {
intercepted.push(2);
route.fallback({ url: 'http://localhost/bar' });
});
await page.route('**/empty.html', route => {
intercepted.push(3);
route.fallback({ url: 'http://localhost/foo' });
});
await page.goto(server.EMPTY_PAGE);
expect(intercepted).toEqual([3, 2, 1]);
});
it('should work with redirects for subresources', async ({ page, server }) => {
const intercepted = [];
await page.route('**/*', route => {