summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_xmlhttprequest.js
blob: 6e478effc886df0c924f6bfdddd17aa83595d38a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"use strict";

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");

var httpserver = new HttpServer();
var testpath = "/simple";
var httpbody = "<?xml version='1.0' ?><root>0123456789</root>";

function createXHR(async) {
  var xhr = new XMLHttpRequest();
  xhr.open(
    "GET",
    "http://localhost:" + httpserver.identity.primaryPort + testpath,
    async
  );
  return xhr;
}

function checkResults(xhr) {
  if (xhr.readyState != 4) {
    return false;
  }

  Assert.equal(xhr.status, 200);
  Assert.equal(xhr.responseText, httpbody);

  var root_node = xhr.responseXML.getElementsByTagName("root").item(0);
  Assert.equal(root_node.firstChild.data, "0123456789");
  return true;
}

function run_test() {
  httpserver.registerPathHandler(testpath, serverHandler);
  httpserver.start(-1);

  // Test sync XHR sending
  var sync = createXHR(false);
  sync.send(null);
  checkResults(sync);

  // Test async XHR sending
  let async = createXHR(true);
  async.addEventListener("readystatechange", function (event) {
    if (checkResults(async)) {
      httpserver.stop(do_test_finished);
    }
  });
  async.send(null);
  do_test_pending();
}

function serverHandler(metadata, response) {
  response.setHeader("Content-Type", "text/xml", false);
  response.bodyOutputStream.write(httpbody, httpbody.length);
}