summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/xhr/setrequestheader-content-type.htm
blob: 07238391eb5cc8639edbe996208a18b6d9d26b04 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE html>
<html>
  <head>
    <title>XMLHttpRequest: setRequestHeader() - Content-Type header</title>
    <meta name="timeout" content="long">
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
    <link rel="help" href="https://xhr.spec.whatwg.org/#the-setrequestheader()-method">
  </head>
  <body>
    <div id="log"></div>
    <script>
      function request(inputGenerator, headersToSend, expectedType, title) {
        test(function() {
          const toSend = inputGenerator(),
                client = new XMLHttpRequest()
          client.open("POST", "resources/inspect-headers.py?filter_name=Content-Type", false)
          for(header in headersToSend) {
            if (headersToSend.hasOwnProperty(header)) {
              client.setRequestHeader(header, headersToSend[header]);
            }
          }
          client.send(toSend)

          const actual = client.responseText
          if (expectedType === undefined || expectedType === null) {
            assert_equals(actual, "");
          } else if (expectedType instanceof RegExp) {
            assert_regexp_match(actual, expectedType);
          } else {
            assert_equals(actual, "Content-Type: " + expectedType + "\n");
          }
        }, title)
      }
      request(
        function _String() { return ""; },
        {"Content-Type": ""},
        "",
        'setRequestHeader("") sends a blank string'
      )
      request(
        function _String() { return ""; },
        {"Content-Type": " "},
        "",
        'setRequestHeader(" ") sends the string " "'
      )
      request(
        function _String() { return ""; },
        {"Content-Type": null},
        "null",
        'setRequestHeader(null) sends the string "null"'
      )
      request(
        function _String() { return ""; },
        {"Content-Type": undefined},
        "undefined",
        'setRequestHeader(undefined) sends the string "undefined"'
      )
      request(
        function _String() { return "test"; },
        {},
        "text/plain;charset=UTF-8",
        'String request has correct default Content-Type of "text/plain;charset=UTF-8"'
      )
      request(
        function _String() { return "test()"; },
        {"Content-Type": "text/javascript;charset=ASCII"},
        "text/javascript;charset=UTF-8",
        "String request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8"
      )
      request(
        function _XMLDocument() { return new DOMParser().parseFromString("<xml/>", "application/xml"); },
        {"Content-Type": ""},
        "",
        'XML Document request respects setRequestHeader("")'
      )
      request(
        function _XMLDocument() { return new DOMParser().parseFromString("<xml/>", "application/xml"); },
        {},
        "application/xml;charset=UTF-8",
        'XML Document request has correct default Content-Type of "application/xml;charset=UTF-8"'
      )
      request(
        function _XMLDocument() { return new DOMParser().parseFromString("<xml/>", "application/xml"); },
        {"Content-Type": "application/xhtml+xml;charset=ASCII"},
        "application/xhtml+xml;charset=UTF-8",
        "XML Document request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8"
      )
      request(
        function _HTMLDocument() { return new DOMParser().parseFromString("<html></html>", "text/html"); },
        {"Content-Type": ""},
        "",
        'HTML Document request respects setRequestHeader("")'
      )
      request(
        function _HTMLDocument() { return new DOMParser().parseFromString("<html></html>", "text/html"); },
        {},
        "text/html;charset=UTF-8",
        'HTML Document request has correct default Content-Type of "text/html;charset=UTF-8"'
      )
      request(
        function _HTMLDocument() { return new DOMParser().parseFromString("<html></html>", "text/html"); },
        {"Content-Type": "text/html+junk;charset=ASCII"},
        "text/html+junk;charset=UTF-8",
        "HTML Document request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8"
      )
      request(
        function _Blob() { return new Blob(["test"]); },
        {"Content-Type": ""},
        "",
        'Blob request respects setRequestHeader("") to be specified'
      )
      request(
        function _Blob() { return new Blob(["test"]); },
        {},
        undefined,
        "Blob request with unset type sends no Content-Type without setRequestHeader() call"
      )
      request(
        function _Blob() { return new Blob(["test"]); },
        {"Content-Type": "application/xml;charset=ASCII"},
        "application/xml;charset=ASCII",
        "Blob request with unset type keeps setRequestHeader() Content-Type and charset"
      )
      request(
        function _Blob() { return new Blob(["<xml/>"], {type : "application/xml;charset=ASCII"}); },
        {"Content-Type": ""},
        "",
        'Blob request with set type respects setRequestHeader("") to be specified'
      )
      request(
        function _Blob() { return new Blob(["<xml/>"], {type : "application/xml;charset=ASCII"}); },
        {},
        "application/xml;charset=ascii", // new Blob lowercases the type argument
        "Blob request with set type uses that it for Content-Type unless setRequestHeader()"
      )
      request(
        function _Blob() { return new Blob(["<xml/>"], {type : "application/xml;charset=UTF8"}); },
        {"Content-Type": "application/xml+junk;charset=ASCII"},
        "application/xml+junk;charset=ASCII",
        "Blob request with set type keeps setRequestHeader() Content-Type and charset"
      )
      request(
        function _ArrayBuffer() { return new ArrayBuffer(10); },
        {"Content-Type": ""},
        "",
        'ArrayBuffer request respects setRequestHeader("")'
      )
      request(
        function _ArrayBuffer() { return new ArrayBuffer(10); },
        {},
        undefined,
        "ArrayBuffer request sends no Content-Type without setRequestHeader() call"
      )
      request(
        function _ArrayBuffer() { return new ArrayBuffer(10); },
        {"Content-Type": "application/xml;charset=ASCII"},
        "application/xml;charset=ASCII",
        "ArrayBuffer request keeps setRequestHeader() Content-Type and charset"
      )
      request(
        function _Uint8Array() { return new Uint8Array(new ArrayBuffer(10)); },
        {"Content-Type": ""},
        "",
        'ArrayBufferView request respects setRequestHeader("")'
      )
      request(
        function _Uint8Array() { return new Uint8Array(new ArrayBuffer(10)); },
        {},
        undefined,
        "ArrayBufferView request sends no Content-Type without setRequestHeader() call"
      )
      request(
        function _Uint8Array() { return new Uint8Array(new ArrayBuffer(10)); },
        {"Content-Type": "application/xml;charset=ASCII"},
        "application/xml;charset=ASCII",
        "ArrayBufferView request keeps setRequestHeader() Content-Type and charset"
      )
      request(
        function _FormData() { return new FormData(); },
        {"Content-Type": ""},
        "",
        'FormData request respects setRequestHeader("")'
      )
      request(
        function _FormData() { return new FormData(); },
        {},
        /multipart\/form-data; boundary=(.*)/,
        'FormData request has correct default Content-Type of "multipart\/form-data; boundary=_"'
      )
      request(
        function _FormData() { return new FormData(); },
        {"Content-Type": "application/xml;charset=ASCII"},
        "application/xml;charset=ASCII",
        "FormData request keeps setRequestHeader() Content-Type and charset"
      )
      request(
        function _URLSearchParams() { return new URLSearchParams("q=testQ&topic=testTopic") },
        {"Content-Type": ""},
        "",
        'URLSearchParams respects setRequestHeader("")'
      )
      request(
        function _URLSearchParams() { return new URLSearchParams("q=testQ&topic=testTopic") },
        {},
        "application/x-www-form-urlencoded;charset=UTF-8",
        'URLSearchParams request has correct default Content-Type of "application/x-www-form-urlencoded;charset=UTF-8"'
      )
      request(
        function _URLSearchParams() { return new URLSearchParams("q=testQ&topic=testTopic") },
        {"Content-Type": "application/xml;charset=ASCII"},
        "application/xml;charset=UTF-8",
        "URLSearchParams request keeps setRequestHeader() Content-Type, with charset adjusted to UTF-8"
        // the default Content-Type for URLSearchParams has a charset specified (utf-8) in
        // https://fetch.spec.whatwg.org/#bodyinit, so the user's must be changed to match it
        // as per https://xhr.spec.whatwg.org/#the-send%28%29-method step 4.
      )
    </script>
  </body>
</html>