11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst common = require('../common');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_ci// This test ensures that the http-parser strips leading and trailing OWS from
51cb0ef41Sopenharmony_ci// header values. It sends the header values in chunks to force the parser to
61cb0ef41Sopenharmony_ci// build the string up through multiple calls to on_header_value().
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ciconst assert = require('assert');
91cb0ef41Sopenharmony_ciconst http = require('http');
101cb0ef41Sopenharmony_ciconst net = require('net');
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cifunction check(hdr, snd, rcv) {
131cb0ef41Sopenharmony_ci  const server = http.createServer(common.mustCall((req, res) => {
141cb0ef41Sopenharmony_ci    assert.strictEqual(req.headers[hdr], rcv);
151cb0ef41Sopenharmony_ci    req.pipe(res);
161cb0ef41Sopenharmony_ci  }));
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  server.listen(0, common.mustCall(function() {
191cb0ef41Sopenharmony_ci    const client = net.connect(this.address().port, start);
201cb0ef41Sopenharmony_ci    function start() {
211cb0ef41Sopenharmony_ci      client.write('GET / HTTP/1.1\r\n' + hdr + ':', drain);
221cb0ef41Sopenharmony_ci    }
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci    function drain() {
251cb0ef41Sopenharmony_ci      if (snd.length === 0) {
261cb0ef41Sopenharmony_ci        return client.write('\r\nConnection: close\r\n\r\n');
271cb0ef41Sopenharmony_ci      }
281cb0ef41Sopenharmony_ci      client.write(snd.shift(), drain);
291cb0ef41Sopenharmony_ci    }
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    const bufs = [];
321cb0ef41Sopenharmony_ci    client.on('data', function(chunk) {
331cb0ef41Sopenharmony_ci      bufs.push(chunk);
341cb0ef41Sopenharmony_ci    });
351cb0ef41Sopenharmony_ci    client.on('end', common.mustCall(function() {
361cb0ef41Sopenharmony_ci      const head = Buffer.concat(bufs)
371cb0ef41Sopenharmony_ci        .toString('latin1')
381cb0ef41Sopenharmony_ci        .split('\r\n')[0];
391cb0ef41Sopenharmony_ci      assert.strictEqual(head, 'HTTP/1.1 200 OK');
401cb0ef41Sopenharmony_ci      server.close();
411cb0ef41Sopenharmony_ci    }));
421cb0ef41Sopenharmony_ci  }));
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_cicheck('host', [' \t foo.com\t'], 'foo.com');
461cb0ef41Sopenharmony_cicheck('host', [' \t foo\tcom\t'], 'foo\tcom');
471cb0ef41Sopenharmony_cicheck('host', [' \t', ' ', ' foo.com\t', '\t '], 'foo.com');
481cb0ef41Sopenharmony_cicheck('host', [' \t', ' \t'.repeat(100), '\t '], '');
491cb0ef41Sopenharmony_cicheck('host', [' \t', ' - - - -   ', '\t '], '- - - -');
50