1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23require('../common'); 24const assert = require('assert'); 25const http = require('http'); 26 27const body = 'hello world\n'; 28const headers = { 'connection': 'keep-alive' }; 29 30const server = http.createServer(function(req, res) { 31 res.writeHead(200, { 'Content-Length': body.length, 'Connection': 'close' }); 32 res.write(body); 33 res.end(); 34}); 35 36let connectCount = 0; 37 38 39server.listen(0, function() { 40 const agent = new http.Agent({ maxSockets: 1 }); 41 const name = agent.getName({ port: this.address().port }); 42 let request = http.request({ 43 method: 'GET', 44 path: '/', 45 headers: headers, 46 port: this.address().port, 47 agent: agent 48 }, function(res) { 49 assert.strictEqual(agent.sockets[name].length, 1); 50 res.resume(); 51 }); 52 request.on('socket', function(s) { 53 s.on('connect', function() { 54 connectCount++; 55 }); 56 }); 57 request.end(); 58 59 request = http.request({ 60 method: 'GET', 61 path: '/', 62 headers: headers, 63 port: this.address().port, 64 agent: agent 65 }, function(res) { 66 assert.strictEqual(agent.sockets[name].length, 1); 67 res.resume(); 68 }); 69 request.on('socket', function(s) { 70 s.on('connect', function() { 71 connectCount++; 72 }); 73 }); 74 request.end(); 75 request = http.request({ 76 method: 'GET', 77 path: '/', 78 headers: headers, 79 port: this.address().port, 80 agent: agent 81 }, function(response) { 82 response.on('end', function() { 83 assert.strictEqual(agent.sockets[name].length, 1); 84 server.close(); 85 }); 86 response.resume(); 87 }); 88 request.on('socket', function(s) { 89 s.on('connect', function() { 90 connectCount++; 91 }); 92 }); 93 request.end(); 94}); 95 96process.on('exit', function() { 97 assert.strictEqual(connectCount, 3); 98}); 99