Skip to content

Commit

Permalink
feat: set fetch timeout to 300 seconds (#1386)
Browse files Browse the repository at this point in the history
* Set fetch timeout to 300 seconds

* Update lib/fetch/index.js

Co-authored-by: Michaël Zasso <targos@protonmail.com>

* Add fetch timeout tests

* Fix formatting

* Skip fetch timeout tests

* skip tests

* fixup

* fixup

Co-authored-by: Matteo Collina <matteo.collina@gmail.com>
Co-authored-by: Michaël Zasso <targos@protonmail.com>
Co-authored-by: Robert Nagy <ronagy@icloud.com>
  • Loading branch information
4 people committed Apr 27, 2022
1 parent 0608be8 commit 2c41ce1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/fetch/index.js
Expand Up @@ -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,
Expand Down
16 changes: 16 additions & 0 deletions test/node-fetch/main.js
Expand Up @@ -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)
// })
})
16 changes: 16 additions & 0 deletions test/node-fetch/utils/server.js
Expand Up @@ -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')
Expand Down

0 comments on commit 2c41ce1

Please sign in to comment.