11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// When using the object form of http.request and using an IPv6 address
41cb0ef41Sopenharmony_ci// as a hostname, and using a non-standard port, the Host header
51cb0ef41Sopenharmony_ci// is improperly formatted.
61cb0ef41Sopenharmony_ci// Issue: https://github.com/nodejs/node/issues/5308
71cb0ef41Sopenharmony_ci// As per https://tools.ietf.org/html/rfc7230#section-5.4 and
81cb0ef41Sopenharmony_ci// https://tools.ietf.org/html/rfc3986#section-3.2.2
91cb0ef41Sopenharmony_ci// the IPv6 address should be enclosed in square brackets
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst common = require('../common');
121cb0ef41Sopenharmony_ciconst assert = require('assert');
131cb0ef41Sopenharmony_ciconst http = require('http');
141cb0ef41Sopenharmony_ciconst net = require('net');
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ciconst requests = [
171cb0ef41Sopenharmony_ci  { host: 'foo:1234', headers: { expectedhost: 'foo:1234:80' } },
181cb0ef41Sopenharmony_ci  { host: '::1', headers: { expectedhost: '[::1]:80' } },
191cb0ef41Sopenharmony_ci];
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_cifunction createLocalConnection(options) {
221cb0ef41Sopenharmony_ci  options.host = undefined;
231cb0ef41Sopenharmony_ci  options.port = this.port;
241cb0ef41Sopenharmony_ci  options.path = undefined;
251cb0ef41Sopenharmony_ci  return net.createConnection(options);
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_cihttp.createServer(common.mustCall(function(req, res) {
291cb0ef41Sopenharmony_ci  this.requests = this.requests || 0;
301cb0ef41Sopenharmony_ci  assert.strictEqual(req.headers.host, req.headers.expectedhost);
311cb0ef41Sopenharmony_ci  res.end();
321cb0ef41Sopenharmony_ci  if (++this.requests === requests.length)
331cb0ef41Sopenharmony_ci    this.close();
341cb0ef41Sopenharmony_ci}, requests.length)).listen(0, function() {
351cb0ef41Sopenharmony_ci  const address = this.address();
361cb0ef41Sopenharmony_ci  for (let i = 0; i < requests.length; ++i) {
371cb0ef41Sopenharmony_ci    requests[i].createConnection =
381cb0ef41Sopenharmony_ci      common.mustCall(createLocalConnection.bind(address));
391cb0ef41Sopenharmony_ci    http.get(requests[i]);
401cb0ef41Sopenharmony_ci  }
411cb0ef41Sopenharmony_ci});
42