• 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 http = require('http');
25const assert = require('assert');
26const httpServer = http.createServer(reqHandler);
27
28function reqHandler(req, res) {
29  if (req.url === '/setHostFalse5') {
30    assert.strictEqual(req.headers.host, undefined);
31  } else {
32    assert.strictEqual(
33      req.headers.host, `localhost:${this.address().port}`,
34      `Wrong host header for req[${req.url}]: ${req.headers.host}`);
35  }
36  res.writeHead(200, {});
37  res.end('ok');
38}
39
40testHttp();
41
42function testHttp() {
43
44  let counter = 0;
45
46  function cb(res) {
47    counter--;
48    if (counter === 0) {
49      httpServer.close();
50    }
51    res.resume();
52  }
53
54  httpServer.listen(0, (er) => {
55    assert.ifError(er);
56    http.get({
57      method: 'GET',
58      path: `/${counter++}`,
59      host: 'localhost',
60      port: httpServer.address().port,
61      rejectUnauthorized: false
62    }, cb).on('error', common.mustNotCall());
63
64    http.request({
65      method: 'GET',
66      path: `/${counter++}`,
67      host: 'localhost',
68      port: httpServer.address().port,
69      rejectUnauthorized: false
70    }, cb).on('error', common.mustNotCall()).end();
71
72    http.request({
73      method: 'POST',
74      path: `/${counter++}`,
75      host: 'localhost',
76      port: httpServer.address().port,
77      rejectUnauthorized: false
78    }, cb).on('error', common.mustNotCall()).end();
79
80    http.request({
81      method: 'PUT',
82      path: `/${counter++}`,
83      host: 'localhost',
84      port: httpServer.address().port,
85      rejectUnauthorized: false
86    }, cb).on('error', common.mustNotCall()).end();
87
88    http.request({
89      method: 'DELETE',
90      path: `/${counter++}`,
91      host: 'localhost',
92      port: httpServer.address().port,
93      rejectUnauthorized: false
94    }, cb).on('error', common.mustNotCall()).end();
95  });
96}
97