• 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');
27
28// RFC 2616, section 10.2.5:
29//
30//   The 204 response MUST NOT contain a message-body, and thus is always
31//   terminated by the first empty line after the header fields.
32//
33// Likewise for 304 responses. Verify that no empty chunk is sent when
34// the user explicitly sets a Transfer-Encoding header.
35
36test(204);
37test(304);
38
39function test(statusCode) {
40  const server = http.createServer(common.mustCall((req, res) => {
41    res.writeHead(statusCode, { 'Transfer-Encoding': 'chunked' });
42    res.end();
43    server.close();
44  }));
45
46  server.listen(0, common.mustCall(() => {
47    const conn = net.createConnection(
48      server.address().port,
49      common.mustCall(() => {
50        conn.write('GET / HTTP/1.1\r\n\r\n');
51
52        let resp = '';
53        conn.setEncoding('utf8');
54        conn.on('data', common.mustCall((data) => {
55          resp += data;
56        }));
57
58        conn.on('end', common.mustCall(() => {
59          // Connection: close should be in the response
60          assert.strictEqual(/^Connection: close\r\n$/m.test(resp), true);
61          // Make sure this doesn't end with 0\r\n\r\n
62          assert.strictEqual(/^0\r\n$/m.test(resp), false);
63        }));
64      })
65    );
66  }));
67}
68