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
12 changes: 9 additions & 3 deletions src/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,20 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel,
}

_onRoute(route: network.Route, request: network.Request) {
let handled = false;
for (const routeHandler of this._routes) {
if (routeHandler.matches(request.url())) {
routeHandler.handle(route, request);
return;
handled = true;
break;
}
}
// it can race with BrowserContext.close() which then throws since its closed
route.continue().catch(() => {});
if (!handled) {
// it can race with BrowserContext.close() which then throws since its closed
route.continue().catch(() => {});
} else {
this._routes = this._routes.filter(route => !route.expired());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mxschmitt Hi, I think setNetworkInterceptionEnabled enabled: false) can be called here when remaining handlers doesn't exist.

}
}

async _onBinding(bindingCall: BindingCall) {
Expand Down
9 changes: 6 additions & 3 deletions src/client/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,15 +626,18 @@ export class RouteHandler {
this.handler = handler;
}

public expired(): boolean {
return !!this._times && this.handledCount >= this._times;
}

public matches(requestURL: string): boolean {
if (this._times && this.handledCount >= this._times)
return false;
return urlMatches(this._baseURL, requestURL, this.url);
}

public handle(route: Route, request: Request) {
this.handler(route, request);
this.handledCount++;
if (this._times)
this.handledCount++;
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/client/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,18 @@ export class Page extends ChannelOwner<channels.PageChannel, channels.PageInitia
}

private _onRoute(route: Route, request: Request) {
let handled = false;
for (const routeHandler of this._routes) {
if (routeHandler.matches(request.url())) {
routeHandler.handle(route, request);
return;
handled = true;
break;
}
}
this._browserContext._onRoute(route, request);
if (!handled)
this._browserContext._onRoute(route, request);
else
this._routes = this._routes.filter(route => !route.expired());
}

async _onBinding(bindingCall: BindingCall) {
Expand Down