• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const assert = require('assert');
25const http = require('http');
26const net = require('net');
27const Countdown = require('../common/countdown');
28
29const testCases = [
30  { path: '/200', statusMessage: 'OK',
31    response: 'HTTP/1.1 200 OK\r\n\r\n' },
32  { path: '/500', statusMessage: 'Internal Server Error',
33    response: 'HTTP/1.1 500 Internal Server Error\r\n\r\n' },
34  { path: '/302', statusMessage: 'Moved Temporarily',
35    response: 'HTTP/1.1 302 Moved Temporarily\r\n\r\n' },
36  { path: '/missing', statusMessage: '',
37    response: 'HTTP/1.1 200 \r\n\r\n' },
38  { path: '/missing-no-space', statusMessage: '',
39    response: 'HTTP/1.1 200\r\n\r\n' },
40];
41testCases.findByPath = function(path) {
42  const matching = this.filter(function(testCase) {
43    return testCase.path === path;
44  });
45  if (matching.length === 0) {
46    assert.fail(`failed to find test case with path ${path}`);
47  }
48  return matching[0];
49};
50
51const server = net.createServer(function(connection) {
52  connection.on('data', function(data) {
53    const path = data.toString().match(/GET (.*) HTTP\/1\.1/)[1];
54    const testCase = testCases.findByPath(path);
55
56    connection.write(testCase.response);
57    connection.end();
58  });
59});
60
61const countdown = new Countdown(testCases.length, () => server.close());
62
63function runTest(testCaseIndex) {
64  const testCase = testCases[testCaseIndex];
65
66  http.get({
67    port: server.address().port,
68    path: testCase.path
69  }, function(response) {
70    console.log(`client: expected status message: ${testCase.statusMessage}`);
71    console.log(`client: actual status message: ${response.statusMessage}`);
72    assert.strictEqual(testCase.statusMessage, response.statusMessage);
73
74    response.on('aborted', common.mustNotCall());
75    response.on('end', function() {
76      countdown.dec();
77      if (testCaseIndex + 1 < testCases.length) {
78        runTest(testCaseIndex + 1);
79      }
80    });
81
82    response.resume();
83  });
84}
85
86server.listen(0, function() { runTest(0); });
87