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_ciconst assert = require('assert'); 261cb0ef41Sopenharmony_ciconst http = require('http'); 271cb0ef41Sopenharmony_ciconst net = require('net'); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciconst tests = []; 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_cifunction test(fn) { 321cb0ef41Sopenharmony_ci if (!tests.length) 331cb0ef41Sopenharmony_ci process.nextTick(run); 341cb0ef41Sopenharmony_ci tests.push(common.mustCall(fn)); 351cb0ef41Sopenharmony_ci} 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_cifunction run() { 381cb0ef41Sopenharmony_ci const fn = tests.shift(); 391cb0ef41Sopenharmony_ci if (fn) { 401cb0ef41Sopenharmony_ci fn(run); 411cb0ef41Sopenharmony_ci } 421cb0ef41Sopenharmony_ci} 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_citest(function serverTimeout(cb) { 451cb0ef41Sopenharmony_ci const server = http.createServer(); 461cb0ef41Sopenharmony_ci server.listen(common.mustCall(() => { 471cb0ef41Sopenharmony_ci const s = server.setTimeout(50, common.mustCall((socket) => { 481cb0ef41Sopenharmony_ci socket.destroy(); 491cb0ef41Sopenharmony_ci server.close(); 501cb0ef41Sopenharmony_ci cb(); 511cb0ef41Sopenharmony_ci })); 521cb0ef41Sopenharmony_ci assert.ok(s instanceof http.Server); 531cb0ef41Sopenharmony_ci http.get({ 541cb0ef41Sopenharmony_ci port: server.address().port 551cb0ef41Sopenharmony_ci }).on('error', common.mustCall()); 561cb0ef41Sopenharmony_ci })); 571cb0ef41Sopenharmony_ci}); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_citest(function serverRequestTimeout(cb) { 601cb0ef41Sopenharmony_ci const server = http.createServer(common.mustCall((req, res) => { 611cb0ef41Sopenharmony_ci // Just do nothing, we should get a timeout event. 621cb0ef41Sopenharmony_ci const s = req.setTimeout(50, common.mustCall((socket) => { 631cb0ef41Sopenharmony_ci socket.destroy(); 641cb0ef41Sopenharmony_ci server.close(); 651cb0ef41Sopenharmony_ci cb(); 661cb0ef41Sopenharmony_ci })); 671cb0ef41Sopenharmony_ci assert.ok(s instanceof http.IncomingMessage); 681cb0ef41Sopenharmony_ci })); 691cb0ef41Sopenharmony_ci server.listen(common.mustCall(() => { 701cb0ef41Sopenharmony_ci const req = http.request({ 711cb0ef41Sopenharmony_ci port: server.address().port, 721cb0ef41Sopenharmony_ci method: 'POST' 731cb0ef41Sopenharmony_ci }); 741cb0ef41Sopenharmony_ci req.on('error', common.mustCall()); 751cb0ef41Sopenharmony_ci req.write('Hello'); 761cb0ef41Sopenharmony_ci // req is in progress 771cb0ef41Sopenharmony_ci })); 781cb0ef41Sopenharmony_ci}); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_citest(function serverResponseTimeout(cb) { 811cb0ef41Sopenharmony_ci const server = http.createServer(common.mustCall((req, res) => { 821cb0ef41Sopenharmony_ci // Just do nothing, we should get a timeout event. 831cb0ef41Sopenharmony_ci const s = res.setTimeout(50, common.mustCall((socket) => { 841cb0ef41Sopenharmony_ci socket.destroy(); 851cb0ef41Sopenharmony_ci server.close(); 861cb0ef41Sopenharmony_ci cb(); 871cb0ef41Sopenharmony_ci })); 881cb0ef41Sopenharmony_ci assert.ok(s instanceof http.OutgoingMessage); 891cb0ef41Sopenharmony_ci })); 901cb0ef41Sopenharmony_ci server.listen(common.mustCall(() => { 911cb0ef41Sopenharmony_ci http.get({ 921cb0ef41Sopenharmony_ci port: server.address().port 931cb0ef41Sopenharmony_ci }).on('error', common.mustCall()); 941cb0ef41Sopenharmony_ci })); 951cb0ef41Sopenharmony_ci}); 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_citest(function serverRequestNotTimeoutAfterEnd(cb) { 981cb0ef41Sopenharmony_ci const server = http.createServer(common.mustCall((req, res) => { 991cb0ef41Sopenharmony_ci // Just do nothing, we should get a timeout event. 1001cb0ef41Sopenharmony_ci const s = req.setTimeout(50, common.mustNotCall()); 1011cb0ef41Sopenharmony_ci assert.ok(s instanceof http.IncomingMessage); 1021cb0ef41Sopenharmony_ci res.on('timeout', common.mustCall()); 1031cb0ef41Sopenharmony_ci })); 1041cb0ef41Sopenharmony_ci server.on('timeout', common.mustCall((socket) => { 1051cb0ef41Sopenharmony_ci socket.destroy(); 1061cb0ef41Sopenharmony_ci server.close(); 1071cb0ef41Sopenharmony_ci cb(); 1081cb0ef41Sopenharmony_ci })); 1091cb0ef41Sopenharmony_ci server.listen(common.mustCall(() => { 1101cb0ef41Sopenharmony_ci http.get({ 1111cb0ef41Sopenharmony_ci port: server.address().port 1121cb0ef41Sopenharmony_ci }).on('error', common.mustCall()); 1131cb0ef41Sopenharmony_ci })); 1141cb0ef41Sopenharmony_ci}); 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_citest(function serverResponseTimeoutWithPipeline(cb) { 1171cb0ef41Sopenharmony_ci let caughtTimeout = ''; 1181cb0ef41Sopenharmony_ci let secReceived = false; 1191cb0ef41Sopenharmony_ci process.on('exit', () => { 1201cb0ef41Sopenharmony_ci assert.strictEqual(caughtTimeout, '/2'); 1211cb0ef41Sopenharmony_ci }); 1221cb0ef41Sopenharmony_ci const server = http.createServer((req, res) => { 1231cb0ef41Sopenharmony_ci if (req.url === '/2') 1241cb0ef41Sopenharmony_ci secReceived = true; 1251cb0ef41Sopenharmony_ci if (req.url === '/1') { 1261cb0ef41Sopenharmony_ci res.end(); 1271cb0ef41Sopenharmony_ci return; 1281cb0ef41Sopenharmony_ci } 1291cb0ef41Sopenharmony_ci const s = res.setTimeout(50, () => { 1301cb0ef41Sopenharmony_ci caughtTimeout += req.url; 1311cb0ef41Sopenharmony_ci }); 1321cb0ef41Sopenharmony_ci assert.ok(s instanceof http.OutgoingMessage); 1331cb0ef41Sopenharmony_ci }); 1341cb0ef41Sopenharmony_ci server.on('timeout', common.mustCall((socket) => { 1351cb0ef41Sopenharmony_ci if (secReceived) { 1361cb0ef41Sopenharmony_ci socket.destroy(); 1371cb0ef41Sopenharmony_ci server.close(); 1381cb0ef41Sopenharmony_ci cb(); 1391cb0ef41Sopenharmony_ci } 1401cb0ef41Sopenharmony_ci })); 1411cb0ef41Sopenharmony_ci server.listen(common.mustCall(() => { 1421cb0ef41Sopenharmony_ci const options = { 1431cb0ef41Sopenharmony_ci port: server.address().port, 1441cb0ef41Sopenharmony_ci allowHalfOpen: true, 1451cb0ef41Sopenharmony_ci }; 1461cb0ef41Sopenharmony_ci const c = net.connect(options, () => { 1471cb0ef41Sopenharmony_ci c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n'); 1481cb0ef41Sopenharmony_ci c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n'); 1491cb0ef41Sopenharmony_ci c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n'); 1501cb0ef41Sopenharmony_ci }); 1511cb0ef41Sopenharmony_ci })); 1521cb0ef41Sopenharmony_ci}); 1531cb0ef41Sopenharmony_ci 1541cb0ef41Sopenharmony_citest(function idleTimeout(cb) { 1551cb0ef41Sopenharmony_ci // Test that the an idle connection invokes the timeout callback. 1561cb0ef41Sopenharmony_ci const server = http.createServer(); 1571cb0ef41Sopenharmony_ci const s = server.setTimeout(50, common.mustCall((socket) => { 1581cb0ef41Sopenharmony_ci socket.destroy(); 1591cb0ef41Sopenharmony_ci server.close(); 1601cb0ef41Sopenharmony_ci cb(); 1611cb0ef41Sopenharmony_ci })); 1621cb0ef41Sopenharmony_ci assert.ok(s instanceof http.Server); 1631cb0ef41Sopenharmony_ci server.listen(common.mustCall(() => { 1641cb0ef41Sopenharmony_ci const options = { 1651cb0ef41Sopenharmony_ci port: server.address().port, 1661cb0ef41Sopenharmony_ci allowHalfOpen: true, 1671cb0ef41Sopenharmony_ci }; 1681cb0ef41Sopenharmony_ci const c = net.connect(options, () => { 1691cb0ef41Sopenharmony_ci // ECONNRESET could happen on a heavily-loaded server. 1701cb0ef41Sopenharmony_ci c.on('error', (e) => { 1711cb0ef41Sopenharmony_ci if (e.message !== 'read ECONNRESET') 1721cb0ef41Sopenharmony_ci throw e; 1731cb0ef41Sopenharmony_ci }); 1741cb0ef41Sopenharmony_ci c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n'); 1751cb0ef41Sopenharmony_ci // Keep-Alive 1761cb0ef41Sopenharmony_ci }); 1771cb0ef41Sopenharmony_ci })); 1781cb0ef41Sopenharmony_ci}); 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_citest(function fastTimeout(cb) { 1811cb0ef41Sopenharmony_ci let connectionHandlerInvoked = false; 1821cb0ef41Sopenharmony_ci let timeoutHandlerInvoked = false; 1831cb0ef41Sopenharmony_ci let connectionSocket; 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci function invokeCallbackIfDone() { 1861cb0ef41Sopenharmony_ci if (connectionHandlerInvoked && timeoutHandlerInvoked) { 1871cb0ef41Sopenharmony_ci connectionSocket.destroy(); 1881cb0ef41Sopenharmony_ci server.close(); 1891cb0ef41Sopenharmony_ci cb(); 1901cb0ef41Sopenharmony_ci } 1911cb0ef41Sopenharmony_ci } 1921cb0ef41Sopenharmony_ci 1931cb0ef41Sopenharmony_ci const server = http.createServer(common.mustCall((req, res) => { 1941cb0ef41Sopenharmony_ci req.on('timeout', common.mustNotCall()); 1951cb0ef41Sopenharmony_ci res.end(); 1961cb0ef41Sopenharmony_ci connectionHandlerInvoked = true; 1971cb0ef41Sopenharmony_ci invokeCallbackIfDone(); 1981cb0ef41Sopenharmony_ci })); 1991cb0ef41Sopenharmony_ci const s = server.setTimeout(1, common.mustCall((socket) => { 2001cb0ef41Sopenharmony_ci connectionSocket = socket; 2011cb0ef41Sopenharmony_ci timeoutHandlerInvoked = true; 2021cb0ef41Sopenharmony_ci invokeCallbackIfDone(); 2031cb0ef41Sopenharmony_ci })); 2041cb0ef41Sopenharmony_ci assert.ok(s instanceof http.Server); 2051cb0ef41Sopenharmony_ci server.listen(common.mustCall(() => { 2061cb0ef41Sopenharmony_ci const options = { 2071cb0ef41Sopenharmony_ci port: server.address().port, 2081cb0ef41Sopenharmony_ci allowHalfOpen: true, 2091cb0ef41Sopenharmony_ci }; 2101cb0ef41Sopenharmony_ci const c = net.connect(options, () => { 2111cb0ef41Sopenharmony_ci c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n'); 2121cb0ef41Sopenharmony_ci // Keep-Alive 2131cb0ef41Sopenharmony_ci }); 2141cb0ef41Sopenharmony_ci })); 2151cb0ef41Sopenharmony_ci}); 216