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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
<!DOCTYPE html>
<title>Service Worker: FetchEvent for resources</title>
<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>
<script>
let url_count = 0;
const expected_results = {};
function add_promise_to_test(url)
{
const expected = expected_results[url];
return new Promise((resolve) => {
expected.resolve = resolve;
});
}
function image_test(frame, url, cross_origin, expected_mode,
expected_credentials) {
const actual_url = url + (++url_count);
expected_results[actual_url] = {
cross_origin: cross_origin,
mode: expected_mode,
credentials: expected_credentials,
redirect: 'follow',
integrity: '',
destination: 'image',
message: `Image load (url:${actual_url} cross_origin:${cross_origin})`
};
frame.contentWindow.load_image(actual_url, cross_origin);
return add_promise_to_test(actual_url);
}
function script_test(frame, url, cross_origin, expected_mode,
expected_credentials) {
const actual_url = url + (++url_count);
expected_results[actual_url] = {
cross_origin: cross_origin,
mode: expected_mode,
credentials: expected_credentials,
redirect: 'follow',
integrity: '',
destination: 'script',
message: `Script load (url:${actual_url} cross_origin:${cross_origin})`
};
frame.contentWindow.load_script(actual_url, cross_origin);
return add_promise_to_test(actual_url);
}
function css_test(frame, url, cross_origin, expected_mode,
expected_credentials) {
const actual_url = url + (++url_count);
expected_results[actual_url] = {
cross_origin: cross_origin,
mode: expected_mode,
credentials: expected_credentials,
redirect: 'follow',
integrity: '',
destination: 'style',
message: `CSS load (url:${actual_url} cross_origin:${cross_origin})`
};
frame.contentWindow.load_css(actual_url, cross_origin);
return add_promise_to_test(actual_url);
}
function font_face_test(frame, url, expected_mode, expected_credentials) {
const actual_url = url + (++url_count);
expected_results[actual_url] = {
url: actual_url,
mode: expected_mode,
credentials: expected_credentials,
redirect: 'follow',
integrity: '',
destination: 'font',
message: `FontFace load (url: ${actual_url})`
};
frame.contentWindow.load_font(actual_url);
return add_promise_to_test(actual_url);
}
function script_integrity_test(frame, url, integrity, expected_integrity) {
const actual_url = url + (++url_count);
expected_results[actual_url] = {
url: actual_url,
mode: 'no-cors',
credentials: 'include',
redirect: 'follow',
integrity: expected_integrity,
destination: 'script',
message: `Script load (url:${actual_url})`
};
frame.contentWindow.load_script_with_integrity(actual_url, integrity);
return add_promise_to_test(actual_url);
}
function css_integrity_test(frame, url, integrity, expected_integrity) {
const actual_url = url + (++url_count);
expected_results[actual_url] = {
url: actual_url,
mode: 'no-cors',
credentials: 'include',
redirect: 'follow',
integrity: expected_integrity,
destination: 'style',
message: `CSS load (url:${actual_url})`
};
frame.contentWindow.load_css_with_integrity(actual_url, integrity);
return add_promise_to_test(actual_url);
}
function fetch_test(frame, url, mode, credentials,
expected_mode, expected_credentials) {
const actual_url = url + (++url_count);
expected_results[actual_url] = {
mode: expected_mode,
credentials: expected_credentials,
redirect: 'follow',
integrity: '',
destination: '',
message: `fetch (url:${actual_url} mode:${mode} ` +
`credentials:${credentials})`
};
frame.contentWindow.fetch(
new Request(actual_url, {mode: mode, credentials: credentials}));
return add_promise_to_test(actual_url);
}
function audio_test(frame, url, cross_origin,
expected_mode, expected_credentials) {
const actual_url = url + (++url_count);
expected_results[actual_url] = {
mode: expected_mode,
credentials: expected_credentials,
redirect: 'follow',
integrity: '',
destination: 'audio',
message: `Audio load (url:${actual_url} cross_origin:${cross_origin})`
};
frame.contentWindow.load_audio(actual_url, cross_origin);
return add_promise_to_test(actual_url);
}
function video_test(frame, url, cross_origin,
expected_mode, expected_credentials) {
const actual_url = url + (++url_count);
expected_results[actual_url] = {
mode: expected_mode,
credentials: expected_credentials,
redirect: 'follow',
integrity: '',
destination: 'video',
message: `Video load (url:${actual_url} cross_origin:${cross_origin})`
};
frame.contentWindow.load_video(actual_url, cross_origin);
return add_promise_to_test(actual_url);
}
promise_test(async t => {
const SCOPE = 'resources/fetch-request-resources-iframe.https.html';
const SCRIPT = 'resources/fetch-request-resources-worker.js';
const host_info = get_host_info();
const LOCAL_URL =
host_info['HTTPS_ORIGIN'] + base_path() + 'resources/sample?test';
const REMOTE_URL =
host_info['HTTPS_REMOTE_ORIGIN'] + base_path() + 'resources/sample?test';
const registration =
await service_worker_unregister_and_register(t, SCRIPT, SCOPE);
t.add_cleanup(() => registration.unregister());
const worker = registration.installing;
await wait_for_state(t, worker, 'activated');
await new Promise((resolve, reject) => {
const channel = new MessageChannel();
channel.port1.onmessage = t.step_func(msg => {
if (msg.data.ready) {
resolve();
return;
}
const result = msg.data;
const expected = expected_results[result.url];
if (!expected) {
return;
}
test(() => {
assert_equals(
result.mode, expected.mode,
`mode of must be ${expected.mode}.`);
assert_equals(
result.credentials, expected.credentials,
`credentials of ${expected.message} must be ` +
`${expected.credentials}.`);
assert_equals(
result.redirect, expected.redirect,
`redirect mode of ${expected.message} must be ` +
`${expected.redirect}.`);
assert_equals(
result.integrity, expected.integrity,
`integrity of ${expected.message} must be ` +
`${expected.integrity}.`);
assert_equals(
result.destination, expected.destination,
`destination of ${expected.message} must be ` +
`${expected.destination}.`);
}, expected.message);
expected.resolve();
delete expected_results[result.url];
});
worker.postMessage({port: channel.port2}, [channel.port2]);
});
const f = await with_iframe(SCOPE);
t.add_cleanup(() => f.remove());
await image_test(f, LOCAL_URL, '', 'no-cors', 'include');
await image_test(f, REMOTE_URL, '', 'no-cors', 'include');
await css_test(f, LOCAL_URL, '', 'no-cors', 'include');
await css_test(f, REMOTE_URL, '', 'no-cors', 'include');
await image_test(f, LOCAL_URL, 'anonymous', 'cors', 'same-origin');
await image_test(f, LOCAL_URL, 'use-credentials', 'cors', 'include');
await image_test(f, REMOTE_URL, '', 'no-cors', 'include');
await image_test(f, REMOTE_URL, 'anonymous', 'cors', 'same-origin');
await image_test(f, REMOTE_URL, 'use-credentials', 'cors', 'include');
await script_test(f, LOCAL_URL, '', 'no-cors', 'include');
await script_test(f, LOCAL_URL, 'anonymous', 'cors', 'same-origin');
await script_test(f, LOCAL_URL, 'use-credentials', 'cors', 'include');
await script_test(f, REMOTE_URL, '', 'no-cors', 'include');
await script_test(f, REMOTE_URL, 'anonymous', 'cors', 'same-origin');
await script_test(f, REMOTE_URL, 'use-credentials', 'cors', 'include');
await css_test(f, LOCAL_URL, '', 'no-cors', 'include');
await css_test(f, LOCAL_URL, 'anonymous', 'cors', 'same-origin');
await css_test(f, LOCAL_URL, 'use-credentials', 'cors', 'include');
await css_test(f, REMOTE_URL, '', 'no-cors', 'include');
await css_test(f, REMOTE_URL, 'anonymous', 'cors', 'same-origin');
await css_test(f, REMOTE_URL, 'use-credentials', 'cors', 'include');
await font_face_test(f, LOCAL_URL, 'cors', 'same-origin');
await font_face_test(f, REMOTE_URL, 'cors', 'same-origin');
await script_integrity_test(f, LOCAL_URL, ' ', ' ');
await script_integrity_test(
f, LOCAL_URL,
'This is not a valid integrity because it has no dashes',
'This is not a valid integrity because it has no dashes');
await script_integrity_test(f, LOCAL_URL, 'sha256-', 'sha256-');
await script_integrity_test(f, LOCAL_URL, 'sha256-foo?123', 'sha256-foo?123');
await script_integrity_test(f, LOCAL_URL, 'sha256-foo sha384-abc ',
'sha256-foo sha384-abc ');
await script_integrity_test(f, LOCAL_URL, 'sha256-foo sha256-abc',
'sha256-foo sha256-abc');
await css_integrity_test(f, LOCAL_URL, ' ', ' ');
await css_integrity_test(
f, LOCAL_URL,
'This is not a valid integrity because it has no dashes',
'This is not a valid integrity because it has no dashes');
await css_integrity_test(f, LOCAL_URL, 'sha256-', 'sha256-');
await css_integrity_test(f, LOCAL_URL, 'sha256-foo?123', 'sha256-foo?123');
await css_integrity_test(f, LOCAL_URL, 'sha256-foo sha384-abc ',
'sha256-foo sha384-abc ');
await css_integrity_test(f, LOCAL_URL, 'sha256-foo sha256-abc',
'sha256-foo sha256-abc');
await fetch_test(f, LOCAL_URL, 'same-origin', 'omit', 'same-origin', 'omit');
await fetch_test(f, LOCAL_URL, 'same-origin', 'same-origin',
'same-origin', 'same-origin');
await fetch_test(f, LOCAL_URL, 'same-origin', 'include',
'same-origin', 'include');
await fetch_test(f, LOCAL_URL, 'no-cors', 'omit', 'no-cors', 'omit');
await fetch_test(f, LOCAL_URL, 'no-cors', 'same-origin',
'no-cors', 'same-origin');
await fetch_test(f, LOCAL_URL, 'no-cors', 'include', 'no-cors', 'include');
await fetch_test(f, LOCAL_URL, 'cors', 'omit', 'cors', 'omit');
await fetch_test(f, LOCAL_URL, 'cors', 'same-origin', 'cors', 'same-origin');
await fetch_test(f, LOCAL_URL, 'cors', 'include', 'cors', 'include');
await fetch_test(f, REMOTE_URL, 'no-cors', 'omit', 'no-cors', 'omit');
await fetch_test(f, REMOTE_URL, 'no-cors', 'same-origin', 'no-cors', 'same-origin');
await fetch_test(f, REMOTE_URL, 'no-cors', 'include', 'no-cors', 'include');
await fetch_test(f, REMOTE_URL, 'cors', 'omit', 'cors', 'omit');
await fetch_test(f, REMOTE_URL, 'cors', 'same-origin', 'cors', 'same-origin');
await fetch_test(f, REMOTE_URL, 'cors', 'include', 'cors', 'include');
await audio_test(f, LOCAL_URL, '', 'no-cors', 'include');
await audio_test(f, LOCAL_URL, 'anonymous', 'cors', 'same-origin');
await audio_test(f, LOCAL_URL, 'use-credentials', 'cors', 'include');
await audio_test(f, REMOTE_URL, '', 'no-cors', 'include');
await audio_test(f, REMOTE_URL, 'anonymous', 'cors', 'same-origin');
await audio_test(f, REMOTE_URL, 'use-credentials', 'cors', 'include');
await video_test(f, LOCAL_URL, '', 'no-cors', 'include');
await video_test(f, LOCAL_URL, 'anonymous', 'cors', 'same-origin');
await video_test(f, LOCAL_URL, 'use-credentials', 'cors', 'include');
await video_test(f, REMOTE_URL, '', 'no-cors', 'include');
await video_test(f, REMOTE_URL, 'anonymous', 'cors', 'same-origin');
await video_test(f, REMOTE_URL, 'use-credentials', 'cors', 'include');
}, 'Verify FetchEvent for resources.');
</script>
|