summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/tentative/static-router/static-router-subresource.https.html
blob: 3f7902a8723367c4bb9174c1dec164577ba56365 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE html>
<meta charset="utf-8">
<title>Static Router: simply skip fetch handler if pattern matches</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="resources/static-router-helpers.sub.js"></script>
<body>
<script>
const SCRIPT = 'resources/static-router-sw.js';
const ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED =
  'condition-urlpattern-constructed-source-network';
const ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED_IGNORE_CASE =
  'condition-urlpattern-constructed-ignore-case-source-network';
const ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED_RESPECT_CASE =
  'condition-urlpattern-constructed-respect-case-source-network';
const ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT =
  'condition-urlpattern-urlpatterninit-source-network';
const ROUTER_RULE_KEY_URL_PATTERN_STRING =
  'condition-urlpattern-string-source-network';
const ROUTER_RULE_KEY_REQUEST = 'condition-request-source-network'
const ROUTER_RULE_KEY_URL_PATTERN_STRING_CACHE =
  'condition-urlpattern-string-source-cache';
const ROUTER_RULE_KEY_OR = 'condition-or-source-network'
const SCOPE = 'resources/';
const HTML_FILE = 'resources/simple.html';
const TXT_FILE = 'resources/direct.txt';
const CSV_FILE = 'resources/simple.csv';
// Warning: please prepare the corresponding `*.text.headers` files, otherwise
// iframeTest() fails to load the following files due to MIME mismatches.
const OR_TEST_FILES = [
  'resources/or-test/direct1.text',
  'resources/or-test/direct2.text',
  'resources/or-test/does-not-exist.text',
];

iframeTest(HTML_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, async (t, iwin) => {
  const rnd = randomString();
  const response = await iwin.fetch('?nonce=' + rnd);
  assert_equals(await response.text(), rnd);
}, 'Subresource load not matched with URLPattern condition');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, async (t, iwin) => {
  const rnd = randomString();
  const response = await iwin.fetch('?nonce=' + rnd);
  assert_equals(await response.text(), "Network\n");
}, 'Subresource load matched with URLPattern condition');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED, async (t, iwin, worker) => {
  const rnd = randomString();
  // Confirm that the given URLPatternInit has a wildcard pattern for the
  // hostname. Also, if |urlPattern| is a consutructed URLPattern object,
  // baseURL won't be set while adding router rules, thus it matches the cross
  // origin request as far as other components matches. So expecting the direct
  // network request and the fetch handler doesn't capture the response.
  // The response is going to be a opaque.
  const origin = get_host_info().HTTPS_REMOTE_ORIGIN;
  const response = await iwin.fetch(
    `${origin}/${TXT_FILE}?nonce=${rnd}`, {mode: 'no-cors'});
  const {requests} = await get_info_from_worker(worker);
  assert_equals(requests.length, 0);
  assert_equals(response.type, 'opaque');
}, 'Subresource cross origin load matched with URLPattern condition via constructed object');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED_IGNORE_CASE, async (t, iwin) => {
  const rnd = randomString();
  const response = await iwin.fetch('?nonce=' + rnd);
  assert_equals(await response.text(), "Network\n");
}, 'Subresource load matched with ignoreCase URLPattern condition');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_CONSTRUCTED_RESPECT_CASE, async (t, iwin) => {
  const rnd = randomString();
  const response = await iwin.fetch('?nonce=' + rnd);
  assert_equals(await response.text(), rnd);
}, 'Subresource load matched without ignoreCase URLPattern condition');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT, async (t, iwin) => {
  const rnd = randomString();
  const response = await iwin.fetch('?nonce=' + rnd);
  assert_equals(await response.text(), "Network\n");
}, 'Subresource load matched with URLPattern condition via URLPatternInit');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_URLPATTERNINIT, async (t, iwin, worker) => {
  // The SW script URL is added as a baseURL when |urlPattern| is passed via
  // URLPatternInit, and there is not |baseURL| in it. Cross origin request will
  // go through the fetch handler because |baseURL| info complements hostname
  // with the hostname of the SW script.
  const rnd = randomString();
  const origin = get_host_info().HTTPS_REMOTE_ORIGIN;
  const response = await iwin.fetch(`${origin}/${TXT_FILE}?nonce=${rnd}`);
  const {requests} = await get_info_from_worker(worker);
  assert_equals(requests.length, 1);
  assert_equals(await response.text(), rnd);
}, 'Subresource cross origin load not matched with URLPattern condition via URLPatternInit');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_STRING, async (t, iwin) => {
  const rnd = randomString();
  const response = await iwin.fetch('?nonce=' + rnd);
  assert_equals(await response.text(), "Network\n");
}, 'Subresource load matched with URLPattern condition via string');

iframeTest(TXT_FILE, ROUTER_RULE_KEY_URL_PATTERN_STRING, async (t, iwin, worker) => {
  // The SW script URL is added as a baseURL when |urlPattern| is passed via
  // string, and there is not |baseURL| in it. Cross origin request will go
  // through the fetch handler because |baseURL| info complements hostname with
  // the hostname of the SW script.
  const rnd = randomString();
  const origin = get_host_info().HTTPS_REMOTE_ORIGIN;
  const response = await iwin.fetch(`${origin}/${TXT_FILE}?nonce=${rnd}`);
  const {requests} = await get_info_from_worker(worker);
  assert_equals(requests.length, 1);
  assert_equals(await response.text(), rnd);
}, 'Subresource cross origin load not matched with URLPattern condition via string');

iframeTest(CSV_FILE, ROUTER_RULE_KEY_REQUEST, async (t, iwin) => {
  const rnd = randomString();
  const response = await iwin.fetch('?nonce=' + rnd, { mode: 'no-cors' });
  assert_equals(await response.text(), "matched,with,non-url,conditions\n");
}, 'Subresource load matched with RequestMode condition');

iframeTest(OR_TEST_FILES[0], ROUTER_RULE_KEY_OR, async (t, iwin) => {
  const rnd = randomString();
  const response = await iwin.fetch('?nonce=' + rnd);
  assert_equals(await response.text(), "Network\n");
}, 'Subresource load matched with the nested `or` condition');

iframeTest(OR_TEST_FILES[1], ROUTER_RULE_KEY_OR, async (t, iwin) => {
  const rnd = randomString();
  const response = await iwin.fetch('?nonce=' + rnd);
  assert_equals(await response.text(), "Network\n");
}, 'Subresource load matched with the next `or` condition');

iframeTest(OR_TEST_FILES[2], ROUTER_RULE_KEY_OR, async (t, iwin) => {
  const rnd = randomString();
  const response = await iwin.fetch('?nonce=' + rnd);
  assert_equals(await response.text(), rnd);
}, 'Subresource load not matched with `or` condition');

iframeTest(HTML_FILE, ROUTER_RULE_KEY_URL_PATTERN_STRING_CACHE, async (t, iwin) => {
  // No need to set `resources/` because the request is dispatched from iframe.
  const CACHED_FILE = 'cache.txt';
  const response = await iwin.fetch(CACHED_FILE);
  assert_equals(response.status, 200);
  assert_equals(await response.text(), "From cache");

  // This doesn't match because the cache key is wrong.
  const rnd = randomString();
  const response_with_param = await iwin.fetch(`${CACHED_FILE}?nonce=${rnd}`);
  assert_equals(response_with_param.status, 404);
}, 'Subresource load matched with the cache source rule');

</script>
</body>