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