<!DOCTYPE html>
<meta charset="utf-8"/>
<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>
  <script>
      if ('serviceWorker' in navigator) {
        window.addEventListener('load', function() {
          navigator.serviceWorker.register('https://{{host}}:{{ports[https][0]}}/fetch/sec-metadata/resources/record-header.py?file=serviceworker-same-origin').then(function(registration) {
            test_same_origin();

            // uninstall the serviceworker after the test
            navigator.serviceWorker.getRegistrations().then(function(registrations) {
              for(let registration of registrations) {
                registration.unregister()
              }
            })
          }, function(err) {
            // registration failed
          });
        });
      }
      else {
        test(function () {
              done();
            }, "Browser does not support serviceworker");
      }
  </script>
</body>

<script>
  function test_same_origin(){
    promise_test(t => {
    return new Promise((resolve, reject) => {
      let key = "serviceworker-same-origin";
      let expected = {"destination":"serviceworker", "site":"same-origin"};
      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));
      })
    })
  }
</script>
