123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- var http = require('http'),
- helpers = require('./helpers'),
- should = require('should');
- var port = 54321;
- describe('request headers', function() {
- var needle,
- server,
- existing_sockets,
- original_defaultMaxSockets;
- before(function(done) {
- setTimeout(function() {
- existing_sockets = get_active_sockets().length;
- server = helpers.server({ port: port }, done);
- }, 100);
- })
- after(function(done) {
- server.close(done);
- })
- function send_request(opts, cb) {
- needle.get('http://localhost:' + port, opts, cb);
- }
- function get_active_sockets() {
- var handles = process._getActiveHandles();
- return handles.filter(function(el) {
- if (el.constructor.name.toString() == 'Socket') {
- return el.destroyed !== true;
- }
- })
- }
- describe('old node versions (<0.11.4) with persistent keep-alive connections', function() {
- // emulate old node behaviour
- before(function() {
- delete require.cache[require.resolve('..')] // in case it was already loaded
- original_defaultMaxSockets = http.Agent.defaultMaxSockets;
- http.Agent.defaultMaxSockets = 5;
- needle = require('..');
- })
- after(function() {
- http.Agent.defaultMaxSockets = original_defaultMaxSockets;
- delete require.cache[require.resolve('..')]
- })
- describe('default options', function() {
- it('sends a Connection: close header', function(done) {
- send_request({}, function(err, resp) {
- resp.body.headers['connection'].should.eql('close');
- done();
- })
- })
- it('no open sockets remain after request', function(done) {
- send_request({}, function(err, resp) {
- setTimeout(function() {
- get_active_sockets().length.should.eql(existing_sockets);
- done();
- }, 10)
- });
- })
- })
- describe('passing connection: close', function() {
- it('sends a Connection: close header', function(done) {
- send_request({ connection: 'close' }, function(err, resp) {
- resp.body.headers['connection'].should.eql('close');
- done();
- })
- })
- it('no open sockets remain after request', function(done) {
- send_request({ connection: 'close' }, function(err, resp) {
- setTimeout(function() {
- get_active_sockets().length.should.eql(existing_sockets);
- done();
- }, 10)
- });
- })
- })
- describe('passing connection: keep-alive', function() {
- it('sends a Connection: keep-alive header (using options.headers.connection)', function(done) {
- send_request({ headers: { connection: 'keep-alive' }}, function(err, resp) {
- resp.body.headers['connection'].should.eql('keep-alive');
- done();
- })
- })
- it('sends a Connection: keep-alive header (using options.connection)', function(done) {
- send_request({ connection: 'keep-alive' }, function(err, resp) {
- resp.body.headers['connection'].should.eql('keep-alive');
- done();
- })
- })
- it('one open socket remain after request', function(done) {
- send_request({ connection: 'keep-alive' }, function(err, resp) {
- get_active_sockets().length.should.eql(existing_sockets + 1);
- done();
- });
- })
- })
- })
- describe('new node versions with smarter connection disposing', function() {
- before(function() {
- delete require.cache[require.resolve('..')]
- original_defaultMaxSockets = http.Agent.defaultMaxSockets;
- http.Agent.defaultMaxSockets = Infinity;
- needle = require('..');
- })
- after(function() {
- http.Agent.defaultMaxSockets = original_defaultMaxSockets;
- delete require.cache[require.resolve('..')]
- })
- describe('default options', function() {
- var node_major_ver = process.version.split('.')[0].replace('v', '');
- if (parseInt(node_major_ver) >= 4) {
- it('sets Connection header to close (> v4)', function(done) {
- send_request({}, function(err, resp) {
- resp.body.headers['connection'].should.eql('close');
- done()
- })
- })
- } else {
- it('sets Connection header to keep-alive (< v4)', function(done) {
- send_request({}, function(err, resp) {
- resp.body.headers['connection'].should.eql('keep-alive');
- done();
- })
- })
- }
- if (parseInt(node_major_ver) >= 14) {
- // TODO: figure out why this happens
- it('two open sockets remains after request (>= v14)', function(done) {
- send_request({}, function(err, resp) {
- get_active_sockets().length.should.eql(existing_sockets + 2);
- done();
- });
- })
- } else if (parseInt(node_major_ver) >= 8 || parseInt(node_major_ver) == 0) {
- it('one open socket remains after request (> v8 && v0.10)', function(done) {
- send_request({}, function(err, resp) {
- get_active_sockets().length.should.eql(existing_sockets + 1);
- done();
- });
- })
- } else {
- it('no open sockets remain after request (> v0.10 && < v8)', function(done) {
- send_request({}, function(err, resp) {
- get_active_sockets().length.should.eql(existing_sockets);
- done();
- });
- })
- }
- })
- describe('passing connection: close', function() {
- it('sends a Connection: close header', function(done) {
- send_request({ connection: 'close' }, function(err, resp) {
- resp.body.headers['connection'].should.eql('close');
- done();
- })
- })
- it('no open sockets remain after request', function(done) {
- send_request({ connection: 'close' }, function(err, resp) {
- setTimeout(function() {
- get_active_sockets().length.should.eql(existing_sockets);
- done();
- }, 10);
- });
- })
- })
- describe('passing connection: keep-alive', function() {
- it('sends a Connection: keep-alive header (using options.headers.connection)', function(done) {
- send_request({ headers: { connection: 'keep-alive' }}, function(err, resp) {
- resp.body.headers['connection'].should.eql('keep-alive');
- done();
- })
- })
- it('sends a Connection: keep-alive header (using options.connection)', function(done) {
- send_request({ connection: 'keep-alive' }, function(err, resp) {
- resp.body.headers['connection'].should.eql('keep-alive');
- done();
- })
- })
- it('one open socket remain after request', function(done) {
- send_request({ connection: 'keep-alive' }, function(err, resp) {
- get_active_sockets().length.should.eql(existing_sockets + 1);
- done();
- });
- })
- })
- })
- describe('using shared keep-alive agent', function() {
- before(function() {
- needle.defaults({ agent: http.Agent({ keepAlive: true }) })
- })
- after(function() {
- needle.defaults().agent.destroy(); // close existing connections
- needle.defaults({ agent: null }); // and reset default value
- })
- describe('default options', function() {
- it('sends a Connection: keep-alive header', function(done) {
- send_request({}, function(err, resp) {
- resp.body.headers['connection'].should.eql('keep-alive');
- done();
- })
- })
- it('one open socket remain after request', function(done) {
- send_request({ connection: 'keep-alive' }, function(err, resp) {
- setTimeout(function() {
- get_active_sockets().length.should.eql(existing_sockets + 1);
- done();
- }, 10);
- });
- })
- })
- describe('passing connection: close', function() {
- it('sends a Connection: close header', function(done) {
- send_request({ connection: 'close' }, function(err, resp) {
- resp.body.headers['connection'].should.eql('close');
- done();
- })
- })
- it('no open sockets remain after request', function(done) {
- send_request({ connection: 'close' }, function(err, resp) {
- setTimeout(function() {
- get_active_sockets().length.should.eql(existing_sockets);
- done();
- }, 10)
- });
- })
- })
- describe('passing connection: keep-alive', function() {
- it('sends a Connection: keep-alive header (using options.headers.connection)', function(done) {
- send_request({ headers: { connection: 'keep-alive' }}, function(err, resp) {
- resp.body.headers['connection'].should.eql('keep-alive');
- done();
- })
- })
- it('sends a Connection: keep-alive header (using options.connection)', function(done) {
- send_request({ connection: 'keep-alive' }, function(err, resp) {
- resp.body.headers['connection'].should.eql('keep-alive');
- done();
- })
- })
- it('one open socket remain after request', function(done) {
- send_request({ connection: 'keep-alive' }, function(err, resp) {
- setTimeout(function() {
- get_active_sockets().length.should.eql(existing_sockets + 1);
- done();
- }, 10);
- })
- })
- })
- })
- })
|