summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/request/request-headers.any.js
blob: 22925e01b6923c3dae8a9624c8b16b1fad83d2f9 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// META: global=window,worker
// META: title=Request Headers

var validRequestHeaders = [
  ["Content-Type", "OK"],
  ["Potato", "OK"],
  ["proxy", "OK"],
  ["proxya", "OK"],
  ["sec", "OK"],
  ["secb", "OK"],
  ["Set-Cookie2", "OK"],
  ["User-Agent", "OK"],
];
var invalidRequestHeaders = [
  ["Accept-Charset", "KO"],
  ["accept-charset", "KO"],
  ["ACCEPT-ENCODING", "KO"],
  ["Accept-Encoding", "KO"],
  ["Access-Control-Request-Headers", "KO"],
  ["Access-Control-Request-Method", "KO"],
  ["Access-Control-Request-Private-Network", "KO"],
  ["Connection", "KO"],
  ["Content-Length", "KO"],
  ["Cookie", "KO"],
  ["Cookie2", "KO"],
  ["Date", "KO"],
  ["DNT", "KO"],
  ["Expect", "KO"],
  ["Host", "KO"],
  ["Keep-Alive", "KO"],
  ["Origin", "KO"],
  ["Referer", "KO"],
  ["Set-Cookie", "KO"],
  ["TE", "KO"],
  ["Trailer", "KO"],
  ["Transfer-Encoding", "KO"],
  ["Upgrade", "KO"],
  ["Via", "KO"],
  ["Proxy-", "KO"],
  ["proxy-a", "KO"],
  ["Sec-", "KO"],
  ["sec-b", "KO"],
];

var validRequestNoCorsHeaders = [
  ["Accept", "OK"],
  ["Accept-Language", "OK"],
  ["content-language", "OK"],
  ["content-type", "application/x-www-form-urlencoded"],
  ["content-type", "application/x-www-form-urlencoded;charset=UTF-8"],
  ["content-type", "multipart/form-data"],
  ["content-type", "multipart/form-data;charset=UTF-8"],
  ["content-TYPE", "text/plain"],
  ["CONTENT-type", "text/plain;charset=UTF-8"],
];
var invalidRequestNoCorsHeaders = [
  ["Content-Type", "KO"],
  ["Potato", "KO"],
  ["proxy", "KO"],
  ["proxya", "KO"],
  ["sec", "KO"],
  ["secb", "KO"],
  ["Empty-Value", ""],
];

validRequestHeaders.forEach(function(header) {
  test(function() {
    var request = new Request("");
    request.headers.set(header[0], header[1]);
    assert_equals(request.headers.get(header[0]), header[1]);
  }, "Adding valid request header \"" + header[0] + ": " + header[1] + "\"");
});
invalidRequestHeaders.forEach(function(header) {
  test(function() {
    var request = new Request("");
    request.headers.set(header[0], header[1]);
    assert_equals(request.headers.get(header[0]), null);
  }, "Adding invalid request header \"" + header[0] + ": " + header[1] + "\"");
});

validRequestNoCorsHeaders.forEach(function(header) {
  test(function() {
    var requestNoCors = new Request("", {"mode": "no-cors"});
    requestNoCors.headers.set(header[0], header[1]);
    assert_equals(requestNoCors.headers.get(header[0]), header[1]);
  }, "Adding valid no-cors request header \"" + header[0] + ": " + header[1] + "\"");
});
invalidRequestNoCorsHeaders.forEach(function(header) {
  test(function() {
    var requestNoCors = new Request("", {"mode": "no-cors"});
    requestNoCors.headers.set(header[0], header[1]);
    assert_equals(requestNoCors.headers.get(header[0]), null);
  }, "Adding invalid no-cors request header \"" + header[0] + ": " + header[1] + "\"");
});

test(function() {
  var headers = new Headers([["Cookie2", "potato"]]);
  var request = new Request("", {"headers": headers});
  assert_equals(request.headers.get("Cookie2"), null);
}, "Check that request constructor is filtering headers provided as init parameter");

test(function() {
  var headers = new Headers([["Content-Type", "potato"]]);
  var request = new Request("", {"headers": headers, "mode": "no-cors"});
  assert_equals(request.headers.get("Content-Type"), null);
}, "Check that no-cors request constructor is filtering headers provided as init parameter");

test(function() {
  var headers = new Headers([["Content-Type", "potato"]]);
  var initialRequest = new Request("", {"headers": headers});
  var request = new Request(initialRequest, {"mode": "no-cors"});
  assert_equals(request.headers.get("Content-Type"), null);
}, "Check that no-cors request constructor is filtering headers provided as part of request parameter");

test(function() {
  var initialHeaders = new Headers([["Content-Type", "potato"]]);
  var initialRequest = new Request("", {"headers" : initialHeaders});
  var request = new Request(initialRequest);
  assert_equals(request.headers.get("Content-Type"), "potato");
}, "Request should get its content-type from the init request");

test(function() {
  var initialHeaders = new Headers([["Content-Type", "potato"]]);
  var initialRequest = new Request("", {"headers" : initialHeaders});
  var headers = new Headers([]);
  var request = new Request(initialRequest, {"headers" : headers});
  assert_false(request.headers.has("Content-Type"));
}, "Request should not get its content-type from the init request if init headers are provided");

test(function() {
  var initialHeaders = new Headers([["Content-Type-Extra", "potato"]]);
  var initialRequest = new Request("", {"headers" : initialHeaders, "body" : "this is my plate", "method" : "POST"});
  var request = new Request(initialRequest);
  assert_equals(request.headers.get("Content-Type"), "text/plain;charset=UTF-8");
}, "Request should get its content-type from the body if none is provided");

test(function() {
  var initialHeaders = new Headers([["Content-Type", "potato"]]);
  var initialRequest = new Request("", {"headers" : initialHeaders, "body" : "this is my plate", "method" : "POST"});
  var request = new Request(initialRequest);
  assert_equals(request.headers.get("Content-Type"), "potato");
}, "Request should get its content-type from init headers if one is provided");

test(function() {
  var array = [["hello", "worldAHH"]];
  var object = {"hello": 'worldOOH'};
  var headers = new Headers(array);

  assert_equals(headers.get("hello"), "worldAHH");

  var request1 = new Request("", {"headers": headers});
  var request2 = new Request("", {"headers": array});
  var request3 = new Request("", {"headers": object});

  assert_equals(request1.headers.get("hello"), "worldAHH");
  assert_equals(request2.headers.get("hello"), "worldAHH");
  assert_equals(request3.headers.get("hello"), "worldOOH");
}, "Testing request header creations with various objects");

promise_test(function(test) {
  var request = new Request("", {"headers" : [["Content-Type", ""]], "body" : "this is my plate", "method" : "POST"});
  return request.blob().then(function(blob) {
    assert_equals(blob.type, "", "Blob type should be the empty string");
  });
}, "Testing empty Request Content-Type header");

test(function() {
  const request1 = new Request("");
  assert_equals(request1.headers, request1.headers);

  const request2 = new Request("", {"headers": {"X-Foo": "bar"}});
  assert_equals(request2.headers, request2.headers);
  const headers = request2.headers;
  request2.headers.set("X-Foo", "quux");
  assert_equals(headers, request2.headers);
  headers.set("X-Other-Header", "baz");
  assert_equals(headers, request2.headers);
}, "Test that Request.headers has the [SameObject] extended attribute");