Skip to content

Commit 5a8ad43

Browse files
committed
[fetch] Replace metadata tests for Service Workers
1 parent 21b2348 commit 5a8ad43

4 files changed

Lines changed: 260 additions & 36 deletions

File tree

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<!DOCTYPE html>
2+
<!--
3+
This test was procedurally generated. Please do not modify it directly.
4+
Sources:
5+
- fetch/metadata/tools/fetch-metadata.conf.yml
6+
- fetch/metadata/tools/templates/serviceworker.https.sub.html
7+
-->
8+
<!DOCTYPE html>
9+
<html lang="en">
10+
<meta charset="utf-8">
11+
<title>HTTP headers on request for Service Workers</title>
12+
<script src="/resources/testharness.js"></script>
13+
<script src="/resources/testharnessreport.js"></script>
14+
<script src="/fetch/metadata/resources/helper.sub.js"></script>
15+
<body>
16+
<script>
17+
'use strict';
18+
19+
function induceRequest(t, url, options, event, clear) {
20+
// Register a service worker and check the request header.
21+
return navigator.serviceWorker.register(url, options)
22+
.then((registration) => {
23+
t.add_cleanup(() => registration.unregister());
24+
if (event === 'register') {
25+
return;
26+
}
27+
return clear().then(() => registration.update());
28+
});
29+
}
30+
31+
promise_test((t) => {
32+
const key = '{{uuid()}}';
33+
const url = makeRequestURL(
34+
key, ['httpsOrigin'], { mime: 'application/javascript' }
35+
);
36+
37+
return induceRequest(t, url, {}, 'register')
38+
.then(() => retrieve(key))
39+
.then((headers) => {
40+
assert_own_property(headers, 'sec-fetch-site');
41+
assert_equals(headers['sec-fetch-site'], 'same-origin');
42+
});
43+
}, 'sec-fetch-site - Same origin, no options - registration');
44+
45+
promise_test((t) => {
46+
const key = '{{uuid()}}';
47+
const url = makeRequestURL(
48+
key, ['httpsOrigin'], { mime: 'application/javascript' }
49+
);
50+
51+
return induceRequest(t, url, {}, 'update', () => retrieve(key))
52+
.then(() => retrieve(key))
53+
.then((headers) => {
54+
assert_own_property(headers, 'sec-fetch-site');
55+
assert_equals(headers['sec-fetch-site'], 'same-origin');
56+
});
57+
}, 'sec-fetch-site - Same origin, no options - updating');
58+
59+
promise_test((t) => {
60+
const key = '{{uuid()}}';
61+
const url = makeRequestURL(
62+
key, [], { mime: 'application/javascript' }
63+
);
64+
65+
return induceRequest(t, url, {"type": "classic"}, 'register')
66+
.then(() => retrieve(key))
67+
.then((headers) => {
68+
assert_own_property(headers, 'sec-fetch-mode');
69+
assert_equals(headers['sec-fetch-mode'], 'same-origin');
70+
});
71+
}, 'sec-fetch-mode - options: type=classic - registration');
72+
73+
promise_test((t) => {
74+
const key = '{{uuid()}}';
75+
const url = makeRequestURL(
76+
key, [], { mime: 'application/javascript' }
77+
);
78+
79+
return induceRequest(t, url, {"type": "classic"}, 'update', () => retrieve(key))
80+
.then(() => retrieve(key))
81+
.then((headers) => {
82+
assert_own_property(headers, 'sec-fetch-mode');
83+
assert_equals(headers['sec-fetch-mode'], 'same-origin');
84+
});
85+
}, 'sec-fetch-mode - options: type=classic - updating');
86+
87+
promise_test((t) => {
88+
const key = '{{uuid()}}';
89+
const url = makeRequestURL(
90+
key, [], { mime: 'application/javascript' }
91+
);
92+
93+
return induceRequest(t, url, {}, 'register')
94+
.then(() => retrieve(key))
95+
.then((headers) => {
96+
assert_own_property(headers, 'sec-fetch-mode');
97+
assert_equals(headers['sec-fetch-mode'], 'same-origin');
98+
});
99+
}, 'sec-fetch-mode - no options - registration');
100+
101+
promise_test((t) => {
102+
const key = '{{uuid()}}';
103+
const url = makeRequestURL(
104+
key, [], { mime: 'application/javascript' }
105+
);
106+
107+
return induceRequest(t, url, {}, 'update', () => retrieve(key))
108+
.then(() => retrieve(key))
109+
.then((headers) => {
110+
assert_own_property(headers, 'sec-fetch-mode');
111+
assert_equals(headers['sec-fetch-mode'], 'same-origin');
112+
});
113+
}, 'sec-fetch-mode - no options - updating');
114+
115+
promise_test((t) => {
116+
const key = '{{uuid()}}';
117+
const url = makeRequestURL(
118+
key, [], { mime: 'application/javascript' }
119+
);
120+
121+
return induceRequest(t, url, {}, 'register')
122+
.then(() => retrieve(key))
123+
.then((headers) => {
124+
assert_own_property(headers, 'sec-fetch-dest');
125+
assert_equals(headers['sec-fetch-dest'], 'serviceworker');
126+
});
127+
}, 'sec-fetch-dest - no options - registration');
128+
129+
promise_test((t) => {
130+
const key = '{{uuid()}}';
131+
const url = makeRequestURL(
132+
key, [], { mime: 'application/javascript' }
133+
);
134+
135+
return induceRequest(t, url, {}, 'update', () => retrieve(key))
136+
.then(() => retrieve(key))
137+
.then((headers) => {
138+
assert_own_property(headers, 'sec-fetch-dest');
139+
assert_equals(headers['sec-fetch-dest'], 'serviceworker');
140+
});
141+
}, 'sec-fetch-dest - no options - updating');
142+
143+
promise_test((t) => {
144+
const key = '{{uuid()}}';
145+
const url = makeRequestURL(
146+
key, [], { mime: 'application/javascript' }
147+
);
148+
149+
return induceRequest(t, url, {}, 'register')
150+
.then(() => retrieve(key))
151+
.then((headers) => {
152+
assert_not_own_property(headers, 'sec-fetch-user');
153+
});
154+
}, 'sec-fetch-user - no options - registration');
155+
156+
promise_test((t) => {
157+
const key = '{{uuid()}}';
158+
const url = makeRequestURL(
159+
key, [], { mime: 'application/javascript' }
160+
);
161+
162+
return induceRequest(t, url, {}, 'update', () => retrieve(key))
163+
.then(() => retrieve(key))
164+
.then((headers) => {
165+
assert_not_own_property(headers, 'sec-fetch-user');
166+
});
167+
}, 'sec-fetch-user - no options - updating');
168+
</script>
169+
</body>
170+
</html>

