content/base/test/test_XHRSendData.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=464848
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>XMLHttpRequest send data and headers</title>
michael@0 8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 10 </head>
michael@0 11 <body onload="gen.next();">
michael@0 12 <a target="_blank"
michael@0 13 href="https://bugzilla.mozilla.org/show_bug.cgi?id=464848">Mozilla Bug 464848</a>
michael@0 14 <p id="display">
michael@0 15 <input id="fileList" type="file"></input>
michael@0 16 </p>
michael@0 17 <div id="content" style="display: none">
michael@0 18
michael@0 19 </div>
michael@0 20 <pre id="test">
michael@0 21 <script class="testbody" type="application/javascript;version=1.8">
michael@0 22 SimpleTest.waitForExplicitFinish();
michael@0 23
michael@0 24 var gen = runTests();
michael@0 25 function continueTest() { gen.next(); }
michael@0 26
michael@0 27 function runTests() {
michael@0 28
michael@0 29 xhr = new XMLHttpRequest();
michael@0 30 xhr.open("GET", "file_XHRSendData_doc.xml", false);
michael@0 31 xhr.send();
michael@0 32 testDoc1 = xhr.responseXML;
michael@0 33 is(testDoc1.inputEncoding, "windows-1252", "wrong encoding");
michael@0 34
michael@0 35 testDoc2 = document.implementation.createDocument("", "", null);
michael@0 36 testDoc2.appendChild(testDoc2.createComment(" doc 2 "));
michael@0 37 testDoc2.appendChild(testDoc2.createElement("res"));
michael@0 38 testDoc2.documentElement.appendChild(testDoc2.createTextNode("text"));
michael@0 39 is(testDoc2.inputEncoding, null, "wrong encoding");
michael@0 40
michael@0 41 var testData = "blahblahblahblahblahblahblaaaaaaaah. blah.";
michael@0 42 var extensions = [".txt",".png",".jpg",".gif",".xml", "noext"];
michael@0 43 var fileTypes = ["text/plain", "image/png", "image/jpeg", "image/gif", "text/xml", null];
michael@0 44 var testFiles = new Array;
michael@0 45 var testDOMFiles = new Array;
michael@0 46
michael@0 47 // arraybuffer test objects
michael@0 48 var shortArray = new ArrayBuffer(1);
michael@0 49 var shortInt8View = new Uint8Array(shortArray);
michael@0 50 shortInt8View[0] = 3;
michael@0 51
michael@0 52 var longArray = new ArrayBuffer(512);
michael@0 53 var longInt8View = new Uint8Array(longArray);
michael@0 54 for (var i = 0; i < longInt8View.length; i++) {
michael@0 55 longInt8View[i] = i % 255;
michael@0 56 }
michael@0 57
michael@0 58 // arraybufferview test objects
michael@0 59 var longArraySlice = longArray.slice(256, 384);
michael@0 60 var longInt32View1 = new Int32Array(longArraySlice)
michael@0 61 var longInt32View2 = new Int32Array(longArray, 256, 32)
michael@0 62 var longInt16View1 = new Uint16Array(longArraySlice)
michael@0 63 var longInt16View2 = new Uint16Array(longArray, 256, 64)
michael@0 64 var longInt8View1 = new Int8Array(longArraySlice)
michael@0 65 var longInt8View2 = new Int8Array(longArray, 256, 128)
michael@0 66
michael@0 67 extensions.forEach(
michael@0 68 function (extension) {
michael@0 69 var testFile = createFileWithDataExt(testData, extension);
michael@0 70 testFiles.push(testFile);
michael@0 71
michael@0 72 var fileList = document.getElementById('fileList');
michael@0 73 SpecialPowers.wrap(fileList).value = testFile.path;
michael@0 74 testDOMFiles.push(fileList.files[0]);
michael@0 75 }
michael@0 76 );
michael@0 77
michael@0 78 function createFileWithDataExt(fileData, extension) {
michael@0 79 var testFile = SpecialPowers.Services.dirsvc.get("ProfD", SpecialPowers.Ci.nsIFile);
michael@0 80 testFile.append("testfile" + extension);
michael@0 81 var outStream = SpecialPowers.Cc["@mozilla.org/network/file-output-stream;1"].createInstance(SpecialPowers.Ci.nsIFileOutputStream);
michael@0 82 outStream.init(testFile, 0x02 | 0x08 | 0x20, 0666, 0);
michael@0 83 outStream.write(fileData, fileData.length);
michael@0 84 outStream.close();
michael@0 85
michael@0 86 return testFile;
michael@0 87 }
michael@0 88
michael@0 89 tests = [{ body: null,
michael@0 90 resBody: "",
michael@0 91 },
michael@0 92 { body: undefined,
michael@0 93 resBody: "",
michael@0 94 },
michael@0 95 { body: "hi",
michael@0 96 resBody: "hi",
michael@0 97 resContentType: "text/plain; charset=UTF-8",
michael@0 98 },
michael@0 99 { body: "r\xe4ksm\xf6rg\xe5s",
michael@0 100 resBody: "r\xc3\xa4ksm\xc3\xb6rg\xc3\xa5s",
michael@0 101 resContentType: "text/plain; charset=UTF-8",
michael@0 102 },
michael@0 103 { body: "hi",
michael@0 104 contentType: "",
michael@0 105 resBody: "hi",
michael@0 106 resContentType: "text/plain; charset=UTF-8",
michael@0 107 },
michael@0 108 { body: "hi",
michael@0 109 contentType: "foo/bar",
michael@0 110 resBody: "hi",
michael@0 111 resContentType: "foo/bar; charset=UTF-8",
michael@0 112 },
michael@0 113 { body: "hi",
michael@0 114 contentType: "foo/bar; baz=bin",
michael@0 115 resBody: "hi",
michael@0 116 resContentType: "foo/bar; charset=UTF-8; baz=bin",
michael@0 117 },
michael@0 118 { body: "hi",
michael@0 119 contentType: "foo/bar; charset=ascii; baz=bin",
michael@0 120 resBody: "hi",
michael@0 121 resContentType: "foo/bar; charset=UTF-8; baz=bin",
michael@0 122 },
michael@0 123 { body: "hi",
michael@0 124 contentType: "foo/bar; charset=uTf-8",
michael@0 125 resBody: "hi",
michael@0 126 resContentType: "foo/bar; charset=uTf-8",
michael@0 127 },
michael@0 128 { body: testDoc1,
michael@0 129 resBody: "<!-- comment -->\n<out>hi</out>",
michael@0 130 resContentType: "application/xml; charset=windows-1252",
michael@0 131 },
michael@0 132 { body: testDoc1,
michael@0 133 contentType: "foo/bar",
michael@0 134 resBody: "<!-- comment -->\n<out>hi</out>",
michael@0 135 resContentType: "foo/bar; charset=windows-1252",
michael@0 136 },
michael@0 137 { body: testDoc1,
michael@0 138 contentType: "foo/bar; charset=ascii; baz=bin",
michael@0 139 resBody: "<!-- comment -->\n<out>hi</out>",
michael@0 140 resContentType: "foo/bar; charset=windows-1252; baz=bin",
michael@0 141 },
michael@0 142 { body: testDoc1,
michael@0 143 contentType: "foo/bar; charset=wIndows-1252",
michael@0 144 resBody: "<!-- comment -->\n<out>hi</out>",
michael@0 145 resContentType: "foo/bar; charset=wIndows-1252",
michael@0 146 },
michael@0 147 { body: testDoc2,
michael@0 148 resBody: "<!-- doc 2 -->\n<res>text</res>",
michael@0 149 resContentType: "application/xml; charset=UTF-8",
michael@0 150 },
michael@0 151 { body: testDoc2,
michael@0 152 contentType: "foo/bar",
michael@0 153 resBody: "<!-- doc 2 -->\n<res>text</res>",
michael@0 154 resContentType: "foo/bar; charset=UTF-8",
michael@0 155 },
michael@0 156 { body: testDoc2,
michael@0 157 contentType: "foo/bar; charset=ascii; baz=bin",
michael@0 158 resBody: "<!-- doc 2 -->\n<res>text</res>",
michael@0 159 resContentType: "foo/bar; charset=UTF-8; baz=bin",
michael@0 160 },
michael@0 161 { body: testDoc2,
michael@0 162 contentType: "foo/bar; charset=uTf-8",
michael@0 163 resBody: "<!-- doc 2 -->\n<res>text</res>",
michael@0 164 resContentType: "foo/bar; charset=uTf-8",
michael@0 165 },
michael@0 166 { //will trigger a redirect test server-side
michael@0 167 body: ("TEST_REDIRECT_STR&url=" + window.location.host + window.location.pathname),
michael@0 168 redirect: true,
michael@0 169 },
michael@0 170 { body: shortArray,
michael@0 171 resBody: shortArray,
michael@0 172 resType: "arraybuffer"
michael@0 173 },
michael@0 174 { body: longArray,
michael@0 175 resBody: longArray,
michael@0 176 resType: "arraybuffer"
michael@0 177 },
michael@0 178 { body: longInt32View1,
michael@0 179 resBody: longArraySlice,
michael@0 180 resType: "arraybuffer"
michael@0 181 },
michael@0 182 { body: longInt32View2,
michael@0 183 resBody: longArraySlice,
michael@0 184 resType: "arraybuffer"
michael@0 185 },
michael@0 186 { body: longInt16View1,
michael@0 187 resBody: longArraySlice,
michael@0 188 resType: "arraybuffer"
michael@0 189 },
michael@0 190 { body: longInt16View2,
michael@0 191 resBody: longArraySlice,
michael@0 192 resType: "arraybuffer"
michael@0 193 },
michael@0 194 { body: longInt8View1,
michael@0 195 resBody: longArraySlice,
michael@0 196 resType: "arraybuffer"
michael@0 197 },
michael@0 198 { body: longInt8View2,
michael@0 199 resBody: longArraySlice,
michael@0 200 resType: "arraybuffer"
michael@0 201 },
michael@0 202 ];
michael@0 203
michael@0 204 for (var i = 0; i < testDOMFiles.length; i++) {
michael@0 205 tests.push({ body: testDOMFiles[i],
michael@0 206 resBody: testData,
michael@0 207 resContentType: fileTypes[i],
michael@0 208 resContentLength: testData.length,
michael@0 209 });
michael@0 210 }
michael@0 211
michael@0 212 try {
michael@0 213 for (test of tests) {
michael@0 214 xhr = new XMLHttpRequest;
michael@0 215 xhr.open("POST", "file_XHRSendData.sjs", !!test.resType);
michael@0 216 if (test.contentType)
michael@0 217 xhr.setRequestHeader("Content-Type", test.contentType);
michael@0 218 if (test.resType) {
michael@0 219 xhr.responseType = test.resType;
michael@0 220 xhr.onloadend = continueTest;
michael@0 221 }
michael@0 222 xhr.send(test.body);
michael@0 223 if (test.resType)
michael@0 224 yield undefined;
michael@0 225
michael@0 226 if (test.resContentType) {
michael@0 227 is(xhr.getResponseHeader("Result-Content-Type"), test.resContentType,
michael@0 228 "Wrong Content-Type sent");
michael@0 229 }
michael@0 230 else {
michael@0 231 is(xhr.getResponseHeader("Result-Content-Type"), null);
michael@0 232 }
michael@0 233
michael@0 234 if (test.resContentLength) {
michael@0 235 is(xhr.getResponseHeader("Result-Content-Length"), test.resContentLength, "Wrong Content-Length sent");
michael@0 236 }
michael@0 237
michael@0 238 if (test.resType == "arraybuffer") {
michael@0 239 is_identical_arraybuffer(xhr.response, test.resBody);
michael@0 240 }
michael@0 241 else if (test.body instanceof Document) {
michael@0 242 is(xhr.responseText.replace("\r\n", "\n"), test.resBody, "Wrong body");
michael@0 243 }
michael@0 244 else if (!test.redirect) {
michael@0 245 is(xhr.responseText, test.resBody, "Wrong body");
michael@0 246 }
michael@0 247 else {
michael@0 248 // If we're testing redirect, determine whether the body is
michael@0 249 // this document by looking for the relevant bug url
michael@0 250 is(xhr.responseText.indexOf("https://bugzilla.mozilla.org/show_bug.cgi?id=464848") >= 0, true,
michael@0 251 "Wrong page for redirect");
michael@0 252 }
michael@0 253 }
michael@0 254 }
michael@0 255 finally {
michael@0 256 cleanUpData();
michael@0 257 }
michael@0 258
michael@0 259 function cleanUpData() {
michael@0 260 testFiles.forEach(
michael@0 261 function (testFile) {
michael@0 262 try {
michael@0 263 testFile.remove(false);
michael@0 264 } catch (e) {}
michael@0 265 }
michael@0 266 );
michael@0 267 }
michael@0 268
michael@0 269 function is_identical_arraybuffer(ab1, ab2) {
michael@0 270 is(ab1.byteLength, ab2.byteLength, "arraybuffer byteLengths not equal");
michael@0 271 u8v1 = new Uint8Array(ab1);
michael@0 272 u8v2 = new Uint8Array(ab2);
michael@0 273 is(String.fromCharCode.apply(String, u8v1),
michael@0 274 String.fromCharCode.apply(String, u8v2), "arraybuffer values not equal");
michael@0 275 }
michael@0 276
michael@0 277 SimpleTest.finish();
michael@0 278 yield undefined;
michael@0 279 } /* runTests */
michael@0 280
michael@0 281 </script>
michael@0 282 </pre>
michael@0 283 </body>
michael@0 284 </html>

mercurial