dom/xhr/tests/test_XHRSendData.html
author moz-wptsync-bot <wptsync@mozilla.com>
Mon, 16 Apr 2018 18:38:41 +0000
changeset 1486024 3a644abd5eaa108f67471c0aae4fb4a424ea41bf
parent 1412709 d5a5ad1dbbf2c53a80386e7397ba6b32153b2b8e
child 1697809 1d3af66c60f8d6607e3052eb93e506fa0589963e
child 1702735 dd3558a8f7ae3211ac8a9ad5f87196d6a00e7a95
permissions -rw-r--r--
Bug 1454380 [wpt PR 8508] - Update wpt metadata, a=testonly wpt-pr: 8508 wpt-type: metadata

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=464848
-->
<head>
  <title>XMLHttpRequest send data and headers</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body onload="createFiles();">
<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 = runTests();
var testDOMFiles;

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


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

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

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 (var 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)

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 (var i = 0; i < testDOMFiles.length; i++) {
  tests.push({ body: testDOMFiles[i],
               resBody: testData,
               resContentType: fileTypes[i],
               resContentLength: testData.length,
              });
}

try {
  for (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");
  u8v1 = new Uint8Array(ab1);
  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>