summaryrefslogtreecommitdiffstats
path: root/dom/xhr/tests/test_XHRSendData.html
blob: 5f801e827db07088fe9d55b92259532989a1856c (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=464848
-->
<head>
  <title>XMLHttpRequest send data and headers</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="setupAndRunTests();">
<a target="_blank"
   href="https://bugzilla.mozilla.org/show_bug.cgi?id=464848">Mozilla Bug 464848</a>
<p id="display">
</p>
<div id="content" style="display: none">

</div>
<pre id="test">
<script class="testbody" type="application/javascript">
SimpleTest.waitForExplicitFinish();

var testData = "blahblahblahblahblahblahblaaaaaaaah. blah.";
var extensions = [".txt",".png",".jpg",".gif",".xml", "noext"];
var fileTypes = ["text/plain", "image/png", "image/jpeg", "image/gif", "text/xml", null];
var gen;
var testDOMFiles;

function setupAndRunTests() {
  SpecialPowers.pushPrefEnv({set: [["dom.xhr.standard_content_type_normalization", true]]},
  function() {
    gen = runTests();
    createFiles();
  });
}

function createFiles() {
  var filesToCreate = [];
  extensions.forEach(function (extension) {
    filesToCreate.push({name: "testfile" + extension, data: testData});
  });
  SpecialPowers.createFiles(filesToCreate,
                            function (files) {
                              testDOMFiles = files;
                              gen.next();
                            },
                            function (msg) {
                              testDOMFiles = [];
                              ok(false, "File creation error: " + msg);
                              gen.next();
                            });
};


function continueTest() { gen.next(); }

function* runTests() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "file_XHRSendData_doc.xml", false);
xhr.send();
var testDoc1 = xhr.responseXML;
is(testDoc1.inputEncoding, "windows-1252", "wrong encoding");

var testDoc2 = document.implementation.createDocument("", "", null);
testDoc2.appendChild(testDoc2.createComment(" doc 2 "));
testDoc2.appendChild(testDoc2.createElement("res"));
testDoc2.documentElement.appendChild(testDoc2.createTextNode("text"));
is(testDoc2.inputEncoding, "UTF-8", "wrong encoding");

// arraybuffer test objects
var shortArray = new ArrayBuffer(1);
var shortInt8View = new Uint8Array(shortArray);
shortInt8View[0] = 3;

var longArray = new ArrayBuffer(512);
var longInt8View = new Uint8Array(longArray);
for (let i = 0; i < longInt8View.length; i++) {
  longInt8View[i] = i % 255;
}

// arraybufferview test objects
var longArraySlice = longArray.slice(256, 384);
var longInt32View1 = new Int32Array(longArraySlice)
var longInt32View2 = new Int32Array(longArray, 256, 32)
var longInt16View1 = new Uint16Array(longArraySlice)
var longInt16View2 = new Uint16Array(longArray, 256, 64)
var longInt8View1 = new Int8Array(longArraySlice)
var longInt8View2 = new Int8Array(longArray, 256, 128)

