summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/loading/early-hints/resources/referrer-policy-test.html
blob: d0389c2e11cce36ae0ed0ddee454809e7360b539 (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
<!DOCTYPE html>
<meta charset=utf-8>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="early-hints-helpers.sub.js"></script>
<body>
<script>
const SEARCH_PARAMS = new URLSearchParams(window.location.search);
const REFERRER_POLICY = SEARCH_PARAMS.get("referrer-policy");

async function get_fetch_timing_and_headers(url_string) {
    const url = new URL(url_string);
    const id = url.searchParams.get("id");
    if (id === null) {
        throw new Error(`"${url.href}" does not contain id parameter`);
    }
    const response = await fetch(`${url.origin}/loading/early-hints/resources/get-fetch-timing-and-headers.h2.py?id=${id}`);
    const json = await response.json();
    return json;
}

function get_expected_referrer(is_same_origin) {
    const full = window.location.href;
    const origin = self.origin + "/";
    // There is no support for security level related policies such as
    // "no-referrer-when-downgrade" since the test is available only on HTTP/2.
    switch (REFERRER_POLICY) {
        case "no-referrer":
            return undefined;
        case "origin":
            return origin;
        case "origin-when-cross-origin":
            return is_same_origin ? full : origin;
        case "same-origin":
            return is_same_origin ? full : undefined;
        case "unsafe-url":
            return full;
        default:
            throw new Error(`Unsupported referrer policy: ${REFERRER_POLICY}`);
    }
}

async function check_referrer(url, expected_referrer) {
    await fetchScript(url);

    const { headers } = await get_fetch_timing_and_headers(url);
    assert_equals(headers["referer"], expected_referrer);

    const name = new URL(url, window.location);
    assert_true(isPreloadedByEarlyHints(name));
}

promise_test(async (t) => {
    const same_origin_preload_url = SEARCH_PARAMS.get("same-origin-preload-url");
    const same_origin_expected = get_expected_referrer(true);
    await check_referrer(same_origin_preload_url, same_origin_expected);

    const cross_origin_preload_url = SEARCH_PARAMS.get("cross-origin-preload-url");
    const cross_origin_expected = get_expected_referrer(false);
    await check_referrer(cross_origin_preload_url, cross_origin_expected);
}, `Referrer policy: ${REFERRER_POLICY}`);
</script>
</body>