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_ciconst assert = require('assert');
251cb0ef41Sopenharmony_ciconst net = require('net');
261cb0ef41Sopenharmony_ciconst http = require('http');
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciconst body = 'hello world\n';
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_cifunction test(handler, request_generator, response_validator) {
311cb0ef41Sopenharmony_ci  const server = http.createServer(handler);
321cb0ef41Sopenharmony_ci
331cb0ef41Sopenharmony_ci  let client_got_eof = false;
341cb0ef41Sopenharmony_ci  let server_response = '';
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_ci  server.listen(0);
371cb0ef41Sopenharmony_ci  server.on('listening', function() {
381cb0ef41Sopenharmony_ci    const c = net.createConnection(this.address().port);
391cb0ef41Sopenharmony_ci
401cb0ef41Sopenharmony_ci    c.setEncoding('utf8');
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci    c.on('connect', function() {
431cb0ef41Sopenharmony_ci      c.write(request_generator());
441cb0ef41Sopenharmony_ci    });
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci    c.on('data', function(chunk) {
471cb0ef41Sopenharmony_ci      server_response += chunk;
481cb0ef41Sopenharmony_ci    });
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci    c.on('end', common.mustCall(function() {
511cb0ef41Sopenharmony_ci      client_got_eof = true;
521cb0ef41Sopenharmony_ci      c.end();
531cb0ef41Sopenharmony_ci      server.close();
541cb0ef41Sopenharmony_ci      response_validator(server_response, client_got_eof, false);
551cb0ef41Sopenharmony_ci    }));
561cb0ef41Sopenharmony_ci  });
571cb0ef41Sopenharmony_ci}
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci{
601cb0ef41Sopenharmony_ci  function handler(req, res) {
611cb0ef41Sopenharmony_ci    assert.strictEqual(req.httpVersion, '1.0');
621cb0ef41Sopenharmony_ci    assert.strictEqual(req.httpVersionMajor, 1);
631cb0ef41Sopenharmony_ci    assert.strictEqual(req.httpVersionMinor, 0);
641cb0ef41Sopenharmony_ci    res.writeHead(200, { 'Content-Type': 'text/plain' });
651cb0ef41Sopenharmony_ci    res.end(body);
661cb0ef41Sopenharmony_ci  }
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  function request_generator() {
691cb0ef41Sopenharmony_ci    return 'GET / HTTP/1.0\r\n\r\n';
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  function response_validator(server_response, client_got_eof, timed_out) {
731cb0ef41Sopenharmony_ci    const m = server_response.split('\r\n\r\n');
741cb0ef41Sopenharmony_ci    assert.strictEqual(m[1], body);
751cb0ef41Sopenharmony_ci    assert.strictEqual(client_got_eof, true);
761cb0ef41Sopenharmony_ci    assert.strictEqual(timed_out, false);
771cb0ef41Sopenharmony_ci  }
781cb0ef41Sopenharmony_ci
791cb0ef41Sopenharmony_ci  test(handler, request_generator, response_validator);
801cb0ef41Sopenharmony_ci}
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci//
831cb0ef41Sopenharmony_ci// Don't send HTTP/1.1 status lines to HTTP/1.0 clients.
841cb0ef41Sopenharmony_ci//
851cb0ef41Sopenharmony_ci// https://github.com/joyent/node/issues/1234
861cb0ef41Sopenharmony_ci//
871cb0ef41Sopenharmony_ci{
881cb0ef41Sopenharmony_ci  function handler(req, res) {
891cb0ef41Sopenharmony_ci    assert.strictEqual(req.httpVersion, '1.0');
901cb0ef41Sopenharmony_ci    assert.strictEqual(req.httpVersionMajor, 1);
911cb0ef41Sopenharmony_ci    assert.strictEqual(req.httpVersionMinor, 0);
921cb0ef41Sopenharmony_ci    res.sendDate = false;
931cb0ef41Sopenharmony_ci    res.writeHead(200, { 'Content-Type': 'text/plain' });
941cb0ef41Sopenharmony_ci    res.write('Hello, '); res._send('');
951cb0ef41Sopenharmony_ci    res.write('world!'); res._send('');
961cb0ef41Sopenharmony_ci    res.end();
971cb0ef41Sopenharmony_ci  }
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci  function request_generator() {
1001cb0ef41Sopenharmony_ci    return ('GET / HTTP/1.0\r\n' +
1011cb0ef41Sopenharmony_ci        'User-Agent: curl/7.19.7 (x86_64-pc-linux-gnu) libcurl/7.19.7 ' +
1021cb0ef41Sopenharmony_ci        'OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15\r\n' +
1031cb0ef41Sopenharmony_ci        'Host: 127.0.0.1:1337\r\n' +
1041cb0ef41Sopenharmony_ci        'Accept: */*\r\n' +
1051cb0ef41Sopenharmony_ci        '\r\n');
1061cb0ef41Sopenharmony_ci  }
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci  function response_validator(server_response, client_got_eof, timed_out) {
1091cb0ef41Sopenharmony_ci    const expected_response = 'HTTP/1.1 200 OK\r\n' +
1101cb0ef41Sopenharmony_ci                              'Content-Type: text/plain\r\n' +
1111cb0ef41Sopenharmony_ci                              'Connection: close\r\n' +
1121cb0ef41Sopenharmony_ci                              '\r\n' +
1131cb0ef41Sopenharmony_ci                              'Hello, world!';
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci    assert.strictEqual(server_response, expected_response);
1161cb0ef41Sopenharmony_ci    assert.strictEqual(client_got_eof, true);
1171cb0ef41Sopenharmony_ci    assert.strictEqual(timed_out, false);
1181cb0ef41Sopenharmony_ci  }
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci  test(handler, request_generator, response_validator);
1211cb0ef41Sopenharmony_ci}
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci{
1241cb0ef41Sopenharmony_ci  function handler(req, res) {
1251cb0ef41Sopenharmony_ci    assert.strictEqual(req.httpVersion, '1.1');
1261cb0ef41Sopenharmony_ci    assert.strictEqual(req.httpVersionMajor, 1);
1271cb0ef41Sopenharmony_ci    assert.strictEqual(req.httpVersionMinor, 1);
1281cb0ef41Sopenharmony_ci    res.sendDate = false;
1291cb0ef41Sopenharmony_ci    res.writeHead(200, { 'Content-Type': 'text/plain' });
1301cb0ef41Sopenharmony_ci    res.write('Hello, '); res._send('');
1311cb0ef41Sopenharmony_ci    res.write('world!'); res._send('');
1321cb0ef41Sopenharmony_ci    res.end();
1331cb0ef41Sopenharmony_ci  }
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci  function request_generator() {
1361cb0ef41Sopenharmony_ci    return 'GET / HTTP/1.1\r\n' +
1371cb0ef41Sopenharmony_ci        'User-Agent: curl/7.19.7 (x86_64-pc-linux-gnu) libcurl/7.19.7 ' +
1381cb0ef41Sopenharmony_ci        'OpenSSL/0.9.8k zlib/1.2.3.3 libidn/1.15\r\n' +
1391cb0ef41Sopenharmony_ci        'Connection: close\r\n' +
1401cb0ef41Sopenharmony_ci        'Host: 127.0.0.1:1337\r\n' +
1411cb0ef41Sopenharmony_ci        'Accept: */*\r\n' +
1421cb0ef41Sopenharmony_ci        '\r\n';
1431cb0ef41Sopenharmony_ci  }
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ci  function response_validator(server_response, client_got_eof, timed_out) {
1461cb0ef41Sopenharmony_ci    const expected_response = 'HTTP/1.1 200 OK\r\n' +
1471cb0ef41Sopenharmony_ci                              'Content-Type: text/plain\r\n' +
1481cb0ef41Sopenharmony_ci                              'Connection: close\r\n' +
1491cb0ef41Sopenharmony_ci                              'Transfer-Encoding: chunked\r\n' +
1501cb0ef41Sopenharmony_ci                              '\r\n' +
1511cb0ef41Sopenharmony_ci                              '7\r\n' +
1521cb0ef41Sopenharmony_ci                              'Hello, \r\n' +
1531cb0ef41Sopenharmony_ci                              '6\r\n' +
1541cb0ef41Sopenharmony_ci                              'world!\r\n' +
1551cb0ef41Sopenharmony_ci                              '0\r\n' +
1561cb0ef41Sopenharmony_ci                              '\r\n';
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci    assert.strictEqual(server_response, expected_response);
1591cb0ef41Sopenharmony_ci    assert.strictEqual(client_got_eof, true);
1601cb0ef41Sopenharmony_ci    assert.strictEqual(timed_out, false);
1611cb0ef41Sopenharmony_ci  }
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci  test(handler, request_generator, response_validator);
1641cb0ef41Sopenharmony_ci}
165