• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3
4// This test ensures that the http-parser strips leading and trailing OWS from
5// header values. It sends the header values in chunks to force the parser to
6// build the string up through multiple calls to on_header_value().
7
8const assert = require('assert');
9const http = require('http');
10const net = require('net');
11
12function check(hdr, snd, rcv) {
13  const server = http.createServer(common.mustCall((req, res) => {
14    assert.strictEqual(req.headers[hdr], rcv);
15    req.pipe(res);
16  }));
17
18  server.listen(0, common.mustCall(function() {
19    const client = net.connect(this.address().port, start);
20    function start() {
21      client.write('GET / HTTP/1.1\r\n' + hdr + ':', drain);
22    }
23
24    function drain() {
25      if (snd.length === 0) {
26        return client.write('\r\nConnection: close\r\n\r\n');
27      }
28      client.write(snd.shift(), drain);
29    }
30
31    const bufs = [];
32    client.on('data', function(chunk) {
33      bufs.push(chunk);
34    });
35    client.on('end', common.mustCall(function() {
36      const head = Buffer.concat(bufs)
37        .toString('latin1')
38        .split('\r\n')[0];
39      assert.strictEqual(head, 'HTTP/1.1 200 OK');
40      server.close();
41    }));
42  }));
43}
44
45check('host', [' \t foo.com\t'], 'foo.com');
46check('host', [' \t foo\tcom\t'], 'foo\tcom');
47check('host', [' \t', ' ', ' foo.com\t', '\t '], 'foo.com');
48check('host', [' \t', ' \t'.repeat(100), '\t '], '');
49check('host', [' \t', ' - - - -   ', '\t '], '- - - -');
50