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