Skip to content

Commit ae6f48c

Browse files
authored
fix(route): match against updated url while chaining (#15112)
1 parent eba2bdf commit ae6f48c

File tree

5 files changed

+47
-3
lines changed

5 files changed

+47
-3
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,10 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
145145
}
146146

147147
async _onRoute(route: network.Route, request: network.Request) {
148-
const routeHandlers = this._routes.filter(r => r.matches(request.url()));
148+
const routeHandlers = this._routes.slice();
149149
for (const routeHandler of routeHandlers) {
150+
if (!routeHandler.matches(request.url()))
151+
continue;
150152
if (routeHandler.willExpire())
151153
this._routes.splice(this._routes.indexOf(routeHandler), 1);
152154
const handled = await routeHandler.handle(route, request);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,10 @@ export class Page extends ChannelOwner<channels.PageChannel> implements api.Page
179179
}
180180

181181
private async _onRoute(route: Route, request: Request) {
182-
const routeHandlers = this._routes.filter(r => r.matches(request.url()));
182+
const routeHandlers = this._routes.slice();
183183
for (const routeHandler of routeHandlers) {
184+
if (!routeHandler.matches(request.url()))
185+
continue;
184186
if (routeHandler.willExpire())
185187
this._routes.splice(this._routes.indexOf(routeHandler), 1);
186188
const handled = await routeHandler.handle(route, request);

tests/library/browsercontext-route.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,26 @@ it('should chain fallback', async ({ context, page, server }) => {
268268
expect(intercepted).toEqual([3, 2, 1]);
269269
});
270270

271+
it('should chain fallback w/ dynamic URL', async ({ context, page, server }) => {
272+
const intercepted = [];
273+
await context.route('**/bar', route => {
274+
intercepted.push(1);
275+
route.fallback({ url: server.EMPTY_PAGE });
276+
});
277+
await context.route('**/foo', route => {
278+
intercepted.push(2);
279+
route.fallback({ url: 'http://localhost/bar' });
280+
});
281+
282+
await context.route('**/empty.html', route => {
283+
intercepted.push(3);
284+
route.fallback({ url: 'http://localhost/foo' });
285+
});
286+
287+
await page.goto(server.EMPTY_PAGE);
288+
expect(intercepted).toEqual([3, 2, 1]);
289+
});
290+
271291
it('should not chain fulfill', async ({ context, page, server }) => {
272292
let failed = false;
273293
await context.route('**/empty.html', route => {

tests/page/page-request-fallback.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ it('should override request url', async ({ page, server }) => {
205205
const request = server.waitForRequest('/global-var.html');
206206

207207
let url: string;
208-
await page.route('**/foo', route => {
208+
await page.route('**/global-var.html', route => {
209209
url = route.request().url();
210210
route.continue();
211211
});

tests/page/page-route.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,26 @@ it('should not work with redirects', async ({ page, server }) => {
321321
expect(chain[i].redirectedTo()).toBe(i ? chain[i - 1] : null);
322322
});
323323

324+
it('should chain fallback w/ dynamic URL', async ({ page, server }) => {
325+
const intercepted = [];
326+
await page.route('**/bar', route => {
327+
intercepted.push(1);
328+
route.fallback({ url: server.EMPTY_PAGE });
329+
});
330+
await page.route('**/foo', route => {
331+
intercepted.push(2);
332+
route.fallback({ url: 'http://localhost/bar' });
333+
});
334+
335+
await page.route('**/empty.html', route => {
336+
intercepted.push(3);
337+
route.fallback({ url: 'http://localhost/foo' });
338+
});
339+
340+
await page.goto(server.EMPTY_PAGE);
341+
expect(intercepted).toEqual([3, 2, 1]);
342+
});
343+
324344
it('should work with redirects for subresources', async ({ page, server }) => {
325345
const intercepted = [];
326346
await page.route('**/*', route => {

0 commit comments

Comments
 (0)