11cb0ef41Sopenharmony_ci// Copyright Joyent, Inc. and other Node contributors. 21cb0ef41Sopenharmony_ci// 31cb0ef41Sopenharmony_ci// Permission is hereby granted, free of charge, to any person obtaining a 41cb0ef41Sopenharmony_ci// copy of this software and associated documentation files (the 51cb0ef41Sopenharmony_ci// "Software"), to deal in the Software without restriction, including 61cb0ef41Sopenharmony_ci// without limitation the rights to use, copy, modify, merge, publish, 71cb0ef41Sopenharmony_ci// distribute, sublicense, and/or sell copies of the Software, and to permit 81cb0ef41Sopenharmony_ci// persons to whom the Software is furnished to do so, subject to the 91cb0ef41Sopenharmony_ci// following conditions: 101cb0ef41Sopenharmony_ci// 111cb0ef41Sopenharmony_ci// The above copyright notice and this permission notice shall be included 121cb0ef41Sopenharmony_ci// in all copies or substantial portions of the Software. 131cb0ef41Sopenharmony_ci// 141cb0ef41Sopenharmony_ci// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 151cb0ef41Sopenharmony_ci// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 161cb0ef41Sopenharmony_ci// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 171cb0ef41Sopenharmony_ci// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 181cb0ef41Sopenharmony_ci// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 191cb0ef41Sopenharmony_ci// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 201cb0ef41Sopenharmony_ci// USE OR OTHER DEALINGS IN THE SOFTWARE. 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ci'use strict'; 231cb0ef41Sopenharmony_ciconst common = require('../common'); 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci// The test works by making a total of 8 requests to the server. The first 261cb0ef41Sopenharmony_ci// two are made with the server off - they should come back as ECONNREFUSED. 271cb0ef41Sopenharmony_ci// The next two are made with server on - they should come back successful. 281cb0ef41Sopenharmony_ci// The next two are made with the server off - and so on. Without the fix 291cb0ef41Sopenharmony_ci// we were experiencing parse errors instead of ECONNREFUSED. 301cb0ef41Sopenharmony_ci// https://github.com/nodejs/node-v0.x-archive/issues/784 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciconst http = require('http'); 331cb0ef41Sopenharmony_ciconst assert = require('assert'); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ciconst server = http.createServer(function(req, res) { 371cb0ef41Sopenharmony_ci let body = ''; 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci req.setEncoding('utf8'); 401cb0ef41Sopenharmony_ci req.on('data', function(chunk) { 411cb0ef41Sopenharmony_ci body += chunk; 421cb0ef41Sopenharmony_ci }); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci req.on('end', function() { 451cb0ef41Sopenharmony_ci assert.strictEqual(body, 'PING'); 461cb0ef41Sopenharmony_ci res.writeHead(200); 471cb0ef41Sopenharmony_ci res.end('PONG'); 481cb0ef41Sopenharmony_ci }); 491cb0ef41Sopenharmony_ci}); 501cb0ef41Sopenharmony_ci 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_ciserver.on('listening', pingping); 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_cifunction serverOn() { 561cb0ef41Sopenharmony_ci console.error('Server ON'); 571cb0ef41Sopenharmony_ci server.listen(common.PORT); 581cb0ef41Sopenharmony_ci} 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_cifunction serverOff() { 621cb0ef41Sopenharmony_ci console.error('Server OFF'); 631cb0ef41Sopenharmony_ci server.close(); 641cb0ef41Sopenharmony_ci pingping(); 651cb0ef41Sopenharmony_ci} 661cb0ef41Sopenharmony_ci 671cb0ef41Sopenharmony_ciconst responses = []; 681cb0ef41Sopenharmony_ci 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_cifunction afterPing(result) { 711cb0ef41Sopenharmony_ci responses.push(result); 721cb0ef41Sopenharmony_ci console.error(`afterPing. responses.length = ${responses.length}`); 731cb0ef41Sopenharmony_ci const ECONNREFUSED_RE = /ECONNREFUSED/; 741cb0ef41Sopenharmony_ci const successRE = /success/; 751cb0ef41Sopenharmony_ci switch (responses.length) { 761cb0ef41Sopenharmony_ci case 2: 771cb0ef41Sopenharmony_ci assert.match(responses[0], ECONNREFUSED_RE); 781cb0ef41Sopenharmony_ci assert.match(responses[1], ECONNREFUSED_RE); 791cb0ef41Sopenharmony_ci serverOn(); 801cb0ef41Sopenharmony_ci break; 811cb0ef41Sopenharmony_ci 821cb0ef41Sopenharmony_ci case 4: 831cb0ef41Sopenharmony_ci assert.match(responses[2], successRE); 841cb0ef41Sopenharmony_ci assert.match(responses[3], successRE); 851cb0ef41Sopenharmony_ci serverOff(); 861cb0ef41Sopenharmony_ci break; 871cb0ef41Sopenharmony_ci 881cb0ef41Sopenharmony_ci case 6: 891cb0ef41Sopenharmony_ci assert.match(responses[4], ECONNREFUSED_RE); 901cb0ef41Sopenharmony_ci assert.match(responses[5], ECONNREFUSED_RE); 911cb0ef41Sopenharmony_ci serverOn(); 921cb0ef41Sopenharmony_ci break; 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ci case 8: 951cb0ef41Sopenharmony_ci assert.match(responses[6], successRE); 961cb0ef41Sopenharmony_ci assert.match(responses[7], successRE); 971cb0ef41Sopenharmony_ci server.close(); 981cb0ef41Sopenharmony_ci // We should go to process.on('exit') from here. 991cb0ef41Sopenharmony_ci break; 1001cb0ef41Sopenharmony_ci } 1011cb0ef41Sopenharmony_ci} 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci 1041cb0ef41Sopenharmony_cifunction ping() { 1051cb0ef41Sopenharmony_ci console.error('making req'); 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci const opt = { 1081cb0ef41Sopenharmony_ci port: common.PORT, 1091cb0ef41Sopenharmony_ci path: '/ping', 1101cb0ef41Sopenharmony_ci method: 'POST' 1111cb0ef41Sopenharmony_ci }; 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ci const req = http.request(opt, function(res) { 1141cb0ef41Sopenharmony_ci let body = ''; 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_ci res.setEncoding('utf8'); 1171cb0ef41Sopenharmony_ci res.on('data', function(chunk) { 1181cb0ef41Sopenharmony_ci body += chunk; 1191cb0ef41Sopenharmony_ci }); 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci res.on('end', function() { 1221cb0ef41Sopenharmony_ci assert.strictEqual(body, 'PONG'); 1231cb0ef41Sopenharmony_ci assert.ok(!hadError); 1241cb0ef41Sopenharmony_ci gotEnd = true; 1251cb0ef41Sopenharmony_ci afterPing('success'); 1261cb0ef41Sopenharmony_ci }); 1271cb0ef41Sopenharmony_ci }); 1281cb0ef41Sopenharmony_ci 1291cb0ef41Sopenharmony_ci req.end('PING'); 1301cb0ef41Sopenharmony_ci 1311cb0ef41Sopenharmony_ci let gotEnd = false; 1321cb0ef41Sopenharmony_ci let hadError = false; 1331cb0ef41Sopenharmony_ci 1341cb0ef41Sopenharmony_ci req.on('error', function(error) { 1351cb0ef41Sopenharmony_ci console.log(`Error making ping req: ${error}`); 1361cb0ef41Sopenharmony_ci hadError = true; 1371cb0ef41Sopenharmony_ci assert.ok(!gotEnd); 1381cb0ef41Sopenharmony_ci afterPing(error.message); 1391cb0ef41Sopenharmony_ci }); 1401cb0ef41Sopenharmony_ci} 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_cifunction pingping() { 1441cb0ef41Sopenharmony_ci ping(); 1451cb0ef41Sopenharmony_ci ping(); 1461cb0ef41Sopenharmony_ci} 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_cipingping(); 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ciprocess.on('exit', function() { 1511cb0ef41Sopenharmony_ci console.error("process.on('exit')"); 1521cb0ef41Sopenharmony_ci console.error(responses); 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_ci assert.strictEqual(responses.length, 8); 1551cb0ef41Sopenharmony_ci}); 156