summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/metadata/tools/templates/fetch-via-serviceworker.https.sub.html
blob: eead7102008d1fb8fd0867ee0287bdffce238d20 (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!DOCTYPE html>
<!--
[%provenance%]
-->
<html lang="en">
  <meta charset="utf-8">
  {%- if subtests|length > 10 %}
  <meta name="timeout" content="long">
  {%- endif %}
  <title>HTTP headers on request using the "fetch" API and passing through a Serive Worker</title>
  <script src="/resources/testharness.js"></script>
  <script src="/resources/testharnessreport.js"></script>
  <script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script>
  <script src="/fetch/metadata/resources/helper.sub.js"></script>
  <body>
  <script>
  'use strict';

  const scripts = {
    fallback: '/fetch/metadata/resources/fetch-via-serviceworker--fallback--sw.js',
    respondWith: '/fetch/metadata/resources/fetch-via-serviceworker--respondWith--sw.js'
  };

  function induceRequest(t, url, init, script) {
    const SCOPE = '/fetch/metadata/resources/fetch-via-serviceworker-frame.html';
    const SCRIPT = scripts[script];

    return service_worker_unregister_and_register(t, SCRIPT, SCOPE)
      .then((registration) => {
        t.add_cleanup(() => registration.unregister());

        return wait_for_state(t, registration.installing, 'activated');
      })
      .then(() => with_iframe(SCOPE))
      .then((frame) => {
        t.add_cleanup(() => frame.remove());

        return frame.contentWindow.fetch(url, init);
      });
  }

  {%- for subtest in subtests %}

  promise_test((t) => {
    const key = '{{uuid()}}';

    return induceRequest(
        t,
        makeRequestURL(key, [% subtest.origins %]),
        [%subtest.init | default({}) | tojson%],
        'respondWith'
      )
      .then(() => retrieve(key))
      .then((headers) => {
        {%- if subtest.expected == none %}
          assert_not_own_property(headers, '[%subtest.headerName%]');
        {%- else %}
          assert_own_property(headers, '[%subtest.headerName%]');
          assert_array_equals(headers['[%subtest.headerName%]'], ['[%subtest.expected%]']);
        {%- endif %}
        });
  }, '[%subtest.headerName%] - [%subtest.description | pad("end", ", ")%][%subtest.init | collection("init")%] - respondWith');

  promise_test((t) => {
    const key = '{{uuid()}}';

    return induceRequest(
        t,
        makeRequestURL(key, [% subtest.origins %]),
        [%subtest.init | default({}) | tojson%],
        'fallback'
      )
      .then(() => retrieve(key))
      .then((headers) => {
        {%- if subtest.expected == none %}
          assert_not_own_property(headers, '[%subtest.headerName%]');
        {%- else %}
          assert_own_property(headers, '[%subtest.headerName%]');
          assert_array_equals(headers['[%subtest.headerName%]'], ['[%subtest.expected%]']);
        {%- endif %}
        });
  }, '[%subtest.headerName%] - [%subtest.description | pad("end", ", ")%][%subtest.init | collection("init")%] - fallback');

  {%- endfor %}

  </script>
  </body>
</html>