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'; 23const common = require('../common'); 24const assert = require('assert'); 25const http = require('http'); 26 27let complete; 28 29const server = http.createServer(common.mustCall((req, res) => { 30 // We should not see the queued /thatotherone request within the server 31 // as it should be aborted before it is sent. 32 assert.strictEqual(req.url, '/'); 33 34 res.writeHead(200); 35 res.write('foo'); 36 37 complete = complete || function() { 38 res.end(); 39 }; 40})); 41 42server.listen(0, common.mustCall(() => { 43 const agent = new http.Agent({ maxSockets: 1 }); 44 assert.strictEqual(Object.keys(agent.sockets).length, 0); 45 46 const options = { 47 hostname: 'localhost', 48 port: server.address().port, 49 method: 'GET', 50 path: '/', 51 agent: agent 52 }; 53 54 const req1 = http.request(options); 55 req1.on('response', (res1) => { 56 assert.strictEqual(Object.keys(agent.sockets).length, 1); 57 assert.strictEqual(Object.keys(agent.requests).length, 0); 58 59 const req2 = http.request({ 60 method: 'GET', 61 host: 'localhost', 62 port: server.address().port, 63 path: '/thatotherone', 64 agent: agent 65 }); 66 assert.strictEqual(Object.keys(agent.sockets).length, 1); 67 assert.strictEqual(Object.keys(agent.requests).length, 1); 68 69 // TODO(jasnell): This event does not appear to currently be triggered. 70 // is this handler actually required? 71 req2.on('error', (err) => { 72 // This is expected in response to our explicit abort call 73 assert.strictEqual(err.code, 'ECONNRESET'); 74 }); 75 76 req2.end(); 77 req2.abort(); 78 79 assert.strictEqual(Object.keys(agent.sockets).length, 1); 80 assert.strictEqual(Object.keys(agent.requests).length, 1); 81 82 res1.on('data', (chunk) => complete()); 83 84 res1.on('end', common.mustCall(() => { 85 setTimeout(common.mustCall(() => { 86 assert.strictEqual(Object.keys(agent.sockets).length, 0); 87 assert.strictEqual(Object.keys(agent.requests).length, 0); 88 89 server.close(); 90 }), 100); 91 })); 92 }); 93 94 req1.end(); 95})); 96