Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(fetch): be compatible with a 0 timeout (#9071)

Max Schmitt449a593050ae
diff --git a/src/server/fetch.ts b/src/server/fetch.ts
index e7f255e7c7..7050e92984 100644
--- a/src/server/fetch.ts
+++ b/src/server/fetch.ts
@@ -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();
diff --git a/tests/browsercontext-fetch.spec.ts b/tests/browsercontext-fetch.spec.ts
index a76bfebf39..b721a347fd 100644
--- a/tests/browsercontext-fetch.spec.ts
+++ b/tests/browsercontext-fetch.spec.ts
@@ -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) => {