<!DOCTYPE html>

<link rel="author" href="mtrzos@google.com" title="Maciek Trzos">
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src=/fetch/sec-metadata/resources/helper.js></script>
<body></body>
<script>
promise_test(t => {
    return new Promise((resolve, reject) => {
      let key = "redirect-multiple-cross-site";

      let e = document.createElement('img');
      e.src = "https://{{host}}:{{ports[https][0]}}/xhr/resources/redirect.py?location=" +// same-origin
      "https://{{hosts[alt][www]}}:{{ports[https][0]}}/xhr/resources/redirect.py?location=" +// cross-site
      "https://{{host}}:{{ports[https][0]}}/fetch/sec-metadata/resources/record-header.py?file=" + key;// same-origin
      let expected = {"destination":"image", "site":"cross-site"};

      e.onload = e => {
        fetch("/fetch/sec-metadata/resources/record-header.py?retrieve=true&file=" + key)
          .then(response => response.text())
          .then(text => assert_header_equals(text, expected))
          .then(_ => resolve())
          .catch(e => reject(e));
      };
      e.onerror = e => {
        fetch("/fetch/sec-metadata/resources/record-header.py?retrieve=true&file=" + key)
          .then(response => response.text())
          .then(text => assert_header_equals(text, expected))
          .then(_ => resolve())
          .catch(e => reject(e));
      };

      document.body.appendChild(e);
    })
  }, "Same-Origin -> Cross-Site -> Same-Origin redirect");
</script>
