1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const http = require('http'); 5const MakeDuplexPair = require('../common/duplexpair'); 6 7// Test 1: Simple HTTP test, no keep-alive. 8{ 9 const testData = 'Hello, World!\n'; 10 const server = http.createServer(common.mustCall((req, res) => { 11 res.statusCode = 200; 12 res.setHeader('Content-Type', 'text/plain'); 13 res.end(testData); 14 })); 15 16 const { clientSide, serverSide } = MakeDuplexPair(); 17 server.emit('connection', serverSide); 18 19 const req = http.request({ 20 createConnection: common.mustCall(() => clientSide) 21 }, common.mustCall((res) => { 22 res.setEncoding('utf8'); 23 res.on('data', common.mustCall((data) => { 24 assert.strictEqual(data, testData); 25 })); 26 res.on('end', common.mustCall()); 27 })); 28 req.end(); 29} 30 31// Test 2: Keep-alive for 2 requests. 32{ 33 const testData = 'Hello, World!\n'; 34 const server = http.createServer(common.mustCall((req, res) => { 35 res.statusCode = 200; 36 res.setHeader('Content-Type', 'text/plain'); 37 res.end(testData); 38 }, 2)); 39 40 const { clientSide, serverSide } = MakeDuplexPair(); 41 server.emit('connection', serverSide); 42 43 function doRequest(cb) { 44 const req = http.request({ 45 createConnection: common.mustCall(() => clientSide), 46 headers: { Connection: 'keep-alive' } 47 }, common.mustCall((res) => { 48 res.setEncoding('utf8'); 49 res.on('data', common.mustCall((data) => { 50 assert.strictEqual(data, testData); 51 })); 52 res.on('end', common.mustCall(cb)); 53 })); 54 req.shouldKeepAlive = true; 55 req.end(); 56 } 57 58 doRequest(() => { 59 doRequest(); 60 }); 61} 62 63// Test 3: Connection: close request/response with chunked 64{ 65 const testData = 'Hello, World!\n'; 66 const server = http.createServer(common.mustCall((req, res) => { 67 req.setEncoding('utf8'); 68 req.resume(); 69 req.on('data', common.mustCall(function test3_req_data(data) { 70 assert.strictEqual(data, testData); 71 })); 72 req.once('end', function() { 73 res.statusCode = 200; 74 res.setHeader('Content-Type', 'text/plain'); 75 res.write(testData); 76 res.end(); 77 }); 78 })); 79 80 const { clientSide, serverSide } = MakeDuplexPair(); 81 server.emit('connection', serverSide); 82 clientSide.on('end', common.mustCall()); 83 serverSide.on('end', common.mustCall()); 84 85 const req = http.request({ 86 createConnection: common.mustCall(() => clientSide), 87 method: 'PUT', 88 headers: { 'Connection': 'close' } 89 }, common.mustCall((res) => { 90 res.setEncoding('utf8'); 91 res.on('data', common.mustCall(function test3_res_data(data) { 92 assert.strictEqual(data, testData); 93 })); 94 res.on('end', common.mustCall()); 95 })); 96 req.write(testData); 97 req.end(); 98} 99 100// Test 4: Connection: close request/response with Content-Length 101// The same as Test 3, but with Content-Length headers 102{ 103 const testData = 'Hello, World!\n'; 104 const server = http.createServer(common.mustCall((req, res) => { 105 assert.strictEqual(req.headers['content-length'], testData.length + ''); 106 req.setEncoding('utf8'); 107 req.on('data', common.mustCall(function test4_req_data(data) { 108 assert.strictEqual(data, testData); 109 })); 110 req.once('end', function() { 111 res.statusCode = 200; 112 res.setHeader('Content-Type', 'text/plain'); 113 res.setHeader('Content-Length', testData.length); 114 res.write(testData); 115 res.end(); 116 }); 117 118 })); 119 120 const { clientSide, serverSide } = MakeDuplexPair(); 121 server.emit('connection', serverSide); 122 clientSide.on('end', common.mustCall()); 123 serverSide.on('end', common.mustCall()); 124 125 const req = http.request({ 126 createConnection: common.mustCall(() => clientSide), 127 method: 'PUT', 128 headers: { 'Connection': 'close' } 129 }, common.mustCall((res) => { 130 res.setEncoding('utf8'); 131 assert.strictEqual(res.headers['content-length'], testData.length + ''); 132 res.on('data', common.mustCall(function test4_res_data(data) { 133 assert.strictEqual(data, testData); 134 })); 135 res.on('end', common.mustCall()); 136 })); 137 req.setHeader('Content-Length', testData.length); 138 req.write(testData); 139 req.end(); 140} 141 142// Test 5: The client sends garbage. 143{ 144 const server = http.createServer(common.mustNotCall()); 145 146 const { clientSide, serverSide } = MakeDuplexPair(); 147 server.emit('connection', serverSide); 148 149 server.on('clientError', common.mustCall()); 150 151 // Send something that is not an HTTP request. 152 clientSide.end( 153 'I’m reading a book about anti-gravity. It’s impossible to put down!'); 154} 155