Skip to content

Commit

Permalink
let nodejs handle the connection pool when agent is passed
Browse files Browse the repository at this point in the history
  • Loading branch information
David Frank committed Apr 30, 2016
1 parent 02def98 commit 7f68577
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function Fetch(url, opts) {
headers.set('user-agent', 'node-fetch/1.0 (+https://github.com/bitinn/node-fetch)');
}

if (!headers.has('connection')) {
if (!headers.has('connection') && !options.agent) {
headers.set('connection', 'close');
}

Expand Down
3 changes: 3 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ function TestServer() {
this.server.on('error', function(err) {
console.log(err.stack);
});
this.server.on('connection', function(socket) {
socket.setTimeout(1500);
});
}

TestServer.prototype.start = function(cb) {
Expand Down
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,20 @@ describe('node-fetch', function() {
});
});

it('should send request with connection keep-alive if agent is provided', function() {
url = base + '/inspect';
opts = {
agent: new http.Agent({
keepAlive: true
})
};
return fetch(url, opts).then(function(res) {
return res.json();
}).then(function(res) {
expect(res.headers['connection']).to.equal('keep-alive');
});
});

it('should ignore unsupported attributes while reading headers', function() {
var FakeHeader = function() {};
// prototypes are ignored
Expand Down

0 comments on commit 7f68577

Please sign in to comment.