mirror of
https://github.com/microsoft/playwright.git
synced 2024-12-12 11:50:22 +03:00
fix(fetch): be compatible with a 0 timeout (#9071)
This commit is contained in:
parent
c66d72bddb
commit
449a593050
@ -111,7 +111,7 @@ export abstract class FetchRequest extends SdkObject {
|
||||
}
|
||||
|
||||
const timeout = defaults.timeoutSettings.timeout(params);
|
||||
const deadline = monotonicTime() + timeout;
|
||||
const deadline = timeout && (monotonicTime() + timeout);
|
||||
|
||||
const options: https.RequestOptions & { maxRedirects: number, deadline: number } = {
|
||||
method,
|
||||
@ -279,16 +279,20 @@ export abstract class FetchRequest extends SdkObject {
|
||||
body.on('error',reject);
|
||||
});
|
||||
request.on('error', reject);
|
||||
const rejectOnTimeout = () => {
|
||||
reject(new Error(`Request timed out after ${options.timeout}ms`));
|
||||
request.abort();
|
||||
};
|
||||
const remaining = options.deadline - monotonicTime();
|
||||
if (remaining <= 0) {
|
||||
rejectOnTimeout();
|
||||
return;
|
||||
|
||||
if (options.deadline) {
|
||||
const rejectOnTimeout = () => {
|
||||
reject(new Error(`Request timed out after ${options.timeout}ms`));
|
||||
request.abort();
|
||||
};
|
||||
const remaining = options.deadline - monotonicTime();
|
||||
if (remaining <= 0) {
|
||||
rejectOnTimeout();
|
||||
return;
|
||||
}
|
||||
request.setTimeout(remaining, rejectOnTimeout);
|
||||
}
|
||||
request.setTimeout(remaining, rejectOnTimeout);
|
||||
|
||||
if (postData)
|
||||
request.write(postData);
|
||||
request.end();
|
||||
|
@ -659,6 +659,23 @@ it('should support timeout option', async function({context, server}) {
|
||||
expect(error.message).toContain(`Request timed out after 10ms`);
|
||||
});
|
||||
|
||||
it('should support a timeout of 0', async function({context, server}) {
|
||||
server.setRoute('/slow', (req, res) => {
|
||||
res.writeHead(200, {
|
||||
'content-length': 4,
|
||||
'content-type': 'text/html',
|
||||
});
|
||||
setTimeout(() => {
|
||||
res.end('done');
|
||||
}, 50);
|
||||
});
|
||||
|
||||
const response = await context._request.get(server.PREFIX + '/slow', {
|
||||
timeout: 0,
|
||||
});
|
||||
expect(await response.text()).toBe('done');
|
||||
});
|
||||
|
||||
it('should respect timeout after redirects', async function({context, server}) {
|
||||
server.setRedirect('/redirect', '/slow');
|
||||
server.setRoute('/slow', (req, res) => {
|
||||
|
Loading…
Reference in New Issue
Block a user