11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst http = require('http'); 51cb0ef41Sopenharmony_ciconst MakeDuplexPair = require('../common/duplexpair'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci// Test 1: Simple HTTP test, no keep-alive. 81cb0ef41Sopenharmony_ci{ 91cb0ef41Sopenharmony_ci const testData = 'Hello, World!\n'; 101cb0ef41Sopenharmony_ci const server = http.createServer(common.mustCall((req, res) => { 111cb0ef41Sopenharmony_ci res.statusCode = 200; 121cb0ef41Sopenharmony_ci res.setHeader('Content-Type', 'text/plain'); 131cb0ef41Sopenharmony_ci res.end(testData); 141cb0ef41Sopenharmony_ci })); 151cb0ef41Sopenharmony_ci 161cb0ef41Sopenharmony_ci const { clientSide, serverSide } = MakeDuplexPair(); 171cb0ef41Sopenharmony_ci server.emit('connection', serverSide); 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ci const req = http.request({ 201cb0ef41Sopenharmony_ci createConnection: common.mustCall(() => clientSide) 211cb0ef41Sopenharmony_ci }, common.mustCall((res) => { 221cb0ef41Sopenharmony_ci res.setEncoding('utf8'); 231cb0ef41Sopenharmony_ci res.on('data', common.mustCall((data) => { 241cb0ef41Sopenharmony_ci assert.strictEqual(data, testData); 251cb0ef41Sopenharmony_ci })); 261cb0ef41Sopenharmony_ci res.on('end', common.mustCall()); 271cb0ef41Sopenharmony_ci })); 281cb0ef41Sopenharmony_ci req.end(); 291cb0ef41Sopenharmony_ci} 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci// Test 2: Keep-alive for 2 requests. 321cb0ef41Sopenharmony_ci{ 331cb0ef41Sopenharmony_ci const testData = 'Hello, World!\n'; 341cb0ef41Sopenharmony_ci const server = http.createServer(common.mustCall((req, res) => { 351cb0ef41Sopenharmony_ci res.statusCode = 200; 361cb0ef41Sopenharmony_ci res.setHeader('Content-Type', 'text/plain'); 371cb0ef41Sopenharmony_ci res.end(testData); 381cb0ef41Sopenharmony_ci }, 2)); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ci const { clientSide, serverSide } = MakeDuplexPair(); 411cb0ef41Sopenharmony_ci server.emit('connection', serverSide); 421cb0ef41Sopenharmony_ci 431cb0ef41Sopenharmony_ci function doRequest(cb) { 441cb0ef41Sopenharmony_ci const req = http.request({ 451cb0ef41Sopenharmony_ci createConnection: common.mustCall(() => clientSide), 461cb0ef41Sopenharmony_ci headers: { Connection: 'keep-alive' } 471cb0ef41Sopenharmony_ci }, common.mustCall((res) => { 481cb0ef41Sopenharmony_ci res.setEncoding('utf8'); 491cb0ef41Sopenharmony_ci res.on('data', common.mustCall((data) => { 501cb0ef41Sopenharmony_ci assert.strictEqual(data, testData); 511cb0ef41Sopenharmony_ci })); 521cb0ef41Sopenharmony_ci res.on('end', common.mustCall(cb)); 531cb0ef41Sopenharmony_ci })); 541cb0ef41Sopenharmony_ci req.shouldKeepAlive = true; 551cb0ef41Sopenharmony_ci req.end(); 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ci doRequest(() => { 591cb0ef41Sopenharmony_ci doRequest(); 601cb0ef41Sopenharmony_ci }); 611cb0ef41Sopenharmony_ci} 621cb0ef41Sopenharmony_ci 631cb0ef41Sopenharmony_ci// Test 3: Connection: close request/response with chunked 641cb0ef41Sopenharmony_ci{ 651cb0ef41Sopenharmony_ci const testData = 'Hello, World!\n'; 661cb0ef41Sopenharmony_ci const server = http.createServer(common.mustCall((req, res) => { 671cb0ef41Sopenharmony_ci req.setEncoding('utf8'); 681cb0ef41Sopenharmony_ci req.resume(); 691cb0ef41Sopenharmony_ci req.on('data', common.mustCall(function test3_req_data(data) { 701cb0ef41Sopenharmony_ci assert.strictEqual(data, testData); 711cb0ef41Sopenharmony_ci })); 721cb0ef41Sopenharmony_ci req.once('end', function() { 731cb0ef41Sopenharmony_ci res.statusCode = 200; 741cb0ef41Sopenharmony_ci res.setHeader('Content-Type', 'text/plain'); 751cb0ef41Sopenharmony_ci res.write(testData); 761cb0ef41Sopenharmony_ci res.end(); 771cb0ef41Sopenharmony_ci }); 781cb0ef41Sopenharmony_ci })); 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci const { clientSide, serverSide } = MakeDuplexPair(); 811cb0ef41Sopenharmony_ci server.emit('connection', serverSide); 821cb0ef41Sopenharmony_ci clientSide.on('end', common.mustCall()); 831cb0ef41Sopenharmony_ci serverSide.on('end', common.mustCall()); 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci const req = http.request({ 861cb0ef41Sopenharmony_ci createConnection: common.mustCall(() => clientSide), 871cb0ef41Sopenharmony_ci method: 'PUT', 881cb0ef41Sopenharmony_ci headers: { 'Connection': 'close' } 891cb0ef41Sopenharmony_ci }, common.mustCall((res) => { 901cb0ef41Sopenharmony_ci res.setEncoding('utf8'); 911cb0ef41Sopenharmony_ci res.on('data', common.mustCall(function test3_res_data(data) { 921cb0ef41Sopenharmony_ci assert.strictEqual(data, testData); 931cb0ef41Sopenharmony_ci })); 941cb0ef41Sopenharmony_ci res.on('end', common.mustCall()); 951cb0ef41Sopenharmony_ci })); 961cb0ef41Sopenharmony_ci req.write(testData); 971cb0ef41Sopenharmony_ci req.end(); 981cb0ef41Sopenharmony_ci} 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ci// Test 4: Connection: close request/response with Content-Length 1011cb0ef41Sopenharmony_ci// The same as Test 3, but with Content-Length headers 1021cb0ef41Sopenharmony_ci{ 1031cb0ef41Sopenharmony_ci const testData = 'Hello, World!\n'; 1041cb0ef41Sopenharmony_ci const server = http.createServer(common.mustCall((req, res) => { 1051cb0ef41Sopenharmony_ci assert.strictEqual(req.headers['content-length'], testData.length + ''); 1061cb0ef41Sopenharmony_ci req.setEncoding('utf8'); 1071cb0ef41Sopenharmony_ci req.on('data', common.mustCall(function test4_req_data(data) { 1081cb0ef41Sopenharmony_ci assert.strictEqual(data, testData); 1091cb0ef41Sopenharmony_ci })); 1101cb0ef41Sopenharmony_ci req.once('end', function() { 1111cb0ef41Sopenharmony_ci res.statusCode = 200; 1121cb0ef41Sopenharmony_ci res.setHeader('Content-Type', 'text/plain'); 1131cb0ef41Sopenharmony_ci res.setHeader('Content-Length', testData.length); 1141cb0ef41Sopenharmony_ci res.write(testData); 1151cb0ef41Sopenharmony_ci res.end(); 1161cb0ef41Sopenharmony_ci }); 1171cb0ef41Sopenharmony_ci 1181cb0ef41Sopenharmony_ci })); 1191cb0ef41Sopenharmony_ci 1201cb0ef41Sopenharmony_ci const { clientSide, serverSide } = MakeDuplexPair(); 1211cb0ef41Sopenharmony_ci server.emit('connection', serverSide); 1221cb0ef41Sopenharmony_ci clientSide.on('end', common.mustCall()); 1231cb0ef41Sopenharmony_ci serverSide.on('end', common.mustCall()); 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ci const req = http.request({ 1261cb0ef41Sopenharmony_ci createConnection: common.mustCall(() => clientSide), 1271cb0ef41Sopenharmony_ci method: 'PUT', 1281cb0ef41Sopenharmony_ci headers: { 'Connection': 'close' } 1291cb0ef41Sopenharmony_ci }, common.mustCall((res) => { 1301cb0ef41Sopenharmony_ci res.setEncoding('utf8'); 1311cb0ef41Sopenharmony_ci assert.strictEqual(res.headers['content-length'], testData.length + ''); 1321cb0ef41Sopenharmony_ci res.on('data', common.mustCall(function test4_res_data(data) { 1331cb0ef41Sopenharmony_ci assert.strictEqual(data, testData); 1341cb0ef41Sopenharmony_ci })); 1351cb0ef41Sopenharmony_ci res.on('end', common.mustCall()); 1361cb0ef41Sopenharmony_ci })); 1371cb0ef41Sopenharmony_ci req.setHeader('Content-Length', testData.length); 1381cb0ef41Sopenharmony_ci req.write(testData); 1391cb0ef41Sopenharmony_ci req.end(); 1401cb0ef41Sopenharmony_ci} 1411cb0ef41Sopenharmony_ci 1421cb0ef41Sopenharmony_ci// Test 5: The client sends garbage. 1431cb0ef41Sopenharmony_ci{ 1441cb0ef41Sopenharmony_ci const server = http.createServer(common.mustNotCall()); 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ci const { clientSide, serverSide } = MakeDuplexPair(); 1471cb0ef41Sopenharmony_ci server.emit('connection', serverSide); 1481cb0ef41Sopenharmony_ci 1491cb0ef41Sopenharmony_ci server.on('clientError', common.mustCall()); 1501cb0ef41Sopenharmony_ci 1511cb0ef41Sopenharmony_ci // Send something that is not an HTTP request. 1521cb0ef41Sopenharmony_ci clientSide.end( 1531cb0ef41Sopenharmony_ci 'I’m reading a book about anti-gravity. It’s impossible to put down!'); 1541cb0ef41Sopenharmony_ci} 155