<!DOCTYPE html>
<title>Test `Referer` header</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/utils.js"></script>
<script src="/common/utils.js"></script>

<body>

<script>
promise_test(async () => {
  const referrer_key = token();
  const referrer_ack_key = token();

  const check_url = generateURL('resources/check-header-referrer.py',
      [referrer_key])
  attachFencedFrame(check_url);

  const expected_referrer = "";
  const actual_referrer = await nextValueFromServer(referrer_key);
  assert_equals(actual_referrer, expected_referrer,
                "The top-level fenced frame has the right value for " +
                "`Referer` header");

  const inner_url = generateURL('resources/header-referrer-inner.html',
      [referrer_key, referrer_ack_key])
  attachFencedFrame(inner_url);

  const iframe_expected_referrer = inner_url.toString().replace("%2C", ",");

  const iframe_actual_referrer = await nextValueFromServer(referrer_key);
  assert_equals(iframe_actual_referrer, iframe_expected_referrer,
                "The iframe inside the fenced frame has the right value for " +
                "`Referer` header");

  writeValueToServer(referrer_ack_key, "ACK");

  const nested_fenced_frame_expected_referrer = "";
  const nested_fenced_frame_actual_referrer = await nextValueFromServer(referrer_key);
  assert_equals(nested_fenced_frame_actual_referrer, nested_fenced_frame_expected_referrer,
                "The nested fenced frame has the right value for " +
                "`Referer` header");

  writeValueToServer(referrer_ack_key, "ACK");

  const navigate_self_expected_referrer =
      inner_url.toString().replace("%2C", ",");
  const navigate_self_actual_referrer = await nextValueFromServer(referrer_key);
  assert_equals(navigate_self_actual_referrer, navigate_self_expected_referrer,
                "The fenced frame after it navigates itself has the right value for " +
                "`Referer` header");
}, "header.referrer");
</script>

</body>
