summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/tentative/static-router/static-router-request-destination.https.html
blob: 55ad6da31aad1058d9707bd63867142a80a9743c (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
<!DOCTYPE html>
<meta charset="utf-8">
<title>
  Static Router: routers are evaluated with the request desitnation condition.
</title>
<script src="/common/get-host-info.sub.js"></script>
<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="resources/static-router-helpers.sub.js">
</script>
<body>
<script>
const ROUTER_KEY_DESTINATION_SCRIPT = 'condition-request-destination-script-network';
const FRAME_SRC = 'resources/direct.html';
const SCRIPT_SRC = 'direct.js';
const STYLE_SRC = 'direct.txt';

const appendScript = async (iwin, src) => {
  const promise = new Promise(resolve => {
    const script = iwin.document.createElement('script');
    script.src = src;
    iwin.document.body.appendChild(script);
    script.onload = () => {
      resolve(script);
    };
  });

  return promise;
};

const appendCSS = async (iwin, src) => {
  const promise = new Promise(resolve => {
    const link = iwin.document.createElement('link');
    link.rel = 'stylesheet';
    link.href = src;
    iwin.document.head.appendChild(link);
    link.onload = () => {
      resolve(link);
    };
  });

  return promise;
};

promise_test(async t => {
  const worker = await registerAndActivate(t, ROUTER_KEY_DESTINATION_SCRIPT);
  const iframe = await createIframe(t, FRAME_SRC);

  let info = await get_info_from_worker(worker);
  assert_equals(info.requests.length, 1);
  assert_equals(info.requests[0].destination, 'iframe');

  // JavaScript is not handled by fetch handler.
  await appendScript(iframe.contentWindow, SCRIPT_SRC);
  assert_equals(iframe.contentWindow.router_condition_request_destination_script, true);
  info = await get_info_from_worker(worker);
  assert_equals(info.requests.length, 1);
}, 'Subreosurce load matched with the requestMethod script condition');

promise_test(async t => {
  const worker = await registerAndActivate(t, ROUTER_KEY_DESTINATION_SCRIPT);
  const iframe = await createIframe(t, FRAME_SRC);

  let info = await get_info_from_worker(worker);
  assert_equals(info.requests.length, 1);
  assert_equals(info.requests[0].destination, 'iframe');

  // Stylesheet is not handled by fetch handler.
  await appendCSS(iframe.contentWindow, STYLE_SRC);
  info = await get_info_from_worker(worker);
  assert_equals(info.requests.length, 2);
  assert_equals(info.requests[1].destination, 'style');
}, 'Subreosurce load not matched with the requestMethod script condition');
</script>
</body>