summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/links/downloading-resources/header-origin.js
blob: acc62ef93b0592540942ce0be70ceb8c51c3fad7 (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
const RESOURCES_DIR = "/html/semantics/links/downloading-resources/resources/";

function testOriginHeader(expectedOrigin) {
  var id = self.token();
  let testUrl = RESOURCES_DIR + "inspect-header.py?header=origin&cmd=put&id=" + id;

  promise_test(function(test) {
    const anchor = document.getElementById("a");
    anchor.setAttribute("ping", testUrl);
    anchor.click();
    return pollResult(id) .then(result => {
      assert_equals(result, expectedOrigin, "Correct origin header result");
    });
  }, "Test origin header " + RESOURCES_DIR);
}

// Sending a ping is an asynchronous and non-blocking request to a web server.
// We may have to create a poll loop to get result from server
function pollResult(id) {
  let checkUrl = RESOURCES_DIR + "inspect-header.py?header=origin&cmd=get&id=" + id;

  return new Promise(resolve => {
    function checkResult() {
      fetch(checkUrl).then(
        function(response) {
          assert_equals(response.status, 200, "Inspect header response's status is 200");
          let result = response.headers.get("x-request-origin");

          if (result != undefined) {
            resolve(result);
          } else {
            step_timeout(checkResult.bind(this), 100);
          }
        });
    }

    checkResult();
  });

}