feat(route): accept timeout to fetch (#22475)

Fixes: #22474
This commit is contained in:
☃ Elliot Shepherd 2023-04-21 01:41:33 +10:00 committed by GitHub
parent 0c70f6900e
commit 106fa45f50
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 1 deletions

View File

@ -503,6 +503,12 @@ If set changes the request URL. New URL must have same protocol as original one.
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.
### option: Route.fetch.timeout
* since: v1.33
- `timeout` <[float]>
Request timeout in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
### option: Route.fetch.method
* since: v1.29
- `method` <[string]>

View File

@ -320,7 +320,7 @@ export class Route extends ChannelOwner<channels.RouteChannel> implements api.Ro
this._reportHandled(true);
}
async fetch(options: FallbackOverrides & { maxRedirects?: number } = {}): Promise<APIResponse> {
async fetch(options: FallbackOverrides & { maxRedirects?: number, timeout?: 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

@ -18206,6 +18206,11 @@ export interface Route {
*/
postData?: string|Buffer|Serializable;
/**
* Request timeout in milliseconds. Defaults to `30000` (30 seconds). Pass `0` to disable timeout.
*/
timeout?: number;
/**
* If set changes the request URL. New URL must have same protocol as original one.
*/

View File

@ -206,6 +206,24 @@ it('should fulfill intercepted response using alias', async ({ page, server, isE
expect(response.headers()['content-type']).toContain('text/html');
});
it('should support timeout option in route.fetch', 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');
server.setRoute('/slow', (req, res) => {
res.writeHead(200, {
'content-length': 4096,
'content-type': 'text/html',
});
});
await page.route('**/*', async route => {
const error = await route.fetch({ timeout: 1000 }).catch(e => e);
expect(error.message).toContain(`Request timed out after 1000ms`);
});
const error = await page.goto(server.PREFIX + '/slow', { timeout: 2000 }).catch(e => e);
expect(error.message).toContain(`Timeout 2000ms exceeded`);
});
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');