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
|
<!DOCTYPE html>
<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>
var worker = 'resources/fetch-event-test-worker.js';
function do_test(referrer, value, expected, name)
{
test(() => {
assert_equals(value, expected);
}, name + (referrer ? " - Custom Referrer" : " - Default Referrer"));
}
function run_referrer_policy_tests(frame, referrer, href, origin) {
return frame.contentWindow.fetch('resources/simple.html?referrerFull',
{method: "GET", referrer: referrer})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: ' + href + '\n' +
'ReferrerPolicy: strict-origin-when-cross-origin',
'Service Worker should respond to fetch with the referrer URL when a member of RequestInit is present');
var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
'/resources/simple.html?referrerFull';
return frame.contentWindow.fetch(http_url,
{method: "GET", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: \n' +
'ReferrerPolicy: strict-origin-when-cross-origin',
'Service Worker should respond to fetch with no referrer when a member of RequestInit is present with an HTTP request');
return frame.contentWindow.fetch('resources/simple.html?referrerFull',
{referrerPolicy: "", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: ' + href + '\n' +
'ReferrerPolicy: strict-origin-when-cross-origin',
'Service Worker should respond to fetch with the referrer with ""');
var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
'/resources/simple.html?referrerFull';
return frame.contentWindow.fetch(http_url,
{referrerPolicy: "", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: \n' +
'ReferrerPolicy: strict-origin-when-cross-origin',
'Service Worker should respond to fetch with no referrer with ""');
return frame.contentWindow.fetch('resources/simple.html?referrerFull',
{referrerPolicy: "origin", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: ' + origin + '/' + '\n' +
'ReferrerPolicy: origin',
'Service Worker should respond to fetch with the referrer origin with "origin" and a same origin request');
var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
'/resources/simple.html?referrerFull';
return frame.contentWindow.fetch(http_url,
{referrerPolicy: "origin", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: ' + origin + '/' + '\n' +
'ReferrerPolicy: origin',
'Service Worker should respond to fetch with the referrer origin with "origin" and a cross origin request');
return frame.contentWindow.fetch('resources/simple.html?referrerFull',
{referrerPolicy: "origin-when-cross-origin", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: ' + href + '\n' +
'ReferrerPolicy: origin-when-cross-origin',
'Service Worker should respond to fetch with the referrer URL with "origin-when-cross-origin" and a same origin request');
var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
'/resources/simple.html?referrerFull';
return frame.contentWindow.fetch(http_url,
{referrerPolicy: "origin-when-cross-origin", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: ' + origin + '/' + '\n' +
'ReferrerPolicy: origin-when-cross-origin',
'Service Worker should respond to fetch with the referrer origin with "origin-when-cross-origin" and a cross origin request');
return frame.contentWindow.fetch('resources/simple.html?referrerFull',
{referrerPolicy: "no-referrer-when-downgrade", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: ' + href + '\n' +
'ReferrerPolicy: no-referrer-when-downgrade',
'Service Worker should respond to fetch with no referrer with "no-referrer-when-downgrade" and a same origin request');
var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
'/resources/simple.html?referrerFull';
return frame.contentWindow.fetch(http_url,
{referrerPolicy: "no-referrer-when-downgrade", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: \n' +
'ReferrerPolicy: no-referrer-when-downgrade',
'Service Worker should respond to fetch with no referrer with "no-referrer-when-downgrade" and an HTTP request');
var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
'/resources/simple.html?referrerFull';
return frame.contentWindow.fetch(http_url, {referrerPolicy: "unsafe-url", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: ' + href + '\n' +
'ReferrerPolicy: unsafe-url',
'Service Worker should respond to fetch with no referrer with "unsafe-url"');
return frame.contentWindow.fetch('resources/simple.html?referrerFull',
{referrerPolicy: "no-referrer", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: \n' +
'ReferrerPolicy: no-referrer',
'Service Worker should respond to fetch with no referrer URL with "no-referrer"');
return frame.contentWindow.fetch('resources/simple.html?referrerFull',
{referrerPolicy: "same-origin", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: ' + href + '\n' +
'ReferrerPolicy: same-origin',
'Service Worker should respond to fetch with referrer URL with "same-origin" and a same origin request');
var http_url = get_host_info()['HTTPS_REMOTE_ORIGIN'] + base_path() +
'/resources/simple.html?referrerFull';
return frame.contentWindow.fetch(http_url,
{referrerPolicy: "same-origin", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: \n' +
'ReferrerPolicy: same-origin',
'Service Worker should respond to fetch with no referrer with "same-origin" and cross origin request');
var http_url = get_host_info()['HTTPS_REMOTE_ORIGIN'] + base_path() +
'/resources/simple.html?referrerFull';
return frame.contentWindow.fetch(http_url,
{referrerPolicy: "strict-origin", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: ' + origin + '/' + '\n' +
'ReferrerPolicy: strict-origin',
'Service Worker should respond to fetch with the referrer origin with "strict-origin" and a HTTPS cross origin request');
return frame.contentWindow.fetch('resources/simple.html?referrerFull',
{referrerPolicy: "strict-origin", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: ' + origin + '/' + '\n' +
'ReferrerPolicy: strict-origin',
'Service Worker should respond to fetch with the referrer origin with "strict-origin" and a same origin request');
var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
'/resources/simple.html?referrerFull';
return frame.contentWindow.fetch(http_url,
{referrerPolicy: "strict-origin", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: \n' +
'ReferrerPolicy: strict-origin',
'Service Worker should respond to fetch with no referrer with "strict-origin" and a HTTP request');
return frame.contentWindow.fetch('resources/simple.html?referrerFull',
{referrerPolicy: "strict-origin-when-cross-origin", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: ' + href + '\n' +
'ReferrerPolicy: strict-origin-when-cross-origin',
'Service Worker should respond to fetch with the referrer URL with "strict-origin-when-cross-origin" and a same origin request');
var http_url = get_host_info()['HTTPS_REMOTE_ORIGIN'] + base_path() +
'/resources/simple.html?referrerFull';
return frame.contentWindow.fetch(http_url,
{referrerPolicy: "strict-origin-when-cross-origin", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: ' + origin + '/' + '\n' +
'ReferrerPolicy: strict-origin-when-cross-origin',
'Service Worker should respond to fetch with the referrer origin with "strict-origin-when-cross-origin" and a HTTPS cross origin request');
var http_url = get_host_info()['HTTP_ORIGIN'] + base_path() +
'/resources/simple.html?referrerFull';
return frame.contentWindow.fetch(http_url,
{referrerPolicy: "strict-origin-when-cross-origin", referrer: referrer});
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
do_test(referrer,
response_text,
'Referrer: \n' +
'ReferrerPolicy: strict-origin-when-cross-origin',
'Service Worker should respond to fetch with no referrer with "strict-origin-when-cross-origin" and a HTTP request');
});
}
promise_test(function(t) {
var scope = 'resources/simple.html?referrerPolicy';
var frame;
return service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
t.add_cleanup(function() {
return service_worker_unregister(t, scope);
});
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(f) {
frame = f;
test(() => {
assert_equals(frame.contentDocument.body.textContent, 'ReferrerPolicy: strict-origin-when-cross-origin');
}, 'Service Worker should respond to fetch with the default referrer policy');
// First, run the referrer policy tests without passing a referrer in RequestInit.
return run_referrer_policy_tests(frame, undefined, frame.contentDocument.location.href,
frame.contentDocument.location.origin);
})
.then(function() {
// Now, run the referrer policy tests while passing a referrer in RequestInit.
var referrer = get_host_info()['HTTPS_ORIGIN'] + base_path() + 'resources/fake-referrer';
return run_referrer_policy_tests(frame, 'fake-referrer', referrer,
frame.contentDocument.location.origin);
})
.then(function() {
frame.remove();
});
}, 'Service Worker responds to fetch event with the referrer policy');
</script>
</body>
|