fix: throw in route.continue if it is called twice (#11701)

This commit is contained in:
Yury Semikhatsky 2022-01-27 14:58:02 -08:00 committed by GitHub
parent 29a84b2df8
commit d305a2ab3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 9 deletions

View file

@ -238,14 +238,12 @@ export class Route extends SdkObject {
} }
async abort(errorCode: string = 'failed') { async abort(errorCode: string = 'failed') {
assert(!this._handled, 'Route is already handled!'); this._startHandling();
this._handled = true;
await this._delegate.abort(errorCode); await this._delegate.abort(errorCode);
} }
async fulfill(overrides: { status?: number, headers?: types.HeadersArray, body?: string, isBase64?: boolean, useInterceptedResponseBody?: boolean, fetchResponseUid?: string }) { async fulfill(overrides: { status?: number, headers?: types.HeadersArray, body?: string, isBase64?: boolean, useInterceptedResponseBody?: boolean, fetchResponseUid?: string }) {
assert(!this._handled, 'Route is already handled!'); this._startHandling();
this._handled = true;
let body = overrides.body; let body = overrides.body;
let isBase64 = overrides.isBase64 || false; let isBase64 = overrides.isBase64 || false;
if (body === undefined) { if (body === undefined) {
@ -269,7 +267,7 @@ export class Route extends SdkObject {
} }
async continue(overrides: types.NormalizedContinueOverrides = {}) { async continue(overrides: types.NormalizedContinueOverrides = {}) {
assert(!this._handled, 'Route is already handled!'); this._startHandling();
if (overrides.url) { if (overrides.url) {
const newUrl = new URL(overrides.url); const newUrl = new URL(overrides.url);
const oldUrl = new URL(this._request.url()); const oldUrl = new URL(this._request.url());
@ -278,6 +276,11 @@ export class Route extends SdkObject {
} }
await this._delegate.continue(this._request, overrides); await this._delegate.continue(this._request, overrides);
} }
private _startHandling() {
assert(!this._handled, 'Route is already handled!');
this._handled = true;
}
} }
export type RouteHandler = (route: Route, request: Request) => void; export type RouteHandler = (route: Route, request: Request) => void;

View file

@ -63,16 +63,18 @@ it('should override request url', async ({ page, server }) => {
}); });
it('should not allow changing protocol when overriding url', async ({ page, server }) => { it('should not allow changing protocol when overriding url', async ({ page, server }) => {
let error: Error | undefined; let resolve;
const errorPromise = new Promise<Error|null>(f => resolve = f);
await page.route('**/*', async route => { await page.route('**/*', async route => {
try { try {
await route.continue({ url: 'file:///tmp/foo' }); await route.continue({ url: 'file:///tmp/foo' });
resolve(null);
} catch (e) { } catch (e) {
error = e; resolve(e);
await route.continue();
} }
}); });
await page.goto(server.EMPTY_PAGE); page.goto(server.EMPTY_PAGE).catch(() => {});
const error = await errorPromise;
expect(error).toBeTruthy(); expect(error).toBeTruthy();
expect(error.message).toContain('New URL must have same protocol as overridden URL'); expect(error.message).toContain('New URL must have same protocol as overridden URL');
}); });

View file

@ -15,6 +15,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { Route } from 'playwright-core';
import { test as it, expect } from './pageTest'; import { test as it, expect } from './pageTest';
it('should intercept #smoke', async ({ page, server }) => { it('should intercept #smoke', async ({ page, server }) => {
@ -710,3 +711,16 @@ it('should contain raw response header after fulfill', async ({ page, server })
const headers = await response.allHeaders(); const headers = await response.allHeaders();
expect(headers['content-type']).toBeTruthy(); expect(headers['content-type']).toBeTruthy();
}); });
for (const method of ['fulfill', 'continue', 'abort'] as const) {
it(`route.${method} should throw if called twice`, async ({ page, server }) => {
const routePromise = new Promise<Route>(async resove => {
await page.route('**/*', resove);
});
page.goto(server.PREFIX + '/empty.html').catch(() => {});
const route = await routePromise;
await route[method]();
const e = await route[method]().catch(e => e);
expect(e.message).toContain('Route is already handled!');
});
}