toolkit/components/social/test/browser/worker_xhr.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/social/test/browser/worker_xhr.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,34 @@
     1.4 +// Used to test XHR in the worker.
     1.5 +onconnect = function(e) {
     1.6 +  let port = e.ports[0];
     1.7 +  let req;
     1.8 +  try {
     1.9 +    req = new XMLHttpRequest();
    1.10 +  } catch(e) {
    1.11 +    port.postMessage({topic: "done", result: "FAILED to create XHR object, " + e.toString() });
    1.12 +  }
    1.13 +  if (req === undefined) { // until bug 756173 is fixed...
    1.14 +    port.postMessage({topic: "done", result: "FAILED to create XHR object"});
    1.15 +    return;
    1.16 +  }
    1.17 +  // The test that uses this worker MUST use the same origin to load the worker.
    1.18 +  // We fetch the test app manifest so we can check the data is what we expect.
    1.19 +  let url = "https://example.com/browser/toolkit/components/social/test/browser/data.json";
    1.20 +  req.open("GET", url, true);
    1.21 +  req.onreadystatechange = function() {
    1.22 +    if (req.readyState === 4) {
    1.23 +      let ok = req.status == 200 && req.responseText.length > 0;
    1.24 +      if (ok) {
    1.25 +        // check we actually got something sane...
    1.26 +        try {
    1.27 +          let data = JSON.parse(req.responseText);
    1.28 +          ok = "response" in data;
    1.29 +        } catch(e) {
    1.30 +          ok = e.toString();
    1.31 +        }
    1.32 +      }
    1.33 +      port.postMessage({topic: "done", result: ok ? "ok" : "bad response"});
    1.34 +    }
    1.35 +  }
    1.36 +  req.send(null);
    1.37 +}

mercurial