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_ci
241cb0ef41Sopenharmony_cirequire('../common');
251cb0ef41Sopenharmony_ciconst assert = require('assert');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst net = require('net');
281cb0ef41Sopenharmony_ciconst http = require('http');
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_cilet requests_recv = 0;
321cb0ef41Sopenharmony_cilet requests_sent = 0;
331cb0ef41Sopenharmony_cilet request_upgradeHead = null;
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_cifunction createTestServer() {
361cb0ef41Sopenharmony_ci  return new testServer();
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_cifunction testServer() {
401cb0ef41Sopenharmony_ci  http.Server.call(this, () => {});
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci  this.on('connection', function() {
431cb0ef41Sopenharmony_ci    requests_recv++;
441cb0ef41Sopenharmony_ci  });
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  this.on('request', function(req, res) {
471cb0ef41Sopenharmony_ci    res.writeHead(200, { 'Content-Type': 'text/plain' });
481cb0ef41Sopenharmony_ci    res.write('okay');
491cb0ef41Sopenharmony_ci    res.end();
501cb0ef41Sopenharmony_ci  });
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  this.on('upgrade', function(req, socket, upgradeHead) {
531cb0ef41Sopenharmony_ci    socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n' +
541cb0ef41Sopenharmony_ci                 'Upgrade: WebSocket\r\n' +
551cb0ef41Sopenharmony_ci                 'Connection: Upgrade\r\n' +
561cb0ef41Sopenharmony_ci                 '\r\n\r\n');
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci    request_upgradeHead = upgradeHead;
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci    socket.on('data', function(d) {
611cb0ef41Sopenharmony_ci      const data = d.toString('utf8');
621cb0ef41Sopenharmony_ci      if (data === 'kill') {
631cb0ef41Sopenharmony_ci        socket.end();
641cb0ef41Sopenharmony_ci      } else {
651cb0ef41Sopenharmony_ci        socket.write(data, 'utf8');
661cb0ef41Sopenharmony_ci      }
671cb0ef41Sopenharmony_ci    });
681cb0ef41Sopenharmony_ci  });
691cb0ef41Sopenharmony_ci}
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ciObject.setPrototypeOf(testServer.prototype, http.Server.prototype);
721cb0ef41Sopenharmony_ciObject.setPrototypeOf(testServer, http.Server);
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_cifunction writeReq(socket, data, encoding) {
761cb0ef41Sopenharmony_ci  requests_sent++;
771cb0ef41Sopenharmony_ci  socket.write(data);
781cb0ef41Sopenharmony_ci}
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci// connection: Upgrade with listener
821cb0ef41Sopenharmony_cifunction test_upgrade_with_listener() {
831cb0ef41Sopenharmony_ci  const conn = net.createConnection(server.address().port);
841cb0ef41Sopenharmony_ci  conn.setEncoding('utf8');
851cb0ef41Sopenharmony_ci  let state = 0;
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci  conn.on('connect', function() {
881cb0ef41Sopenharmony_ci    writeReq(conn,
891cb0ef41Sopenharmony_ci             'GET / HTTP/1.1\r\n' +
901cb0ef41Sopenharmony_ci             'Upgrade: WebSocket\r\n' +
911cb0ef41Sopenharmony_ci             'Connection: Upgrade\r\n' +
921cb0ef41Sopenharmony_ci             '\r\n' +
931cb0ef41Sopenharmony_ci             'WjN}|M(6');
941cb0ef41Sopenharmony_ci  });
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci  conn.on('data', function(data) {
971cb0ef41Sopenharmony_ci    state++;
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci    assert.strictEqual(typeof data, 'string');
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci    if (state === 1) {
1021cb0ef41Sopenharmony_ci      assert.strictEqual(data.substr(0, 12), 'HTTP/1.1 101');
1031cb0ef41Sopenharmony_ci      assert.strictEqual(request_upgradeHead.toString('utf8'), 'WjN}|M(6');
1041cb0ef41Sopenharmony_ci      conn.write('test', 'utf8');
1051cb0ef41Sopenharmony_ci    } else if (state === 2) {
1061cb0ef41Sopenharmony_ci      assert.strictEqual(data, 'test');
1071cb0ef41Sopenharmony_ci      conn.write('kill', 'utf8');
1081cb0ef41Sopenharmony_ci    }
1091cb0ef41Sopenharmony_ci  });
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci  conn.on('end', function() {
1121cb0ef41Sopenharmony_ci    assert.strictEqual(state, 2);
1131cb0ef41Sopenharmony_ci    conn.end();
1141cb0ef41Sopenharmony_ci    server.removeAllListeners('upgrade');
1151cb0ef41Sopenharmony_ci    test_upgrade_no_listener();
1161cb0ef41Sopenharmony_ci  });
1171cb0ef41Sopenharmony_ci}
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci// connection: Upgrade, no listener
1201cb0ef41Sopenharmony_cifunction test_upgrade_no_listener() {
1211cb0ef41Sopenharmony_ci  const conn = net.createConnection(server.address().port);
1221cb0ef41Sopenharmony_ci  conn.setEncoding('utf8');
1231cb0ef41Sopenharmony_ci
1241cb0ef41Sopenharmony_ci  conn.on('connect', function() {
1251cb0ef41Sopenharmony_ci    writeReq(conn,
1261cb0ef41Sopenharmony_ci             'GET / HTTP/1.1\r\n' +
1271cb0ef41Sopenharmony_ci             'Upgrade: WebSocket\r\n' +
1281cb0ef41Sopenharmony_ci             'Connection: Upgrade\r\n' +
1291cb0ef41Sopenharmony_ci             '\r\n');
1301cb0ef41Sopenharmony_ci  });
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ci  conn.once('data', (data) => {
1331cb0ef41Sopenharmony_ci    assert.strictEqual(typeof data, 'string');
1341cb0ef41Sopenharmony_ci    assert.strictEqual(data.substr(0, 12), 'HTTP/1.1 200');
1351cb0ef41Sopenharmony_ci    conn.end();
1361cb0ef41Sopenharmony_ci  });
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci  conn.on('close', function() {
1391cb0ef41Sopenharmony_ci    test_standard_http();
1401cb0ef41Sopenharmony_ci  });
1411cb0ef41Sopenharmony_ci}
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci// connection: normal
1441cb0ef41Sopenharmony_cifunction test_standard_http() {
1451cb0ef41Sopenharmony_ci  const conn = net.createConnection(server.address().port);
1461cb0ef41Sopenharmony_ci  conn.setEncoding('utf8');
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci  conn.on('connect', function() {
1491cb0ef41Sopenharmony_ci    writeReq(conn, 'GET / HTTP/1.1\r\n\r\n');
1501cb0ef41Sopenharmony_ci  });
1511cb0ef41Sopenharmony_ci
1521cb0ef41Sopenharmony_ci  conn.once('data', function(data) {
1531cb0ef41Sopenharmony_ci    assert.strictEqual(typeof data, 'string');
1541cb0ef41Sopenharmony_ci    assert.strictEqual(data.substr(0, 12), 'HTTP/1.1 200');
1551cb0ef41Sopenharmony_ci    conn.end();
1561cb0ef41Sopenharmony_ci  });
1571cb0ef41Sopenharmony_ci
1581cb0ef41Sopenharmony_ci  conn.on('close', function() {
1591cb0ef41Sopenharmony_ci    server.close();
1601cb0ef41Sopenharmony_ci  });
1611cb0ef41Sopenharmony_ci}
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ciconst server = createTestServer();
1651cb0ef41Sopenharmony_ci
1661cb0ef41Sopenharmony_ciserver.listen(0, function() {
1671cb0ef41Sopenharmony_ci  // All tests get chained after this:
1681cb0ef41Sopenharmony_ci  test_upgrade_with_listener();
1691cb0ef41Sopenharmony_ci});
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci// Fin.
1731cb0ef41Sopenharmony_ciprocess.on('exit', function() {
1741cb0ef41Sopenharmony_ci  assert.strictEqual(requests_recv, 3);
1751cb0ef41Sopenharmony_ci  assert.strictEqual(requests_sent, 3);
1761cb0ef41Sopenharmony_ci});
177