summaryrefslogtreecommitdiffstats
path: root/dom/media/test/test_midflight_redirect_blocked.html
blob: ea85673b45178ee43431708014053f6f6b54ac8c (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<!DOCTYPE HTML>
<html>
  <head>
    <title>Test mid-flight cross site redirects are blocked</title>
    <script type="text/javascript" src="/MochiKit/MochiKit.js"></script>
    <script src="/tests/SimpleTest/SimpleTest.js"></script>
    <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    <script type="text/javascript" src="manifest.js"></script>
  </head>
  <body>
    <pre id='test'>
    <script class="testbody" type='application/javascript'>

      function testIfLoadsToMetadata(test, useCors) {
        return new Promise(function(resolve, reject) {
          var elemType = getMajorMimeType(test.type);
          var element = document.createElement(elemType);

          if (useCors) {
            element.crossOrigin = "anonymous";
          }

          // Log events for debugging.
          [
            "suspend", "play", "canplay", "canplaythrough", "loadstart",
            "loadedmetadata", "loadeddata", "playing", "ended", "error",
            "stalled", "emptied", "abort", "waiting", "pause"
          ].forEach((eventName) => {
            element.addEventListener(eventName, (event)=> {
              info(test.name + " " + event.type);
            });
          });

          element.addEventListener("loadedmetadata", ()=>{
            resolve(true);
            removeNodeAndSource(element);
          });

          element.addEventListener("error", ()=>{
            resolve(false);
            removeNodeAndSource(element);
          });

          // Note: request redirect before the end of metadata, otherwise we won't
          // error before metadata has loaded if mixed origins are allowed.
          element.src = "midflight-redirect.sjs?resource=" + test.name
                      + (useCors ? "&cors" : "")
                      + "&type=" + test.type
                      + "&redirectAt=200";
          element.preload = "metadata";
          document.body.appendChild(element);
          element.load()
        });
      }

      let v = document.createElement("video");
      const testCases = gSmallTests.filter(t => v.canPlayType(t.type));

      async function testMediaLoad(message, {useCors}) {
        for (let test of testCases) {
          let loaded = await testIfLoadsToMetadata(test, useCors);
          is(loaded, useCors, test.name + " " + message);
        }
      }

      async function runTest() {
        try {
          SimpleTest.info("Test that all media do not play without CORS...");
          await testMediaLoad("expected to be blocked", {useCors: false});

          SimpleTest.info("Test that all media play if CORS used...");
          await testMediaLoad("expected to play with CORS", {useCors: true});
        } catch (e) {
          info("Exception " + e.message);
          ok(false, "Threw exception " + e.message);
        }
        SimpleTest.finish();
      }

      SimpleTest.waitForExplicitFinish();

      addLoadEvent(runTest);

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