summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_http_headers.js
blob: b43cebea1fd4b975ae2532f1f19a0ee2658dc602 (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
"use strict";

function check_request_header(chan, name, value) {
  var chanValue;
  try {
    chanValue = chan.getRequestHeader(name);
  } catch (e) {
    do_throw("Expected to find header '" + name + "' but didn't find it");
  }
  Assert.equal(chanValue, value);
}

function run_test() {
  var chan = NetUtil.newChannel({
    uri: "http://www.mozilla.org/",
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);

  check_request_header(chan, "host", "www.mozilla.org");
  check_request_header(chan, "Host", "www.mozilla.org");

  chan.setRequestHeader("foopy", "bar", false);
  check_request_header(chan, "foopy", "bar");

  chan.setRequestHeader("foopy", "baz", true);
  check_request_header(chan, "foopy", "bar, baz");

  for (let i = 0; i < 100; ++i) {
    chan.setRequestHeader("foopy" + i, i, false);
  }

  for (let i = 0; i < 100; ++i) {
    check_request_header(chan, "foopy" + i, i);
  }

  var x = false;
  try {
    chan.setRequestHeader("foo:py", "baz", false);
  } catch (e) {
    x = true;
  }
  if (!x) {
    do_throw("header with colon not rejected");
  }

  x = false;
  try {
    chan.setRequestHeader("foopy", "b\naz", false);
  } catch (e) {
    x = true;
  }
  if (!x) {
    do_throw("header value with newline not rejected");
  }

  x = false;
  try {
    chan.setRequestHeader("foopy\u0080", "baz", false);
  } catch (e) {
    x = true;
  }
  if (!x) {
    do_throw("header name with non-ASCII not rejected");
  }

  x = false;
  try {
    chan.setRequestHeader("foopy", "b\u0000az", false);
  } catch (e) {
    x = true;
  }
  if (!x) {
    do_throw("header value with null-byte not rejected");
  }
}