• 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');
26
27const { once } = require('events');
28
29const expectedHeaders = {
30  'DELETE': ['host', 'connection'],
31  'GET': ['host', 'connection'],
32  'HEAD': ['host', 'connection'],
33  'OPTIONS': ['host', 'connection'],
34  'POST': ['host', 'connection', 'content-length'],
35  'PUT': ['host', 'connection', 'content-length'],
36  'TRACE': ['host', 'connection']
37};
38
39const expectedMethods = Object.keys(expectedHeaders);
40
41const server = http.createServer(common.mustCall((req, res) => {
42  res.end();
43
44  assert(expectedHeaders.hasOwnProperty(req.method),
45         `${req.method} was an unexpected method`);
46
47  const requestHeaders = Object.keys(req.headers);
48  requestHeaders.forEach((header) => {
49    assert.ok(
50      expectedHeaders[req.method].includes(header.toLowerCase()),
51      `${header} should not exist for method ${req.method}`
52    );
53  });
54
55  assert.strictEqual(
56    requestHeaders.length,
57    expectedHeaders[req.method].length,
58    `some headers were missing for method: ${req.method}`
59  );
60}, expectedMethods.length));
61
62server.listen(0, common.mustCall(() => {
63  Promise.all(expectedMethods.map(async (method) => {
64    const request = http.request({
65      method: method,
66      port: server.address().port
67    }).end();
68    return once(request, 'response');
69  })).then(common.mustCall(() => { server.close(); }));
70}));
71