3 <script src=
"/resources/testharness.js"></script>
4 <script src=
"/resources/testharnessreport.js"></script>
5 <script src=
"early-hints-helpers.sub.js"></script>
8 const SEARCH_PARAMS
= new URLSearchParams(window
.location
.search
);
9 const REFERRER_POLICY
= SEARCH_PARAMS
.get("referrer-policy");
11 async
function get_fetch_timing_and_headers(url_string
) {
12 const url
= new URL(url_string
);
13 const id
= url
.searchParams
.get("id");
15 throw new Error(`"${url.href}" does not contain id parameter`);
17 const response
= await
fetch(`${url.origin}/loading/early-hints/resources/get-fetch-timing-and-headers.h2.py?id=${id}`);
18 const json
= await response
.json();
22 function get_expected_referrer(is_same_origin
) {
23 const full
= window
.location
.href
;
24 const origin
= self
.origin
+ "/";
25 // There is no support for security level related policies such as
26 // "no-referrer-when-downgrade" since the test is available only on HTTP/2.
27 switch (REFERRER_POLICY
) {
32 case "origin-when-cross-origin":
33 return is_same_origin
? full
: origin
;
35 return is_same_origin
? full
: undefined;
39 throw new Error(`Unsupported referrer policy: ${REFERRER_POLICY}`);
43 async
function check_referrer(url
, expected_referrer
) {
44 await
fetchScript(url
);
46 const { headers
} = await
get_fetch_timing_and_headers(url
);
47 assert_equals(headers
["referer"], expected_referrer
);
49 const name
= new URL(url
, window
.location
);
50 assert_true(isPreloadedByEarlyHints(name
));
53 promise_test(async (t
) => {
54 const same_origin_preload_url
= SEARCH_PARAMS
.get("same-origin-preload-url");
55 const same_origin_expected
= get_expected_referrer(true);
56 await
check_referrer(same_origin_preload_url
, same_origin_expected
);
58 const cross_origin_preload_url
= SEARCH_PARAMS
.get("cross-origin-preload-url");
59 const cross_origin_expected
= get_expected_referrer(false);
60 await
check_referrer(cross_origin_preload_url
, cross_origin_expected
);
61 }, `Referrer policy: ${REFERRER_POLICY}`);