11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst assert = require('assert');
61cb0ef41Sopenharmony_ciconst http = require('http');
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_cifunction execute(options) {
91cb0ef41Sopenharmony_ci  http.createServer(function(req, res) {
101cb0ef41Sopenharmony_ci    const expectHeaders = {
111cb0ef41Sopenharmony_ci      'x-foo': 'boom',
121cb0ef41Sopenharmony_ci      'cookie': 'a=1; b=2; c=3',
131cb0ef41Sopenharmony_ci      'connection': 'close'
141cb0ef41Sopenharmony_ci    };
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci    // no Host header when you set headers an array
171cb0ef41Sopenharmony_ci    if (!Array.isArray(options.headers)) {
181cb0ef41Sopenharmony_ci      expectHeaders.host = `localhost:${this.address().port}`;
191cb0ef41Sopenharmony_ci    }
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci    // no Authorization header when you set headers an array
221cb0ef41Sopenharmony_ci    if (options.auth && !Array.isArray(options.headers)) {
231cb0ef41Sopenharmony_ci      expectHeaders.authorization =
241cb0ef41Sopenharmony_ci          `Basic ${Buffer.from(options.auth).toString('base64')}`;
251cb0ef41Sopenharmony_ci    }
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci    this.close();
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci    assert.deepStrictEqual(req.headers, expectHeaders);
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci    res.end();
321cb0ef41Sopenharmony_ci  }).listen(0, function() {
331cb0ef41Sopenharmony_ci    options = Object.assign(options, {
341cb0ef41Sopenharmony_ci      port: this.address().port,
351cb0ef41Sopenharmony_ci      path: '/'
361cb0ef41Sopenharmony_ci    });
371cb0ef41Sopenharmony_ci    const req = http.request(options);
381cb0ef41Sopenharmony_ci    req.end();
391cb0ef41Sopenharmony_ci  });
401cb0ef41Sopenharmony_ci}
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ci// Should be the same except for implicit Host header on the first two
431cb0ef41Sopenharmony_ciexecute({ headers: { 'x-foo': 'boom', 'cookie': 'a=1; b=2; c=3' } });
441cb0ef41Sopenharmony_ciexecute({ headers: { 'x-foo': 'boom', 'cookie': [ 'a=1', 'b=2', 'c=3' ] } });
451cb0ef41Sopenharmony_ciexecute({ headers: [[ 'x-foo', 'boom' ], [ 'cookie', 'a=1; b=2; c=3' ]] });
461cb0ef41Sopenharmony_ciexecute({ headers: [
471cb0ef41Sopenharmony_ci  [ 'x-foo', 'boom' ], [ 'cookie', [ 'a=1', 'b=2', 'c=3' ]],
481cb0ef41Sopenharmony_ci] });
491cb0ef41Sopenharmony_ciexecute({ headers: [
501cb0ef41Sopenharmony_ci  [ 'x-foo', 'boom' ], [ 'cookie', 'a=1' ],
511cb0ef41Sopenharmony_ci  [ 'cookie', 'b=2' ], [ 'cookie', 'c=3'],
521cb0ef41Sopenharmony_ci] });
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci// Authorization and Host header both missing from the second
551cb0ef41Sopenharmony_ciexecute({ auth: 'foo:bar', headers:
561cb0ef41Sopenharmony_ci  { 'x-foo': 'boom', 'cookie': 'a=1; b=2; c=3' } });
571cb0ef41Sopenharmony_ciexecute({ auth: 'foo:bar', headers: [
581cb0ef41Sopenharmony_ci  [ 'x-foo', 'boom' ], [ 'cookie', 'a=1' ],
591cb0ef41Sopenharmony_ci  [ 'cookie', 'b=2' ], [ 'cookie', 'c=3'],
601cb0ef41Sopenharmony_ci] });
61