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'; 23 const common = require('../common'); 24 25 // The test works by making a total of 8 requests to the server. The first 26 // two are made with the server off - they should come back as ECONNREFUSED. 27 // The next two are made with server on - they should come back successful. 28 // The next two are made with the server off - and so on. Without the fix 29 // we were experiencing parse errors instead of ECONNREFUSED. 30 // https://github.com/nodejs/node-v0.x-archive/issues/784 31 32 const http = require('http'); 33 const assert = require('assert'); 34 35 36 const server = http.createServer(function(req, res) { 37 let body = ''; 38 39 req.setEncoding('utf8'); 40 req.on('data', function(chunk) { 41 body += chunk; 42 }); 43 44 req.on('end', function() { 45 assert.strictEqual(body, 'PING'); 46 res.writeHead(200); 47 res.end('PONG'); 48 }); 49 }); 50 51 52 server.on('listening', pingping); 53 54 55 function serverOn() { 56 console.error('Server ON'); 57 server.listen(common.PORT); 58 } 59 60 61 function serverOff() { 62 console.error('Server OFF'); 63 server.close(); 64 pingping(); 65 } 66 67 const responses = []; 68 69 70 function afterPing(result) { 71 responses.push(result); 72 console.error(`afterPing. responses.length = ${responses.length}`); 73 const ECONNREFUSED_RE = /ECONNREFUSED/; 74 const successRE = /success/; 75 switch (responses.length) { 76 case 2: 77 assert.ok(ECONNREFUSED_RE.test(responses[0])); 78 assert.ok(ECONNREFUSED_RE.test(responses[1])); 79 serverOn(); 80 break; 81 82 case 4: 83 assert.ok(successRE.test(responses[2])); 84 assert.ok(successRE.test(responses[3])); 85 serverOff(); 86 break; 87 88 case 6: 89 assert.ok(ECONNREFUSED_RE.test(responses[4])); 90 assert.ok(ECONNREFUSED_RE.test(responses[5])); 91 serverOn(); 92 break; 93 94 case 8: 95 assert.ok(successRE.test(responses[6])); 96 assert.ok(successRE.test(responses[7])); 97 server.close(); 98 // We should go to process.on('exit') from here. 99 break; 100 } 101 } 102 103 104 function ping() { 105 console.error('making req'); 106 107 const opt = { 108 port: common.PORT, 109 path: '/ping', 110 method: 'POST' 111 }; 112 113 const req = http.request(opt, function(res) { 114 let body = ''; 115 116 res.setEncoding('utf8'); 117 res.on('data', function(chunk) { 118 body += chunk; 119 }); 120 121 res.on('end', function() { 122 assert.strictEqual(body, 'PONG'); 123 assert.ok(!hadError); 124 gotEnd = true; 125 afterPing('success'); 126 }); 127 }); 128 129 req.end('PING'); 130 131 let gotEnd = false; 132 let hadError = false; 133 134 req.on('error', function(error) { 135 console.log(`Error making ping req: ${error}`); 136 hadError = true; 137 assert.ok(!gotEnd); 138 afterPing(error.message); 139 }); 140 } 141 142 143 function pingping() { 144 ping(); 145 ping(); 146 } 147 148 pingping(); 149 150 process.on('exit', function() { 151 console.error("process.on('exit')"); 152 console.error(responses); 153 154 assert.strictEqual(responses.length, 8); 155 }); 156