content/base/test/test_XHRSendData.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/test_XHRSendData.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,284 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=464848
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>XMLHttpRequest send data and headers</title>
    1.11 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
    1.12 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.13 +</head>
    1.14 +<body onload="gen.next();">
    1.15 +<a target="_blank"
    1.16 +   href="https://bugzilla.mozilla.org/show_bug.cgi?id=464848">Mozilla Bug 464848</a>
    1.17 +<p id="display">
    1.18 +  <input id="fileList" type="file"></input>
    1.19 +</p>
    1.20 +<div id="content" style="display: none">
    1.21 +  
    1.22 +</div>
    1.23 +<pre id="test">
    1.24 +<script class="testbody" type="application/javascript;version=1.8">
    1.25 +SimpleTest.waitForExplicitFinish();
    1.26 +
    1.27 +var gen = runTests();
    1.28 +function continueTest() { gen.next(); }
    1.29 +
    1.30 +function runTests() {
    1.31 +
    1.32 +xhr = new XMLHttpRequest();
    1.33 +xhr.open("GET", "file_XHRSendData_doc.xml", false);
    1.34 +xhr.send();
    1.35 +testDoc1 = xhr.responseXML;
    1.36 +is(testDoc1.inputEncoding, "windows-1252", "wrong encoding");
    1.37 +
    1.38 +testDoc2 = document.implementation.createDocument("", "", null);
    1.39 +testDoc2.appendChild(testDoc2.createComment(" doc 2 "));
    1.40 +testDoc2.appendChild(testDoc2.createElement("res"));
    1.41 +testDoc2.documentElement.appendChild(testDoc2.createTextNode("text"));
    1.42 +is(testDoc2.inputEncoding, null, "wrong encoding");
    1.43 +
    1.44 +var testData = "blahblahblahblahblahblahblaaaaaaaah. blah.";
    1.45 +var extensions = [".txt",".png",".jpg",".gif",".xml", "noext"];
    1.46 +var fileTypes = ["text/plain", "image/png", "image/jpeg", "image/gif", "text/xml", null];
    1.47 +var testFiles = new Array;
    1.48 +var testDOMFiles = new Array;
    1.49 +
    1.50 +// arraybuffer test objects
    1.51 +var shortArray = new ArrayBuffer(1);
    1.52 +var shortInt8View = new Uint8Array(shortArray);
    1.53 +shortInt8View[0] = 3;
    1.54 +
    1.55 +var longArray = new ArrayBuffer(512);
    1.56 +var longInt8View = new Uint8Array(longArray);
    1.57 +for (var i = 0; i < longInt8View.length; i++) {
    1.58 +  longInt8View[i] = i % 255;
    1.59 +}
    1.60 +
    1.61 +// arraybufferview test objects
    1.62 +var longArraySlice = longArray.slice(256, 384);
    1.63 +var longInt32View1 = new Int32Array(longArraySlice)
    1.64 +var longInt32View2 = new Int32Array(longArray, 256, 32)
    1.65 +var longInt16View1 = new Uint16Array(longArraySlice)
    1.66 +var longInt16View2 = new Uint16Array(longArray, 256, 64)
    1.67 +var longInt8View1 = new Int8Array(longArraySlice)
    1.68 +var longInt8View2 = new Int8Array(longArray, 256, 128)
    1.69 +
    1.70 +extensions.forEach(
    1.71 +    function (extension) {
    1.72 +      var testFile = createFileWithDataExt(testData, extension);
    1.73 +      testFiles.push(testFile);
    1.74 +
    1.75 +      var fileList = document.getElementById('fileList');
    1.76 +      SpecialPowers.wrap(fileList).value = testFile.path;
    1.77 +      testDOMFiles.push(fileList.files[0]);
    1.78 +    }
    1.79 +);
    1.80 +
    1.81 +function createFileWithDataExt(fileData, extension) {
    1.82 +  var testFile = SpecialPowers.Services.dirsvc.get("ProfD", SpecialPowers.Ci.nsIFile);
    1.83 +  testFile.append("testfile" + extension);
    1.84 +  var outStream = SpecialPowers.Cc["@mozilla.org/network/file-output-stream;1"].createInstance(SpecialPowers.Ci.nsIFileOutputStream);
    1.85 +  outStream.init(testFile, 0x02 | 0x08 | 0x20, 0666, 0);
    1.86 +  outStream.write(fileData, fileData.length);
    1.87 +  outStream.close();
    1.88 +
    1.89 +  return testFile;
    1.90 +}
    1.91 +
    1.92 +tests = [{ body: null,
    1.93 +           resBody: "",
    1.94 +         },
    1.95 +         { body: undefined,
    1.96 +           resBody: "",
    1.97 +         },
    1.98 +         { body: "hi",
    1.99 +           resBody: "hi",
   1.100 +           resContentType: "text/plain; charset=UTF-8",
   1.101 +         },
   1.102 +         { body: "r\xe4ksm\xf6rg\xe5s",
   1.103 +           resBody: "r\xc3\xa4ksm\xc3\xb6rg\xc3\xa5s",
   1.104 +           resContentType: "text/plain; charset=UTF-8",
   1.105 +         },
   1.106 +         { body: "hi",
   1.107 +           contentType: "",
   1.108 +           resBody: "hi",
   1.109 +           resContentType: "text/plain; charset=UTF-8",
   1.110 +         },
   1.111 +         { body: "hi",
   1.112 +           contentType: "foo/bar",
   1.113 +           resBody: "hi",
   1.114 +           resContentType: "foo/bar; charset=UTF-8",
   1.115 +         },
   1.116 +         { body: "hi",
   1.117 +           contentType: "foo/bar; baz=bin",
   1.118 +           resBody: "hi",
   1.119 +           resContentType: "foo/bar; charset=UTF-8; baz=bin",
   1.120 +         },
   1.121 +         { body: "hi",
   1.122 +           contentType: "foo/bar; charset=ascii; baz=bin",
   1.123 +           resBody: "hi",
   1.124 +           resContentType: "foo/bar; charset=UTF-8; baz=bin",
   1.125 +         },
   1.126 +         { body: "hi",
   1.127 +           contentType: "foo/bar; charset=uTf-8",
   1.128 +           resBody: "hi",
   1.129 +           resContentType: "foo/bar; charset=uTf-8",
   1.130 +         },
   1.131 +         { body: testDoc1,
   1.132 +           resBody: "<!-- comment -->\n<out>hi</out>",
   1.133 +           resContentType: "application/xml; charset=windows-1252",
   1.134 +         },
   1.135 +         { body: testDoc1,
   1.136 +           contentType: "foo/bar",
   1.137 +           resBody: "<!-- comment -->\n<out>hi</out>",
   1.138 +           resContentType: "foo/bar; charset=windows-1252",
   1.139 +         },
   1.140 +         { body: testDoc1,
   1.141 +           contentType: "foo/bar; charset=ascii; baz=bin",
   1.142 +           resBody: "<!-- comment -->\n<out>hi</out>",
   1.143 +           resContentType: "foo/bar; charset=windows-1252; baz=bin",
   1.144 +         },
   1.145 +         { body: testDoc1,
   1.146 +           contentType: "foo/bar; charset=wIndows-1252",
   1.147 +           resBody: "<!-- comment -->\n<out>hi</out>",
   1.148 +           resContentType: "foo/bar; charset=wIndows-1252",
   1.149 +         },
   1.150 +         { body: testDoc2,
   1.151 +           resBody: "<!-- doc 2 -->\n<res>text</res>",
   1.152 +           resContentType: "application/xml; charset=UTF-8",
   1.153 +         },
   1.154 +         { body: testDoc2,
   1.155 +           contentType: "foo/bar",
   1.156 +           resBody: "<!-- doc 2 -->\n<res>text</res>",
   1.157 +           resContentType: "foo/bar; charset=UTF-8",
   1.158 +         },
   1.159 +         { body: testDoc2,
   1.160 +           contentType: "foo/bar; charset=ascii; baz=bin",
   1.161 +           resBody: "<!-- doc 2 -->\n<res>text</res>",
   1.162 +           resContentType: "foo/bar; charset=UTF-8; baz=bin",
   1.163 +         },
   1.164 +         { body: testDoc2,
   1.165 +           contentType: "foo/bar; charset=uTf-8",
   1.166 +           resBody: "<!-- doc 2 -->\n<res>text</res>",
   1.167 +           resContentType: "foo/bar; charset=uTf-8",
   1.168 +         },
   1.169 +         { //will trigger a redirect test server-side
   1.170 +           body: ("TEST_REDIRECT_STR&url=" + window.location.host + window.location.pathname),
   1.171 +           redirect: true,
   1.172 +         },
   1.173 +         { body: shortArray,
   1.174 +           resBody: shortArray,
   1.175 +           resType: "arraybuffer"
   1.176 +         },
   1.177 +         { body: longArray,
   1.178 +           resBody: longArray,
   1.179 +           resType: "arraybuffer"
   1.180 +         },
   1.181 +         { body: longInt32View1,
   1.182 +           resBody: longArraySlice,
   1.183 +           resType: "arraybuffer"
   1.184 +         },
   1.185 +         { body: longInt32View2,
   1.186 +           resBody: longArraySlice,
   1.187 +           resType: "arraybuffer"
   1.188 +         },
   1.189 +         { body: longInt16View1,
   1.190 +           resBody: longArraySlice,
   1.191 +           resType: "arraybuffer"
   1.192 +         },
   1.193 +         { body: longInt16View2,
   1.194 +           resBody: longArraySlice,
   1.195 +           resType: "arraybuffer"
   1.196 +         },
   1.197 +         { body: longInt8View1,
   1.198 +           resBody: longArraySlice,
   1.199 +           resType: "arraybuffer"
   1.200 +         },
   1.201 +         { body: longInt8View2,
   1.202 +           resBody: longArraySlice,
   1.203 +           resType: "arraybuffer"
   1.204 +         },
   1.205 +         ];
   1.206 +
   1.207 +for (var i = 0; i < testDOMFiles.length; i++) {
   1.208 +  tests.push({ body: testDOMFiles[i],
   1.209 +               resBody: testData,
   1.210 +               resContentType: fileTypes[i],
   1.211 +               resContentLength: testData.length,
   1.212 +              });
   1.213 +}
   1.214 +
   1.215 +try {
   1.216 +  for (test of tests) {
   1.217 +    xhr = new XMLHttpRequest;
   1.218 +    xhr.open("POST", "file_XHRSendData.sjs", !!test.resType);
   1.219 +    if (test.contentType)
   1.220 +      xhr.setRequestHeader("Content-Type", test.contentType);
   1.221 +    if (test.resType) {
   1.222 +      xhr.responseType = test.resType;
   1.223 +      xhr.onloadend = continueTest;
   1.224 +    }
   1.225 +    xhr.send(test.body);
   1.226 +    if (test.resType)
   1.227 +      yield undefined;
   1.228 +
   1.229 +    if (test.resContentType) {
   1.230 +      is(xhr.getResponseHeader("Result-Content-Type"), test.resContentType,
   1.231 +         "Wrong Content-Type sent");
   1.232 +    }
   1.233 +    else {
   1.234 +      is(xhr.getResponseHeader("Result-Content-Type"), null);
   1.235 +    }
   1.236 +
   1.237 +    if (test.resContentLength) {
   1.238 +      is(xhr.getResponseHeader("Result-Content-Length"), test.resContentLength, "Wrong Content-Length sent");
   1.239 +    }
   1.240 +
   1.241 +    if (test.resType == "arraybuffer") {
   1.242 +      is_identical_arraybuffer(xhr.response, test.resBody);
   1.243 +    }
   1.244 +    else if (test.body instanceof Document) {
   1.245 +      is(xhr.responseText.replace("\r\n", "\n"), test.resBody, "Wrong body");
   1.246 +    }
   1.247 +    else if (!test.redirect) {
   1.248 +      is(xhr.responseText, test.resBody, "Wrong body");
   1.249 +    }
   1.250 +    else {
   1.251 +      // If we're testing redirect, determine whether the body is
   1.252 +      // this document by looking for the relevant bug url
   1.253 +      is(xhr.responseText.indexOf("https://bugzilla.mozilla.org/show_bug.cgi?id=464848") >= 0, true,
   1.254 +                                  "Wrong page for redirect");
   1.255 +    }
   1.256 +  }
   1.257 +}
   1.258 +finally {
   1.259 +  cleanUpData();
   1.260 +}
   1.261 +
   1.262 +function cleanUpData() {
   1.263 +  testFiles.forEach(
   1.264 +      function (testFile) {
   1.265 +        try {
   1.266 +          testFile.remove(false);
   1.267 +        } catch (e) {}
   1.268 +      }
   1.269 +  );
   1.270 +}
   1.271 +
   1.272 +function is_identical_arraybuffer(ab1, ab2) {
   1.273 +  is(ab1.byteLength, ab2.byteLength, "arraybuffer byteLengths not equal");
   1.274 +  u8v1 = new Uint8Array(ab1);
   1.275 +  u8v2 = new Uint8Array(ab2);
   1.276 +  is(String.fromCharCode.apply(String, u8v1),
   1.277 +     String.fromCharCode.apply(String, u8v2), "arraybuffer values not equal");
   1.278 +}
   1.279 +
   1.280 +SimpleTest.finish();
   1.281 +yield undefined;
   1.282 +} /* runTests */
   1.283 +
   1.284 +</script>
   1.285 +</pre>
   1.286 +</body>
   1.287 +</html>

mercurial