summaryrefslogtreecommitdiffstats
path: root/netwerk/test/httpserver/test/test_host_identity.js
blob: 1a1662d8cf7a1a6fb7accaa7cced2712380ddba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**
 * Tests that the server accepts requests to custom host names.
 * This is commonly used in tests that map custom host names to the server via
 * a proxy e.g. by XPCShellContentUtils.createHttpServer.
 */

var srv = createServer();
srv.start(-1);
registerCleanupFunction(() => new Promise(resolve => srv.stop(resolve)));
const PORT = srv.identity.primaryPort;
srv.registerPathHandler("/dump-request", dumpRequestLines);

function dumpRequestLines(request, response) {
  writeDetails(request, response);
  response.setStatusLine(request.httpVersion, 200, "TEST PASSED");
}

function makeRawRequest(requestLinePath, hostHeader) {
  return `GET ${requestLinePath} HTTP/1.1\r\nHost: ${hostHeader}\r\n\r\n`;
}

function verifyResponseHostPort(data, query, expectedHost, expectedPort) {
  var iter = LineIterator(data);

  // Status-Line
  Assert.equal(iter.next().value, "HTTP/1.1 200 TEST PASSED");

  skipHeaders(iter);

  // Okay, next line must be the data we expected to be written
  var body = [
    "Method:  GET",
    "Path:    /dump-request",
    "Query:   " + query,
    "Version: 1.1",
    "Scheme:  http",
    "Host:    " + expectedHost,
    "Port:    " + expectedPort,
  ];

  expectLines(iter, body);
}

function runIdentityTest(host, port) {
  srv.identity.add("http", host, port);

  function checkAbsoluteRequestURI(data) {
    verifyResponseHostPort(data, "absolute", host, port);
  }
  function checkHostHeader(data) {
    verifyResponseHostPort(data, "relative", host, port);
  }

  let tests = [];
  let test, data;
  let hostport = `${host}:${port}`;
  data = makeRawRequest(`http://${hostport}/dump-request?absolute`, hostport);
  test = new RawTest("localhost", PORT, data, checkAbsoluteRequestURI);
  tests.push(test);

  data = makeRawRequest("/dump-request?relative", hostport);
  test = new RawTest("localhost", PORT, data, checkHostHeader);
  tests.push(test);
  return new Promise(resolve => {
    runRawTests(tests, resolve);
  });
}

/** *************
 * BEGIN TESTS *
 ***************/

add_task(async function test_basic_example_com() {
  await runIdentityTest("example.com", 1234);
  await runIdentityTest("example.com", 5432);
});

add_task(async function test_fully_qualified_domain_name_aka_fqdn() {
  await runIdentityTest("fully-qualified-domain-name.", 1234);
});

add_task(async function test_ipv4() {
  await runIdentityTest("1.2.3.4", 1234);
});

add_task(async function test_ipv6() {
  Assert.throws(
    () => srv.identity.add("http", "[notipv6]", 1234),
    /NS_ERROR_ILLEGAL_VALUE/,
    "should reject invalid host, clearly not bracketed IPv6"
  );
  Assert.throws(
    () => srv.identity.add("http", "[::127.0.0.1]", 1234),
    /NS_ERROR_ILLEGAL_VALUE/,
    "should reject non-canonical IPv6"
  );
  await runIdentityTest("[::123]", 1234);
  await runIdentityTest("[1:2:3:a:b:c:d:abcd]", 1234);
});

add_task(async function test_internationalized_domain_name() {
  Assert.throws(
    () => srv.identity.add("http", "δοκιμή", 1234),
    /NS_ERROR_ILLEGAL_VALUE/,
    "should reject IDN not in punycode"
  );

  await runIdentityTest("xn--jxalpdlp", 1234);
});