content/base/test/test_XHRSendData.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial