Skip to content

Commit

Permalink
Pass custom timeout to subsequent requests on redirect (#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitinn committed Apr 26, 2019
1 parent cfc8e5b commit 49d7760
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/index.js
Expand Up @@ -153,6 +153,7 @@ export default function fetch(url, opts) {
method: request.method,
body: request.body,
signal: request.signal,
timeout: request.timeout
};

// HTTP-redirect fetch step 9
Expand Down
8 changes: 8 additions & 0 deletions test/server.js
Expand Up @@ -275,6 +275,14 @@ export default class TestServer {
}, 1000);
}

if (p === '/redirect/slow-chain') {
res.statusCode = 301;
res.setHeader('Location', '/redirect/slow');
setTimeout(function() {
res.end();
}, 100);
}

if (p === '/redirect/slow-stream') {
res.statusCode = 301;
res.setHeader('Location', '/slow');
Expand Down
11 changes: 11 additions & 0 deletions test/test.js
Expand Up @@ -799,6 +799,17 @@ describe('node-fetch', () => {
});
});

it('should allow custom timeout on redirected requests', function() {
this.timeout(2000);
const url = `${base}redirect/slow-chain`;
const opts = {
timeout: 200
};
return expect(fetch(url, opts)).to.eventually.be.rejected
.and.be.an.instanceOf(FetchError)
.and.have.property('type', 'request-timeout');
});

it('should clear internal timeout on fetch response', function (done) {
this.timeout(2000);
spawn('node', ['-e', `require('./')('${base}hello', { timeout: 10000 })`])
Expand Down

0 comments on commit 49d7760

Please sign in to comment.