From 2c41ce19f7f8596f0334167912c0eec7b21247d2 Mon Sep 17 00:00:00 2001 From: Sergiy Kyrylkov Date: Wed, 27 Apr 2022 14:15:18 +0200 Subject: [PATCH] feat: set fetch timeout to 300 seconds (#1386) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Set fetch timeout to 300 seconds * Update lib/fetch/index.js Co-authored-by: Michaël Zasso * Add fetch timeout tests * Fix formatting * Skip fetch timeout tests * skip tests * fixup * fixup Co-authored-by: Matteo Collina Co-authored-by: Michaël Zasso Co-authored-by: Robert Nagy --- lib/fetch/index.js | 4 +++- test/node-fetch/main.js | 16 ++++++++++++++++ test/node-fetch/utils/server.js | 16 ++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/fetch/index.js b/lib/fetch/index.js index c4ec1faf5b3..ec1503c159e 100644 --- a/lib/fetch/index.js +++ b/lib/fetch/index.js @@ -1920,7 +1920,9 @@ async function httpNetworkFetch ( method: request.method, body: fetchParams.controller.dispatcher[kIsMockActive] ? request.body && request.body.source : body, headers: [...request.headersList].flat(), - maxRedirections: 0 + maxRedirections: 0, + bodyTimeout: 300_000, + headersTimeout: 300_000 }, { body: null, diff --git a/test/node-fetch/main.js b/test/node-fetch/main.js index 77a61a29454..a6d1501f40d 100644 --- a/test/node-fetch/main.js +++ b/test/node-fetch/main.js @@ -1641,4 +1641,20 @@ describe('node-fetch', () => { expect(res.ok).to.be.false }) }) + + // it('should not time out waiting for a response 60 seconds', function () { + // this.timeout(65_000) + // return fetch(`${base}timeout60s`).then(res => { + // expect(res.status).to.equal(200) + // expect(res.ok).to.be.true + // return res.text().then(result => { + // expect(result).to.equal('text') + // }) + // }) + // }) + + // it('should time out waiting for more than 300 seconds', function () { + // this.timeout(305_000) + // return expect(fetch(`${base}timeout300s`)).to.eventually.be.rejectedWith(TypeError) + // }) }) diff --git a/test/node-fetch/utils/server.js b/test/node-fetch/utils/server.js index a2817a2d30b..97b551582bd 100644 --- a/test/node-fetch/utils/server.js +++ b/test/node-fetch/utils/server.js @@ -227,6 +227,22 @@ module.exports = class TestServer { }, 1000) } + if (p === '/timeout60s') { + setTimeout(() => { + res.statusCode = 200 + res.setHeader('Content-Type', 'text/plain') + res.end('text') + }, 60_000) + } + + if (p === '/timeout300s') { + setTimeout(() => { + res.statusCode = 200 + res.setHeader('Content-Type', 'text/plain') + res.end('text') + }, 300_000) + } + if (p === '/slow') { res.statusCode = 200 res.setHeader('Content-Type', 'text/plain')