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 27let requests_sent = 0; 28let requests_done = 0; 29const options = { 30 method: 'GET', 31 port: undefined, 32 host: '127.0.0.1', 33}; 34 35const server = http.createServer((req, res) => { 36 const m = /\/(.*)/.exec(req.url); 37 const reqid = parseInt(m[1], 10); 38 if (reqid % 2) { 39 // Do not reply the request 40 } else { 41 res.writeHead(200, { 'Content-Type': 'text/plain' }); 42 res.write(reqid.toString()); 43 res.end(); 44 } 45}); 46 47server.listen(0, options.host, function() { 48 options.port = this.address().port; 49 50 for (requests_sent = 0; requests_sent < 30; requests_sent += 1) { 51 options.path = `/${requests_sent}`; 52 const req = http.request(options); 53 req.id = requests_sent; 54 req.on('response', (res) => { 55 res.on('data', function(data) { 56 console.log(`res#${this.req.id} data:${data}`); 57 }); 58 res.on('end', function(data) { 59 console.log(`res#${this.req.id} end`); 60 requests_done += 1; 61 req.destroy(); 62 }); 63 }); 64 req.on('close', function() { 65 console.log(`req#${this.id} close`); 66 }); 67 req.on('error', function() { 68 console.log(`req#${this.id} error`); 69 this.destroy(); 70 }); 71 req.setTimeout(50, function() { 72 console.log(`req#${this.id} timeout`); 73 this.abort(); 74 requests_done += 1; 75 }); 76 req.end(); 77 } 78 79 setTimeout(function maybeDone() { 80 if (requests_done >= requests_sent) { 81 setTimeout(() => { 82 server.close(); 83 }, 100); 84 } else { 85 setTimeout(maybeDone, 100); 86 } 87 }, 100); 88}); 89 90process.on('exit', () => { 91 console.error(`done=${requests_done} sent=${requests_sent}`); 92 // Check that timeout on http request was not called too much 93 assert.strictEqual(requests_done, requests_sent); 94}); 95