Name: js-handler/node_modules/restify/node_modules/keep-alive-agent/test/test.js
| 1: | /*global describe:true, it:true, before:true, after:true */ |
| 2: | |
| 3: | var |
| 4: | chai = require('chai'), |
| 5: | assert = chai.assert, |
| 6: | should = chai.should(), |
| 7: | http = require('http'), |
| 8: | https = require('https'), |
| 9: | KeepAliveAgent = require('../index'), |
| 10: | util = require('util') |
| 11: | ; |
| 12: | |
| 13: | |
| 14: | var serverConfig = { |
| 15: | hostname: 'localhost', |
| 16: | port: 8000 |
| 17: | }; |
| 18: | |
| 19: | function makeTestRequest(agent, callback) |
| 20: | { |
| 21: | http.get( |
| 22: | { |
| 23: | hostname: serverConfig.hostname, |
| 24: | port: serverConfig.port, |
| 25: | path: '/', |
| 26: | agent: agent |
| 27: | }, callback); |
| 28: | } |
| 29: | |
| 30: | describe('KeepAliveAgent', function() |
| 31: | { |
| 32: | var server; |
| 33: | |
| 34: | beforeEach(function(done) |
| 35: | { |
| 36: | server = http.createServer(function(request, response) |
| 37: | { |
| 38: | response.end("pong") |
| 39: | }); |
| 40: | server.on('listening', done); |
| 41: | server.listen(serverConfig.port); |
| 42: | }); |
| 43: | |
| 44: | afterEach(function() |
| 45: | { |
| 46: | server.close(); |
| 47: | server = null; |
| 48: | }); |
| 49: | |
| 50: | it('constructs an agent with the passed-in options', function() |
| 51: | { |
| 52: | var agent = new KeepAliveAgent({ maxSockets: 3 }); |
| 53: | |
| 54: | assert(agent.maxSockets === 3, 'max sockets option not passed through'); |
| 55: | agent.should.have.property('idleSockets'); |
| 56: | }); |
| 57: | |
| 58: | it('provides a socket to a request', function(done) |
| 59: | { |
| 60: | var agent = new KeepAliveAgent(); |
| 61: | http.get( |
| 62: | { |
| 63: | hostname: serverConfig.hostname, |
| 64: | port: serverConfig.port, |
| 65: | path: '/', |
| 66: | agent: agent |
| 67: | }, function(res) |
| 68: | { |
| 69: | // if we get here at all, it worked |
| 70: | done(); |
| 71: | }); |
| 72: | }); |
| 73: | |
| 74: | it('re-uses sockets on repeated requests to the same host:port', function(done) |
| 75: | { |
| 76: | var agent = new KeepAliveAgent(); |
| 77: | var getOptions = { |
| 78: | hostname: serverConfig.hostname, |
| 79: | port: serverConfig.port, |
| 80: | path: '/', |
| 81: | agent: agent |
| 82: | }; |
| 83: | |
| 84: | var requestsToDo = 10; |
| 85: | var intervalID; |
| 86: | |
| 87: | var requestOne = function() |
| 88: | { |
| 89: | http.get(getOptions, function(res) |
| 90: | { |
| 91: | if (--requestsToDo === 0) |
| 92: | { |
| 93: | clearInterval(intervalID); |
| 94: | |
| 95: | process.nextTick(function() |
| 96: | { |
| 97: | var name = serverConfig.hostname + ':' + serverConfig.port; |
| 98: | |
| 99: | agent.idleSockets.should.have.property(name); |
| 100: | agent.idleSockets[name].should.be.an('array'); |
| 101: | agent.idleSockets[name].length.should.equal(1); |
| 102: | var socket = agent.idleSockets[name][0]; |
| 103: | socket._requestCount.should.equal(10); |
| 104: | |
| 105: | done(); |
| 106: | }); |
| 107: | } |
| 108: | }); |
| 109: | }; |
| 110: | |
| 111: | intervalID = setInterval(requestOne, 15); |
| 112: | }); |
| 113: | |
| 114: | it('does not return destroyed sockets to the idle pool', function(done) |
| 115: | { |
| 116: | var agent = new KeepAliveAgent(); |
| 117: | makeTestRequest(agent, function(response) |
| 118: | { |
| 119: | response.connection.destroy(); |
| 120: | |
| 121: | process.nextTick(function() |
| 122: | { |
| 123: | var name = serverConfig.hostname + ':' + serverConfig.port; |
| 124: | agent.idleSockets.should.not.have.property(name); |
| 125: | done(); |
| 126: | }); |
| 127: | }); |
| 128: | }); |
| 129: | |
| 130: | it('does not attempt to use destroyed sockets from the idle list', function() |
| 131: | { |
| 132: | var agent = new KeepAliveAgent(); |
| 133: | var name = serverConfig.hostname + ':' + serverConfig.port; |
| 134: | |
| 135: | agent.idleSockets[name] = []; |
| 136: | agent.idleSockets[name].push({ destroyed: true }); |
| 137: | agent.idleSockets[name].push({ destroyed: true }); |
| 138: | agent.idleSockets[name].push({ destroyed: true }); |
| 139: | agent.idleSockets[name].push({ destroyed: true }); |
| 140: | |
| 141: | var socket = agent.nextIdleSocket(name); |
| 142: | assert.equal(socket, null); |
| 143: | assert.equal(agent.idleSockets[name].length, 0); |
| 144: | }); |
| 145: | |
| 146: | it('reuses a good socket until it is destroyed', function(done) |
| 147: | { |
| 148: | var agent = new KeepAliveAgent(); |
| 149: | var name = serverConfig.hostname + ':' + serverConfig.port; |
| 150: | |
| 151: | makeTestRequest(agent, function(response) |
| 152: | { |
| 153: | |
| 154: | process.nextTick(function() |
| 155: | { |
| 156: | agent.idleSockets.should.have.property(name); |
| 157: | assert(Array.isArray(agent.idleSockets[name]), 'expected idle sockets list for ' + name + ' to be an array'); |
| 158: | assert.equal(agent.idleSockets[name].length, 1, 'expected idle sockets list to contain exactly 1 item'); |
| 159: | var socket = agent.idleSockets[name][0]; |
| 160: | assert.equal(socket._requestCount, 1, 'expected socket request count to be 1') |
| 161: | |
| 162: | makeTestRequest(agent, function(response) |
| 163: | { |
| 164: | process.nextTick(function() |
| 165: | { |
| 166: | agent.idleSockets.should.have.property(name); |
| 167: | assert.equal(agent.idleSockets[name].length, 0, 'expected zero sockets in our idle queue'); |
| 168: | done(); |
| 169: | }); |
| 170: | response.connection.destroy(); |
| 171: | }); |
| 172: | }); |
| 173: | }); |
| 174: | }); |
| 175: | }); |
| 176: | |
| 177: | describe('KeepAliveAgent.Secure', function() |
| 178: | { |
| 179: | it('can construct a secure keep-alive agent', function() |
| 180: | { |
| 181: | var secureAgent = new KeepAliveAgent.Secure({}); |
| 182: | assert(secureAgent.defaultPort === 443); |
| 183: | }); |
| 184: | |
| 185: | it('provides a socket to a request', function(done) |
| 186: | { |
| 187: | https.get( |
| 188: | { |
| 189: | hostname: 'one.voxer.com', |
| 190: | port: 443, |
| 191: | path: '/ping', |
| 192: | agent: new KeepAliveAgent.Secure(), |
| 193: | }, function(response) |
| 194: | { |
| 195: | // if we get here at all, it worked |
| 196: | done(); |
| 197: | }); |
| 198: | }); |
| 199: | |
| 200: | it('reuses sockets for secure connections', function(done) |
| 201: | { |
| 202: | var agent = new KeepAliveAgent.Secure(); |
| 203: | var getOptions = { |
| 204: | hostname: 'one.voxer.com', |
| 205: | port: 443, |
| 206: | path: '/ping', |
| 207: | agent: agent, |
| 208: | }; |
| 209: | var name = 'one.voxer.com:443'; |
| 210: | |
| 211: | https.get(getOptions, function(response) |
| 212: | { |
| 213: | |
| 214: | process.nextTick(function() |
| 215: | { |
| 216: | agent.idleSockets.should.have.property(name); |
| 217: | assert(Array.isArray(agent.idleSockets[name]), 'expected idle sockets list for ' + name + ' to be an array'); |
| 218: | assert.equal(agent.idleSockets[name].length, 1, 'expected idle sockets list to contain exactly 1 item'); |
| 219: | var socket = agent.idleSockets[name][0]; |
| 220: | assert.equal(socket._requestCount, 1, 'expected socket request count to be 1') |
| 221: | |
| 222: | https.get(getOptions, function(response) |
| 223: | { |
| 224: | process.nextTick(function() |
| 225: | { |
| 226: | agent.idleSockets.should.have.property(name); |
| 227: | assert.equal(agent.idleSockets[name].length, 0, 'expected zero sockets in our idle queue'); |
| 228: | done(); |
| 229: | }); |
| 230: | response.connection.destroy(); |
| 231: | }); |
| 232: | }); |
| 233: | }); |
| 234: | }); |
| 235: | |
| 236: | it('does not attempt to use destroyed sockets from the idle list', function() |
| 237: | { |
| 238: | var agent = new KeepAliveAgent.Secure(); |
| 239: | var name = serverConfig.hostname + ':' + serverConfig.port; |
| 240: | |
| 241: | agent.idleSockets[name] = []; |
| 242: | agent.idleSockets[name].push({ pair: { ssl: null } }); |
| 243: | agent.idleSockets[name].push({ pair: { ssl: null } }); |
| 244: | agent.idleSockets[name].push({ pair: { ssl: null } }); |
| 245: | agent.idleSockets[name].push({ pair: { ssl: null } }); |
| 246: | agent.idleSockets[name].push({ pair: { ssl: null } }); |
| 247: | |
| 248: | var socket = agent.nextIdleSocket(name); |
| 249: | assert.equal(socket, null); |
| 250: | assert.equal(agent.idleSockets[name].length, 0); |
| 251: | }); |
| 252: | |
| 253: | }); |
