Branch data Line data Source code
1 [ + ]: 1 : import assert from 'node:assert/strict';
2 : 1 : import { XMLHttpRequest } from '../../../lib/whatwg-xhr.js';
3 : 1 : import { FormData } from '../../../lib/whatwg-xhr.js';
4 : 1 :
5 [ + ]: 1 : export default (activeURL) => {
6 : 1 :
7 : 1 : request(
8 [ + ]: 1 : function _String() { return ""; },
9 : 1 : {"Content-Type": ""},
10 : 1 : "",
11 : 1 : 'setRequestHeader("") sends a blank string'
12 : 1 : );
13 : 1 : request(
14 [ + ]: 1 : function _String() { return ""; },
15 : 1 : {"Content-Type": " "},
16 : 1 : "",
17 : 1 : 'setRequestHeader(" ") sends the string " "'
18 : 1 : );
19 : 1 : request(
20 [ + ]: 1 : function _String() { return ""; },
21 : 1 : {"Content-Type": null},
22 : 1 : "null",
23 : 1 : 'setRequestHeader(null) sends the string "null"'
24 : 1 : );
25 : 1 : request(
26 [ + ]: 1 : function _String() { return ""; },
27 : 1 : {"Content-Type": undefined},
28 : 1 : "undefined",
29 : 1 : 'setRequestHeader(undefined) sends the string "undefined"'
30 : 1 : );
31 : 1 : request(
32 [ + ]: 1 : function _String() { return "test"; },
33 : 1 : {},
34 : 1 : "text/plain;charset=UTF-8",
35 : 1 : 'String request has correct default Content-Type of "text/plain;charset=UTF-8"'
36 : 1 : );
37 : 1 : request(
38 [ + ]: 1 : function _String() { return "test()"; },
39 : 1 : {"Content-Type": "text/javascript;charset=ASCII"},
40 : 1 : "text/javascript;charset=UTF-8",
41 : 1 : "String request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8"
42 : 1 : );
43 : 1 :
44 : 1 : /*
45 : 1 : request(
46 : 1 : function _XMLDocument() { return new DOMParser().parseFromString("<xml/>", "application/xml"); },
47 : 1 : {"Content-Type": ""},
48 : 1 : "",
49 : 1 : 'XML Document request respects setRequestHeader("")'
50 : 1 : );
51 : 1 : request(
52 : 1 : function _XMLDocument() { return new DOMParser().parseFromString("<xml/>", "application/xml"); },
53 : 1 : {},
54 : 1 : "application/xml;charset=UTF-8",
55 : 1 : 'XML Document request has correct default Content-Type of "application/xml;charset=UTF-8"'
56 : 1 : );
57 : 1 : request(
58 : 1 : function _XMLDocument() { return new DOMParser().parseFromString("<xml/>", "application/xml"); },
59 : 1 : {"Content-Type": "application/xhtml+xml;charset=ASCII"},
60 : 1 : "application/xhtml+xml;charset=UTF-8",
61 : 1 : "XML Document request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8"
62 : 1 : );
63 : 1 : request(
64 : 1 : function _HTMLDocument() { return new DOMParser().parseFromString("<html></html>", "text/html"); },
65 : 1 : {"Content-Type": ""},
66 : 1 : "",
67 : 1 : 'HTML Document request respects setRequestHeader("")'
68 : 1 : );
69 : 1 : request(
70 : 1 : function _HTMLDocument() { return new DOMParser().parseFromString("<html></html>", "text/html"); },
71 : 1 : {},
72 : 1 : "text/html;charset=UTF-8",
73 : 1 : 'HTML Document request has correct default Content-Type of "text/html;charset=UTF-8"'
74 : 1 : );
75 : 1 : request(
76 : 1 : function _HTMLDocument() { return new DOMParser().parseFromString("<html></html>", "text/html"); },
77 : 1 : {"Content-Type": "text/html+junk;charset=ASCII"},
78 : 1 : "text/html+junk;charset=UTF-8",
79 : 1 : "HTML Document request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8"
80 : 1 : );
81 : 1 : */
82 : 1 :
83 : 1 : request(
84 [ + ]: 1 : function _Blob() { return new Blob(["test"]); },
85 : 1 : {"Content-Type": ""},
86 : 1 : "",
87 : 1 : 'Blob request respects setRequestHeader("") to be specified'
88 : 1 : );
89 : 1 : request(
90 [ + ]: 1 : function _Blob() { return new Blob(["test"]); },
91 : 1 : {},
92 : 1 : undefined,
93 : 1 : "Blob request with unset type sends no Content-Type without setRequestHeader() call"
94 : 1 : );
95 : 1 : request(
96 [ + ]: 1 : function _Blob() { return new Blob(["test"]); },
97 : 1 : {"Content-Type": "application/xml;charset=ASCII"},
98 : 1 : "application/xml;charset=ASCII",
99 : 1 : "Blob request with unset type keeps setRequestHeader() Content-Type and charset"
100 : 1 : );
101 : 1 : request(
102 [ + ]: 1 : function _Blob() { return new Blob(["<xml/>"], {type : "application/xml;charset=ASCII"}); },
103 : 1 : {"Content-Type": ""},
104 : 1 : "",
105 : 1 : 'Blob request with set type respects setRequestHeader("") to be specified'
106 : 1 : );
107 : 1 : request(
108 [ + ]: 1 : function _Blob() { return new Blob(["<xml/>"], {type : "application/xml;charset=ASCII"}); },
109 : 1 : {},
110 : 1 : "application/xml;charset=ascii", // new Blob lowercases the type argument
111 : 1 : "Blob request with set type uses that it for Content-Type unless setRequestHeader()"
112 : 1 : );
113 : 1 : request(
114 [ + ]: 1 : function _Blob() { return new Blob(["<xml/>"], {type : "application/xml;charset=UTF8"}); },
115 : 1 : {"Content-Type": "application/xml+junk;charset=ASCII"},
116 : 1 : "application/xml+junk;charset=ASCII",
117 : 1 : "Blob request with set type keeps setRequestHeader() Content-Type and charset"
118 : 1 : );
119 : 1 : request(
120 [ + ]: 1 : function _ArrayBuffer() { return new ArrayBuffer(10); },
121 : 1 : {"Content-Type": ""},
122 : 1 : "",
123 : 1 : 'ArrayBuffer request respects setRequestHeader("")'
124 : 1 : );
125 : 1 : request(
126 [ + ]: 1 : function _ArrayBuffer() { return new ArrayBuffer(10); },
127 : 1 : {},
128 : 1 : undefined,
129 : 1 : "ArrayBuffer request sends no Content-Type without setRequestHeader() call"
130 : 1 : );
131 : 1 : request(
132 [ + ]: 1 : function _ArrayBuffer() { return new ArrayBuffer(10); },
133 : 1 : {"Content-Type": "application/xml;charset=ASCII"},
134 : 1 : "application/xml;charset=ASCII",
135 : 1 : "ArrayBuffer request keeps setRequestHeader() Content-Type and charset"
136 : 1 : );
137 : 1 : request(
138 [ + ]: 1 : function _Uint8Array() { return new Uint8Array(new ArrayBuffer(10)); },
139 : 1 : {"Content-Type": ""},
140 : 1 : "",
141 : 1 : 'ArrayBufferView request respects setRequestHeader("")'
142 : 1 : );
143 : 1 : request(
144 [ + ]: 1 : function _Uint8Array() { return new Uint8Array(new ArrayBuffer(10)); },
145 : 1 : {},
146 : 1 : undefined,
147 : 1 : "ArrayBufferView request sends no Content-Type without setRequestHeader() call"
148 : 1 : );
149 : 1 : request(
150 [ + ]: 1 : function _Uint8Array() { return new Uint8Array(new ArrayBuffer(10)); },
151 : 1 : {"Content-Type": "application/xml;charset=ASCII"},
152 : 1 : "application/xml;charset=ASCII",
153 : 1 : "ArrayBufferView request keeps setRequestHeader() Content-Type and charset"
154 : 1 : );
155 : 1 : request(
156 [ + ]: 1 : function _FormData() { return new FormData(); },
157 : 1 : {"Content-Type": ""},
158 : 1 : "",
159 : 1 : 'FormData request respects setRequestHeader("")'
160 : 1 : );
161 : 1 : request(
162 [ + ]: 1 : function _FormData() { return new FormData(); },
163 : 1 : {},
164 : 1 : /multipart\/form-data; boundary=(.*)/,
165 : 1 : 'FormData request has correct default Content-Type of "multipart\/form-data; boundary=_"'
166 : 1 : );
167 : 1 : request(
168 [ + ]: 1 : function _FormData() { return new FormData(); },
169 : 1 : {"Content-Type": "application/xml;charset=ASCII"},
170 : 1 : "application/xml;charset=ASCII",
171 : 1 : "FormData request keeps setRequestHeader() Content-Type and charset"
172 : 1 : );
173 : 1 : request(
174 [ + ]: 1 : function _URLSearchParams() { return new URLSearchParams("q=testQ&topic=testTopic") },
175 : 1 : {"Content-Type": ""},
176 : 1 : "",
177 : 1 : 'URLSearchParams respects setRequestHeader("")'
178 : 1 : );
179 : 1 : request(
180 [ + ]: 1 : function _URLSearchParams() { return new URLSearchParams("q=testQ&topic=testTopic") },
181 : 1 : {},
182 : 1 : "application/x-www-form-urlencoded;charset=UTF-8",
183 : 1 : 'URLSearchParams request has correct default Content-Type of "application/x-www-form-urlencoded;charset=UTF-8"'
184 : 1 : );
185 : 1 : request(
186 [ + ]: 1 : function _URLSearchParams() { return new URLSearchParams("q=testQ&topic=testTopic") },
187 : 1 : {"Content-Type": "application/xml;charset=ASCII"},
188 : 1 : "application/xml;charset=UTF-8",
189 : 1 : "URLSearchParams request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8"
190 : 1 :
191 : 1 : // the default Content-Type for URLSearchParams has a charset specified (utf-8) in
192 : 1 : // https://fetch.spec.whatwg.org/#bodyinit, so the user's must be changed to match it
193 : 1 : // as per https://xhr.spec.whatwg.org/#the-send%28%29-method step 4.
194 : 1 : );
195 : 1 :
196 [ + ]: 1 : function request(inputGenerator, headersToSend, expectedType, title){
197 : 24 :
198 : 24 : const toSend = inputGenerator();
199 : 24 :
200 : 24 : const xhr = new XMLHttpRequest();
201 : 24 :
202 : 24 : xhr.open('POST', `${activeURL}/inspect-headers.py?filter_name=Content-Type`, false);
203 : 24 :
204 [ + ]: 24 : for(let header in headersToSend){
205 : 17 : if(headersToSend.hasOwnProperty(header)){
206 : 17 : xhr.setRequestHeader(header, headersToSend[header]);
207 : 17 : }
208 : 17 : }
209 : 24 :
210 : 24 : xhr.send(toSend);
211 : 24 :
212 : 24 : const actual = xhr.responseText;
213 : 24 :
214 [ + ][ + ]: 24 : if(expectedType === undefined || expectedType === null){
215 : 3 : assert.strictEqual(actual, '');
216 [ + ]: 3 : }
217 : 21 : else
218 [ + ]: 21 : if(expectedType instanceof RegExp){
219 : 1 : assert(expectedType.test(actual));
220 [ + ]: 1 : }
221 : 20 : else{
222 : 20 : assert.strictEqual(actual, `Content-Type: ${expectedType}\n`);
223 : 20 : }
224 : 24 : }
225 : 1 : }
226 : 1 :
227 : 1 : /*
228 : 1 : * setrequestheader-content-type.htm
229 : 1 : *
230 : 1 :
231 : 1 : <!DOCTYPE html>
232 : 1 : <html>
233 : 1 : <head>
234 : 1 : <title>XMLHttpRequest: setRequestHeader() - Content-Type header</title>
235 : 1 : <meta name="timeout" content="long">
236 : 1 : <script src="/resources/testharness.js"></script>
237 : 1 : <script src="/resources/testharnessreport.js"></script>
238 : 1 : <link rel="help" href="https://xhr.spec.whatwg.org/#the-setrequestheader()-method">
239 : 1 : </head>
240 : 1 : <body>
241 : 1 : <div id="log"></div>
242 : 1 : <script>
243 : 1 : function request(inputGenerator, headersToSend, expectedType, title) {
244 : 1 : test(function() {
245 : 1 : const toSend = inputGenerator(),
246 : 1 : client = new XMLHttpRequest()
247 : 1 : client.open("POST", "resources/inspect-headers.py?filter_name=Content-Type", false)
248 : 1 : for(header in headersToSend) {
249 : 1 : if (headersToSend.hasOwnProperty(header)) {
250 : 1 : client.setRequestHeader(header, headersToSend[header]);
251 : 1 : }
252 : 1 : }
253 : 1 : client.send(toSend)
254 : 1 :
255 : 1 : const actual = client.responseText
256 : 1 : if (expectedType === undefined || expectedType === null) {
257 : 1 : assert_equals(actual, "");
258 : 1 : } else if (expectedType instanceof RegExp) {
259 : 1 : assert_regexp_match(actual, expectedType);
260 : 1 : } else {
261 : 1 : assert_equals(actual, "Content-Type: " + expectedType + "\n");
262 : 1 : }
263 : 1 : }, title)
264 : 1 : }
265 : 1 : request(
266 : 1 : function _String() { return ""; },
267 : 1 : {"Content-Type": ""},
268 : 1 : "",
269 : 1 : 'setRequestHeader("") sends a blank string'
270 : 1 : )
271 : 1 : request(
272 : 1 : function _String() { return ""; },
273 : 1 : {"Content-Type": " "},
274 : 1 : "",
275 : 1 : 'setRequestHeader(" ") sends the string " "'
276 : 1 : )
277 : 1 : request(
278 : 1 : function _String() { return ""; },
279 : 1 : {"Content-Type": null},
280 : 1 : "null",
281 : 1 : 'setRequestHeader(null) sends the string "null"'
282 : 1 : )
283 : 1 : request(
284 : 1 : function _String() { return ""; },
285 : 1 : {"Content-Type": undefined},
286 : 1 : "undefined",
287 : 1 : 'setRequestHeader(undefined) sends the string "undefined"'
288 : 1 : )
289 : 1 : request(
290 : 1 : function _String() { return "test"; },
291 : 1 : {},
292 : 1 : "text/plain;charset=UTF-8",
293 : 1 : 'String request has correct default Content-Type of "text/plain;charset=UTF-8"'
294 : 1 : )
295 : 1 : request(
296 : 1 : function _String() { return "test()"; },
297 : 1 : {"Content-Type": "text/javascript;charset=ASCII"},
298 : 1 : "text/javascript;charset=UTF-8",
299 : 1 : "String request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8"
300 : 1 : )
301 : 1 : request(
302 : 1 : function _XMLDocument() { return new DOMParser().parseFromString("<xml/>", "application/xml"); },
303 : 1 : {"Content-Type": ""},
304 : 1 : "",
305 : 1 : 'XML Document request respects setRequestHeader("")'
306 : 1 : )
307 : 1 : request(
308 : 1 : function _XMLDocument() { return new DOMParser().parseFromString("<xml/>", "application/xml"); },
309 : 1 : {},
310 : 1 : "application/xml;charset=UTF-8",
311 : 1 : 'XML Document request has correct default Content-Type of "application/xml;charset=UTF-8"'
312 : 1 : )
313 : 1 : request(
314 : 1 : function _XMLDocument() { return new DOMParser().parseFromString("<xml/>", "application/xml"); },
315 : 1 : {"Content-Type": "application/xhtml+xml;charset=ASCII"},
316 : 1 : "application/xhtml+xml;charset=UTF-8",
317 : 1 : "XML Document request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8"
318 : 1 : )
319 : 1 : request(
320 : 1 : function _HTMLDocument() { return new DOMParser().parseFromString("<html></html>", "text/html"); },
321 : 1 : {"Content-Type": ""},
322 : 1 : "",
323 : 1 : 'HTML Document request respects setRequestHeader("")'
324 : 1 : )
325 : 1 : request(
326 : 1 : function _HTMLDocument() { return new DOMParser().parseFromString("<html></html>", "text/html"); },
327 : 1 : {},
328 : 1 : "text/html;charset=UTF-8",
329 : 1 : 'HTML Document request has correct default Content-Type of "text/html;charset=UTF-8"'
330 : 1 : )
331 : 1 : request(
332 : 1 : function _HTMLDocument() { return new DOMParser().parseFromString("<html></html>", "text/html"); },
333 : 1 : {"Content-Type": "text/html+junk;charset=ASCII"},
334 : 1 : "text/html+junk;charset=UTF-8",
335 : 1 : "HTML Document request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8"
336 : 1 : )
337 : 1 : request(
338 : 1 : function _Blob() { return new Blob(["test"]); },
339 : 1 : {"Content-Type": ""},
340 : 1 : "",
341 : 1 : 'Blob request respects setRequestHeader("") to be specified'
342 : 1 : )
343 : 1 : request(
344 : 1 : function _Blob() { return new Blob(["test"]); },
345 : 1 : {},
346 : 1 : undefined,
347 : 1 : "Blob request with unset type sends no Content-Type without setRequestHeader() call"
348 : 1 : )
349 : 1 : request(
350 : 1 : function _Blob() { return new Blob(["test"]); },
351 : 1 : {"Content-Type": "application/xml;charset=ASCII"},
352 : 1 : "application/xml;charset=ASCII",
353 : 1 : "Blob request with unset type keeps setRequestHeader() Content-Type and charset"
354 : 1 : )
355 : 1 : request(
356 : 1 : function _Blob() { return new Blob(["<xml/>"], {type : "application/xml;charset=ASCII"}); },
357 : 1 : {"Content-Type": ""},
358 : 1 : "",
359 : 1 : 'Blob request with set type respects setRequestHeader("") to be specified'
360 : 1 : )
361 : 1 : request(
362 : 1 : function _Blob() { return new Blob(["<xml/>"], {type : "application/xml;charset=ASCII"}); },
363 : 1 : {},
364 : 1 : "application/xml;charset=ascii", // new Blob lowercases the type argument
365 : 1 : "Blob request with set type uses that it for Content-Type unless setRequestHeader()"
366 : 1 : )
367 : 1 : request(
368 : 1 : function _Blob() { return new Blob(["<xml/>"], {type : "application/xml;charset=UTF8"}); },
369 : 1 : {"Content-Type": "application/xml+junk;charset=ASCII"},
370 : 1 : "application/xml+junk;charset=ASCII",
371 : 1 : "Blob request with set type keeps setRequestHeader() Content-Type and charset"
372 : 1 : )
373 : 1 : request(
374 : 1 : function _ArrayBuffer() { return new ArrayBuffer(10); },
375 : 1 : {"Content-Type": ""},
376 : 1 : "",
377 : 1 : 'ArrayBuffer request respects setRequestHeader("")'
378 : 1 : )
379 : 1 : request(
380 : 1 : function _ArrayBuffer() { return new ArrayBuffer(10); },
381 : 1 : {},
382 : 1 : undefined,
383 : 1 : "ArrayBuffer request sends no Content-Type without setRequestHeader() call"
384 : 1 : )
385 : 1 : request(
386 : 1 : function _ArrayBuffer() { return new ArrayBuffer(10); },
387 : 1 : {"Content-Type": "application/xml;charset=ASCII"},
388 : 1 : "application/xml;charset=ASCII",
389 : 1 : "ArrayBuffer request keeps setRequestHeader() Content-Type and charset"
390 : 1 : )
391 : 1 : request(
392 : 1 : function _Uint8Array() { return new Uint8Array(new ArrayBuffer(10)); },
393 : 1 : {"Content-Type": ""},
394 : 1 : "",
395 : 1 : 'ArrayBufferView request respects setRequestHeader("")'
396 : 1 : )
397 : 1 : request(
398 : 1 : function _Uint8Array() { return new Uint8Array(new ArrayBuffer(10)); },
399 : 1 : {},
400 : 1 : undefined,
401 : 1 : "ArrayBufferView request sends no Content-Type without setRequestHeader() call"
402 : 1 : )
403 : 1 : request(
404 : 1 : function _Uint8Array() { return new Uint8Array(new ArrayBuffer(10)); },
405 : 1 : {"Content-Type": "application/xml;charset=ASCII"},
406 : 1 : "application/xml;charset=ASCII",
407 : 1 : "ArrayBufferView request keeps setRequestHeader() Content-Type and charset"
408 : 1 : )
409 : 1 : request(
410 : 1 : function _FormData() { return new FormData(); },
411 : 1 : {"Content-Type": ""},
412 : 1 : "",
413 : 1 : 'FormData request respects setRequestHeader("")'
414 : 1 : )
415 : 1 : request(
416 : 1 : function _FormData() { return new FormData(); },
417 : 1 : {},
418 : 1 : /multipart\/form-data; boundary=(.*)/,
419 : 1 : 'FormData request has correct default Content-Type of "multipart\/form-data; boundary=_"'
420 : 1 : )
421 : 1 : request(
422 : 1 : function _FormData() { return new FormData(); },
423 : 1 : {"Content-Type": "application/xml;charset=ASCII"},
424 : 1 : "application/xml;charset=ASCII",
425 : 1 : "FormData request keeps setRequestHeader() Content-Type and charset"
426 : 1 : )
427 : 1 : request(
428 : 1 : function _URLSearchParams() { return new URLSearchParams("q=testQ&topic=testTopic") },
429 : 1 : {"Content-Type": ""},
430 : 1 : "",
431 : 1 : 'URLSearchParams respects setRequestHeader("")'
432 : 1 : )
433 : 1 : request(
434 : 1 : function _URLSearchParams() { return new URLSearchParams("q=testQ&topic=testTopic") },
435 : 1 : {},
436 : 1 : "application/x-www-form-urlencoded;charset=UTF-8",
437 : 1 : 'URLSearchParams request has correct default Content-Type of "application/x-www-form-urlencoded;charset=UTF-8"'
438 : 1 : )
439 : 1 : request(
440 : 1 : function _URLSearchParams() { return new URLSearchParams("q=testQ&topic=testTopic") },
441 : 1 : {"Content-Type": "application/xml;charset=ASCII"},
442 : 1 : "application/xml;charset=UTF-8",
443 : 1 : "URLSearchParams request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8"
444 : 1 : // the default Content-Type for URLSearchParams has a charset specified (utf-8) in
445 : 1 : // https://fetch.spec.whatwg.org/#bodyinit, so the user's must be changed to match it
446 : 1 : // as per https://xhr.spec.whatwg.org/#the-send%28%29-method step 4.
447 : 1 : )
448 : 1 : </script>
449 : 1 : </body>
450 : 1 : </html>
451 : 1 :
452 : 1 : *
453 : 1 : * setrequestheader-content-type.htm
454 : 1 : */
|