summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/fetch/test_headers_mainthread.html
blob: de78b364e332d029c3cc44759564765ce140d74f (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!-- Any copyright is dedicated to the Public Domain.
   - http://creativecommons.org/publicdomain/zero/1.0/ -->
<!DOCTYPE HTML>
<html>
<head>
  <title>Test Fetch Headers - Basic</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script type="text/javascript" src="test_headers_common.js"> </script>
<script type="text/javascript">
// Main thread specific tests because they need SpecialPowers.  Expects
// test_headers_common.js to already be loaded.

function TestRequestHeaders() {
  is(typeof Headers, "function", "Headers global constructor exists.");
  var headers = new Headers();
  ok(headers, "Constructed empty Headers object");
  SpecialPowers.wrap(headers).guard = "request";
  TestCoreBehavior(headers, "foo");
  var forbidden = [
    "Accept-Charset",
    "Accept-Encoding",
    "Access-Control-Request-Headers",
    "Access-Control-Request-Method",
    "Connection",
    "Content-Length",
    "Cookie",
    "Cookie2",
    "Date",
    "DNT",
    "Expect",
    "Host",
    "Keep-Alive",
    "Origin",
    "Referer",
    "TE",
    "Trailer",
    "Transfer-Encoding",
    "Upgrade",
    "Via",
    "Proxy-Authorization",
    "Proxy-blarg",
    "Proxy-",
    "Sec-foo",
    "Sec-"
  ];

  for (var i = 0, n = forbidden.length; i < n; ++i) {
    var name = forbidden[i];
    headers.append(name, "hmm");
    checkNotHas(headers, name, "Should not be able to append " + name + " to request headers");
    headers.set(name, "hmm");
    checkNotHas(headers, name, "Should not be able to set " + name + " on request headers");
  }
}

function TestRequestNoCorsHeaders() {
  is(typeof Headers, "function", "Headers global constructor exists.");
  var headers = new Headers();
  ok(headers, "Constructed empty Headers object");
  SpecialPowers.wrap(headers).guard = "request-no-cors";

  headers.append("foo", "bar");
  checkNotHas(headers, "foo", "Should not be able to append arbitrary headers to request-no-cors headers.");
  headers.set("foo", "bar");
  checkNotHas(headers, "foo", "Should not be able to set arbitrary headers on request-no-cors headers.");

  var simpleNames = [
    "Accept",
    "Accept-Language",
    "Content-Language"
  ];

  var simpleContentTypes = [
    "application/x-www-form-urlencoded",
    "multipart/form-data",
    "text/plain",
    "application/x-www-form-urlencoded; charset=utf-8",
    "multipart/form-data; charset=utf-8",
    "text/plain; charset=utf-8"
  ];

  for (var i = 0, n = simpleNames.length; i < n; ++i) {
    var name = simpleNames[i];
    headers.append(name, "hmm");
    checkHas(headers, name, "Should be able to append " + name + " to request-no-cors headers");
    headers.set(name, "hmm");
    checkHas(headers, name, "Should be able to set " + name + " on request-no-cors headers");
  }

  for (var i = 0, n = simpleContentTypes.length; i < n; ++i) {
    var value = simpleContentTypes[i];
    headers.append("Content-Type", value);
    checkHas(headers, "Content-Type", "Should be able to append " + value + " Content-Type to request-no-cors headers");
    headers.delete("Content-Type");
    headers.set("Content-Type", value);
    checkHas(headers, "Content-Type", "Should be able to set " + value + " Content-Type on request-no-cors headers");
  }
}

function TestResponseHeaders() {
  is(typeof Headers, "function", "Headers global constructor exists.");
  var headers = new Headers();
  ok(headers, "Constructed empty Headers object");
  SpecialPowers.wrap(headers).guard = "response";
  TestCoreBehavior(headers, "foo");
  var forbidden = [
    "Set-Cookie",
    "Set-Cookie2"
  ];

  for (var i = 0, n = forbidden.length; i < n; ++i) {
    var name = forbidden[i];
    headers.append(name, "hmm");
    checkNotHas(headers, name, "Should not be able to append " + name + " to response headers");
    headers.set(name, "hmm");
    checkNotHas(headers, name, "Should not be able to set " + name + " on response headers");
  }
}

function TestImmutableHeaders() {
  is(typeof Headers, "function", "Headers global constructor exists.");
  var headers = new Headers();
  ok(headers, "Constructed empty Headers object");
  TestCoreBehavior(headers, "foo");
  headers.append("foo", "atleastone");

  SpecialPowers.wrap(headers).guard = "immutable";

  shouldThrow(function() {
    headers.append("foo", "wat");
  }, TypeError, "Should not be able to append to immutable headers");

  shouldThrow(function() {
    headers.set("foo", "wat");
  }, TypeError, "Should not be able to set immutable headers");

  shouldThrow(function() {
    headers.delete("foo");
  }, TypeError, "Should not be able to delete immutable headers");

  checkHas(headers, "foo", "Should be able to check immutable headers");
  ok(headers.get("foo"), "Should be able to get immutable headers");
}

TestRequestHeaders();
TestRequestNoCorsHeaders();
TestResponseHeaders();
TestImmutableHeaders();
</script>
</body>
</html>