summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/beacon/test_beaconRedirect.html
blob: 210b7a1e98cff41d677ff70bcca9bb2191d2396e (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
56
57
<!DOCTYPE HTML>
<html>
<head>
  <title>Bug 1280692 - sendBeacon() should follow 30x redirect</title>
  <!-- Including SimpleTest.js so we can use waitForExplicitFinish !-->
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
  <p id="display"></p>
  <div id="content" style="visibility: hidden">
    <iframe style="width:100%;" id="testframe"></iframe>
  </div>

<script class="testbody" type="text/javascript">

/* Description of the test:
 *   We do perform a non simple sendBeacon request which should not use CORS and should follow
 *   a 30x cross origin redirect, which is allowed by the spec.
 */

SimpleTest.waitForExplicitFinish();

const BEACON_URL = "http://example.com/tests/dom/tests/mochitest/beacon/beacon-redirect-handler.sjs?beacon";

SpecialPowers.pushPrefEnv({'set': [["beacon.enabled", true]]}, runTest);

var intervalID = null;

function queryIfRedirectSucceeded() {
  clearInterval(intervalID);
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "beacon-redirect-handler.sjs?verifyRedirectDidSucceed", true);
  xhr.onload = function() {
    is(xhr.responseText, "green", "SendBeacon should follow cross origin redirects!");
    SimpleTest.finish();
  };
  xhr.onerror = function() {
    ok(false, "xhr request returned error");
    SimpleTest.finish();
  };
  xhr.send();
}

function runTest() {
  var data = new Uint8Array([0,1,2,3]);
  navigator.sendBeacon(BEACON_URL, data);

  // we have to make sure the channel did follow the redirect hence
  // we have to wait for 4 seconds before we can query the result.
  intervalID = setInterval(queryIfRedirectSucceeded, 4000);
}

</script>
</pre>
</body>
</html>