feat(route): add maxRedirects option to Route.fetch (#20567)

References #20501.
This commit is contained in:
Dmitry Gozman 2023-02-01 14:43:21 -08:00 committed by GitHub
parent 0edf77fe91
commit 6ad4687f4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 1 deletions

View file

@ -488,6 +488,9 @@ await page.RouteAsync("https://dog.ceo/api/breeds/list/all", async route =>
If set changes the request URL. New URL must have same protocol as original one.
### option: Route.fetch.maxRedirects = %%-js-python-csharp-fetch-option-maxredirects-%%
* since: v1.31
### option: Route.fetch.method
* since: v1.29
- `method` <[string]>

View file

@ -326,7 +326,7 @@ export class Route extends ChannelOwner<channels.RouteChannel> implements api.Ro
this._reportHandled(true);
}
async fetch(options: FallbackOverrides = {}) {
async fetch(options: FallbackOverrides & { maxRedirects?: number } = {}): Promise<APIResponse> {
return await this._wrapApiCall(async () => {
const context = this.request()._context();
return context.request._innerFetch({ request: this.request(), data: options.postData, ...options });

View file

@ -17924,6 +17924,12 @@ export interface Route {
*/
headers?: { [key: string]: string; };
/**
* Maximum number of request redirects that will be followed automatically. An error will be thrown if the number is
* exceeded. Defaults to `20`. Pass `0` to not follow redirects.
*/
maxRedirects?: number;
/**
* If set changes the request method (e.g. GET or POST).
*/

View file

@ -206,6 +206,21 @@ it('should fulfill intercepted response using alias', async ({ page, server, isE
expect(response.headers()['content-type']).toContain('text/html');
});
it('should not follow redirects when maxRedirects is set to 0 in route.fetch', async ({ page, server, isAndroid, isElectron }) => {
it.fixme(isElectron, 'error: Browser context management is not supported.');
it.skip(isAndroid, 'The internal Android localhost (10.0.0.2) != the localhost on the host');
server.setRedirect('/foo', '/empty.html');
await page.route('**/*', async route => {
const response = await route.fetch({ maxRedirects: 0 });
expect(response.headers()['location']).toBe('/empty.html');
expect(response.status()).toBe(302);
await route.fulfill({ body: 'hello' });
});
await page.goto(server.PREFIX + '/foo');
expect(await page.content()).toContain('hello');
});
it('should intercept with url override', async ({ page, server, isElectron, isAndroid }) => {
it.fixme(isElectron, 'error: Browser context management is not supported.');
it.skip(isAndroid, 'The internal Android localhost (10.0.0.2) != the localhost on the host');