var tests = [{ body: null,
           resBody: "",
         },
         { body: undefined,
           resBody: "",
         },
         { body: "hi",
           resBody: "hi",
           resContentType: "text/plain;charset=UTF-8",
         },
         { body: "r\xe4ksm\xf6rg\xe5s",
           resBody: "r\xc3\xa4ksm\xc3\xb6rg\xc3\xa5s",
           resContentType: "text/plain;charset=UTF-8",
         },
         { body: "hi",
           contentType: "",
           resBody: "hi",
           resContentType: "text/plain;charset=UTF-8",
         },
         { body: "hi",
           contentType: "foo/bar",
           resBody: "hi",
           resContentType: "foo/bar",
         },
         { body: "hi",
           contentType: "foo/bar; baz=bin",
           resBody: "hi",
           resContentType: "foo/bar; baz=bin",
         },
         { body: "hi",
           contentType: "foo/bar; charset=ascii; baz=bin",
           resBody: "hi",
           resContentType: "foo/bar;charset=UTF-8;baz=bin",
         },
         { body: "hi",
           contentType: "foo/bar; charset=uTf-8",
           resBody: "hi",
           resContentType: "foo/bar; charset=uTf-8",
         },
         { body: testDoc1,
           resBody: "<!-- comment -->\n<out>hi</out>",
           resContentType: "application/xml;charset=UTF-8",
         },
         { body: testDoc1,
           contentType: "foo/bar",
           resBody: "<!-- comment -->\n<out>hi</out>",
           resContentType: "foo/bar",
         },
         { body: testDoc1,
           contentType: "foo/bar; charset=ascii; baz=bin",
           resBody: "<!-- comment -->\n<out>hi</out>",
           resContentType: "foo/bar;charset=UTF-8;baz=bin",
         },
         { body: testDoc1,
           contentType: "foo/bar; charset=wIndows-1252",
           resBody: "<!-- comment -->\n<out>hi</out>",
           resContentType: "foo/bar;charset=UTF-8",
         },
         { body: testDoc2,
           resBody: "<!-- doc 2 -->\n<res>text</res>",
           resContentType: "application/xml;charset=UTF-8",
         },
         { body: testDoc2,
           contentType: "foo/bar",
           resBody: "<!-- doc 2 -->\n<res>text</res>",
           resContentType: "foo/bar",
         },
         { body: testDoc2,
           contentType: "foo/bar; charset=ascii; baz=bin",
           resBody: "<!-- doc 2 -->\n<res>text</res>",
           resContentType: "foo/bar;charset=UTF-8;baz=bin",
         },
         { body: testDoc2,
           contentType: "foo/bar; charset=uTf-8",
           resBody: "<!-- doc 2 -->\n<res>text</res>",
           resContentType: "foo/bar; charset=uTf-8",
         },
         { //will trigger a redirect test server-side
           body: ("TEST_REDIRECT_STR&url=" + window.location.host + window.location.pathname),
           redirect: true,
         },
         { body: shortArray,
           resBody: shortArray,
           resType: "arraybuffer"
         },
         { body: longArray,
           resBody: longArray,
           resType: "arraybuffer"
         },
         { body: longInt32View1,
           resBody: longArraySlice,
           resType: "arraybuffer"
         },
         { body: longInt32View2,
           resBody: longArraySlice,
           resType: "arraybuffer"
         },
         { body: longInt16View1,
           resBody: longArraySlice,
           resType: "arraybuffer"
         },
         { body: longInt16View2,
           resBody: longArraySlice,
           resType: "arraybuffer"
         },
         { body: longInt8View1,
           resBody: longArraySlice,
           resType: "arraybuffer"
         },
         { body: longInt8View2,
           resBody: longArraySlice,
           resType: "arraybuffer"
         },
         ];

for (let i = 0; i < testDOMFiles.length; i++) {
  tests.push({ body: testDOMFiles[i],
               resBody: testData,
               resContentType: fileTypes[i],
               resContentLength: testData.length,
              });
}

try {
  for (var test of tests) {
    xhr = new XMLHttpRequest;
    xhr.open("POST", "file_XHRSendData.sjs", !!test.resType);
    if (test.contentType)
      xhr.setRequestHeader("Content-Type", test.contentType);
    if (test.resType) {
      xhr.responseType = test.resType;
      xhr.onloadend = continueTest;
    }
    xhr.send(test.body);
    if (test.resType)
      yield undefined;

    if (test.resContentType) {
      is(xhr.getResponseHeader("Result-Content-Type"), test.resContentType,
         "Wrong Content-Type sent");
    }
    else {
      is(xhr.getResponseHeader("Result-Content-Type"), null);
    }

    if (test.resContentLength) {
      is(xhr.getResponseHeader("Result-Content-Length"),
         String(test.resContentLength),
         "Wrong Content-Length sent");
    }

    if (test.resType == "arraybuffer") {
      is_identical_arraybuffer(xhr.response, test.resBody);
    }
    else if (test.body instanceof Document) {
      is(xhr.responseText.replace("\r\n", "\n"), test.resBody, "Wrong body");
    }
    else if (!test.redirect) {
      is(xhr.responseText, test.resBody, "Wrong body");
    }
    else {
      // If we're testing redirect, determine whether the body is
      // this document by looking for the relevant bug url
      is(xhr.responseText.includes("https://bugzilla.mozilla.org/show_bug.cgi?id=464848"), true,
                                  "Wrong page for redirect");
    }
  }
} catch (e) {
}

function is_identical_arraybuffer(ab1, ab2) {
  is(ab1.byteLength, ab2.byteLength, "arraybuffer byteLengths not equal");
  var u8v1 = new Uint8Array(ab1);
  var u8v2 = new Uint8Array(ab2);
  is(String.fromCharCode.apply(String, u8v1),
     String.fromCharCode.apply(String, u8v2), "arraybuffer values not equal");
}

SimpleTest.finish();
} /* runTests */
</script>
</pre>
</body>
</html>