summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/local-url-inherit-controller.https.html
blob: 6702abcadbb18958af4cb581d6286685f90b60c4 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE html>
<title>Service Worker: local URL windows and workers inherit controller</title>
<meta name=timeout content=long>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<body>
<script>

const SCRIPT = 'resources/local-url-inherit-controller-worker.js';
const SCOPE = 'resources/local-url-inherit-controller-frame.html';

async function doAsyncTest(t, opts) {
  let name = `${opts.scheme}-${opts.child}-${opts.check}`;
  let scope = SCOPE + '?name=' + name;
  let reg = await service_worker_unregister_and_register(t, SCRIPT, scope);
  add_completion_callback(_ => reg.unregister());
  await wait_for_state(t, reg.installing, 'activated');

  let frame = await with_iframe(scope);
  add_completion_callback(_ => frame.remove());
  assert_not_equals(frame.contentWindow.navigator.serviceWorker.controller, null,
                    'frame should be controlled');

  let result = await frame.contentWindow.checkChildController(opts);
  result = result.data;

  let expect = 'unexpected';
  if (opts.check === 'controller') {
    expect = opts.expect === 'inherit'
               ? frame.contentWindow.navigator.serviceWorker.controller.scriptURL
               : null;
  } else if (opts.check === 'fetch') {
    // The service worker FetchEvent handler will provide an "intercepted"
    // body.  If the local URL ends up with an opaque origin and is not
    // intercepted then it will get an opaque Response.  In that case it
    // should see an empty string body.
    expect = opts.expect === 'intercept' ? 'intercepted' : '';
  }

  assert_equals(result, expect,
                `${opts.scheme} URL ${opts.child} should ${opts.expect} ${opts.check}`);
}

promise_test(function(t) {
  return doAsyncTest(t, {
    scheme: 'blob',
    child: 'iframe',
    check: 'controller',
    expect: 'inherit',
  });
}, 'Same-origin blob URL iframe should inherit service worker controller.');

promise_test(function(t) {
  return doAsyncTest(t, {
    scheme: 'blob',
    child: 'iframe',
    check: 'fetch',
    expect: 'intercept',
  });
}, 'Same-origin blob URL iframe should intercept fetch().');

promise_test(function(t) {
  return doAsyncTest(t, {
    scheme: 'blob',
    child: 'worker',
    check: 'controller',
    expect: 'inherit',
  });
}, 'Same-origin blob URL worker should inherit service worker controller.');

promise_test(function(t) {
  return doAsyncTest(t, {
    scheme: 'blob',
    child: 'worker',
    check: 'fetch',
    expect: 'intercept',
  });
}, 'Same-origin blob URL worker should intercept fetch().');

promise_test(function(t) {
  return doAsyncTest(t, {
    scheme: 'data',
    child: 'iframe',
    check: 'fetch',
    expect: 'not intercept',
  });
}, 'Data URL iframe should not intercept fetch().');

promise_test(function(t) {
  // Data URLs should result in an opaque origin and should probably not
  // have access to a cross-origin service worker.  See:
  //
  // https://github.com/w3c/ServiceWorker/issues/1262
  //
  return doAsyncTest(t, {
    scheme: 'data',
    child: 'worker',
    check: 'controller',
    expect: 'not inherit',
  });
}, 'Data URL worker should not inherit service worker controller.');

promise_test(function(t) {
  return doAsyncTest(t, {
    scheme: 'data',
    child: 'worker',
    check: 'fetch',
    expect: 'not intercept',
  });
}, 'Data URL worker should not intercept fetch().');

</script>
</body>