fetch/metadata/serviceworker.https.sub.html

Lines changed: 0 additions & 36 deletions
This file was deleted.

fetch/metadata/tools/fetch-metadata.conf.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ cases:
5151
# Service workers are only available in secure context
5252
fetch-via-serviceworker.https.sub.html: []
5353
cors-preflight.sub.html: []
54+
# Service workers are only available in secure context
55+
serviceworker.https.sub.html: []
5456

5557
css-images.sub.html:
5658
- filename_flags: [tentative]
@@ -155,6 +157,7 @@ cases:
155157
window-location.sub.html: [{}]
156158
script-module-import-dynamic.sub.html: [{}]
157159
script-module-import-static.sub.html: [{}]
160+
serviceworker.https.sub.html: [{}]
158161
svg-image.sub.html: [{}]
159162
window-history.sub.html: [{}]
160163
worker-dedicated-importscripts.sub.html: [{}]
@@ -184,6 +187,8 @@ cases:
184187
cors-preflight.sub.html: []
185188
# Service workers are only available in secure context
186189
fetch-via-serviceworker.https.sub.html: []
190+
# Service workers' redirect mode is "error"
191+
serviceworker.https.sub.html: []
187192
# Interstitial locations in an HTTP redirect chain are not added to the
188193
# session history, so these requests cannot be initiated using the
189194
# History API.
@@ -280,6 +285,8 @@ cases:
280285
# preflight requests are only issued on the basis of the origin of the
281286
# initial request.
282287
cors-preflight.sub.html: []
288+
# Service Workers' redirect mode is "error"
289+
serviceworker.https.sub.html: []
283290
# Interstitial locations in an HTTP redirect chain are not added to the
284291
# session history, so these requests cannot be initiated using the
285292
# History API.
@@ -372,6 +379,8 @@ cases:
372379
header-link.sub.html: []
373380
script-module-import-static.sub.html: []
374381
script-module-import-dynamic.sub.html: []
382+
# Service Workers' redirect mode is "error"
383+
serviceworker.https.sub.html: []
375384
cors-preflight.sub.html: []
376385
# Interstitial locations in an HTTP redirect chain are not added to the
377386
# session history, so these requests cannot be initiated using the
@@ -573,6 +582,11 @@ cases:
573582
elementAttrs: { crossorigin: anonymous }
574583
- expected: cors
575584
elementAttrs: { crossorigin: use-credentials }
585+
serviceworker.https.sub.html:
586+
- expected: same-origin
587+
options: { type: 'classic' }
588+
# https://github.com/whatwg/html/pull/5875
589+
- expected: same-origin
576590
worker-dedicated-constructor.sub.html:
577591
- expected: same-origin
578592
- options: { type: module }
@@ -691,6 +705,8 @@ cases:
691705
- expected: script
692706
script-module-import-static.sub.html:
693707
- expected: script
708+
serviceworker.https.sub.html:
709+
- expected: serviceworker
694710
# Implemented as "image" in Chromium and Firefox, but specified as
695711
# "empty"
696712
# https://github.com/w3c/svgwg/issues/782
@@ -796,6 +812,8 @@ cases:
796812
- expected: NULL
797813
script-module-import-static.sub.html:
798814
- expected: NULL
815+
serviceworker.https.sub.html:
816+
- expected: NULL
799817
svg-image.sub.html:
800818
- expected: NULL
801819
worker-dedicated-constructor.sub.html:
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<!DOCTYPE html>
2+
<!--
3+
[%provenance%]
4+
-->
5+
<!DOCTYPE html>
6+
<html lang="en">
7+
<meta charset="utf-8">
8+
<title>HTTP headers on request for Service Workers</title>
9+
<script src="/resources/testharness.js"></script>
10+
<script src="/resources/testharnessreport.js"></script>
11+
<script src="/fetch/metadata/resources/helper.sub.js"></script>
12+
<body>
13+
<script>
14+
'use strict';
15+
16+
function induceRequest(t, url, options, event, clear) {
17+
// Register a service worker and check the request header.
18+
return navigator.serviceWorker.register(url, options)
19+
.then((registration) => {
20+
t.add_cleanup(() => registration.unregister());
21+
if (event === 'register') {
22+
return;
23+
}
24+
return clear().then(() => registration.update());
25+
});
26+
}
27+
28+
{%- for subtest in subtests %}
29+
{%- set origin = subtest.origins[0]|default('httpsOrigin') %}
30+
{%- if origin == 'httpsOrigin' or not origin %}
31+
32+
promise_test((t) => {
33+
const key = '{{uuid()}}';
34+
const url = makeRequestURL(
35+
key, [% subtest.origins %], { mime: 'application/javascript' }
36+
);
37+
38+
return induceRequest(t, url, [%subtest.options | default({}) | tojson%], 'register')
39+
.then(() => retrieve(key))
40+
.then((headers) => {
41+
{%- if subtest.expected == none %}
42+
assert_not_own_property(headers, '[%subtest.headerName%]');
43+
{%- else %}
44+
assert_own_property(headers, '[%subtest.headerName%]');
45+
assert_equals(headers['[%subtest.headerName%]'], '[%subtest.expected%]');
46+
{%- endif %}
47+
});
48+
}, '[%subtest.headerName%] - [%subtest.description | pad("end", ", ")%][%subtest.options | collection("options")%] - registration');
49+
50+
promise_test((t) => {
51+
const key = '{{uuid()}}';
52+
const url = makeRequestURL(
53+
key, [% subtest.origins %], { mime: 'application/javascript' }
54+
);
55+
56+
return induceRequest(t, url, [%subtest.options | default({}) | tojson%], 'update', () => retrieve(key))
57+
.then(() => retrieve(key))
58+
.then((headers) => {
59+
{%- if subtest.expected == none %}
60+
assert_not_own_property(headers, '[%subtest.headerName%]');
61+
{%- else %}
62+
assert_own_property(headers, '[%subtest.headerName%]');
63+
assert_equals(headers['[%subtest.headerName%]'], '[%subtest.expected%]');
64+
{%- endif %}
65+
});
66+
}, '[%subtest.headerName%] - [%subtest.description | pad("end", ", ")%][%subtest.options | collection("options")%] - updating');
67+
68+
{%- endif %}
69+
{%- endfor %}
70+
</script>
71+
</body>
72+
</html>

0 commit comments

Comments
 (0)