summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/fetch-later/resources/header-referrer-helper.js
blob: 374097614ae92646ab6260948abcc64cbdaf4d8d (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
'use strict';

// https://w3c.github.io/webappsec-referrer-policy/#determine-requests-referrer
const REFERRER_ORIGIN = self.location.origin + '/';
const REFERRER_URL = self.location.href;

function testReferrerHeader(id, host, expectedReferrer) {
  const url = `${
      host}/beacon/resources/inspect-header.py?header=referer&cmd=put&id=${id}`;

  promise_test(t => {
    fetchLater(url, {activateAfter: 0});
    return pollResult(expectedReferrer, id).then(result => {
      assert_equals(result, expectedReferrer, 'Correct referrer header result');
    });
  }, `Test referer header ${host}`);
}

function pollResult(expectedReferrer, id) {
  const checkUrl =
      `/beacon/resources/inspect-header.py?header=referer&cmd=get&id=${id}`;

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

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