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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
|
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/get-host-info.sub.js"></script>
<script src="resources/test-helpers.sub.js"></script>
<body>
<script>
var worker = 'resources/fetch-event-test-worker.js';
async_test(function(t) {
var scope = 'resources/simple.html?string';
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(frame) {
assert_equals(
frame.contentDocument.body.textContent,
'Test string',
'Service Worker should respond to fetch with a test string');
assert_equals(
frame.contentDocument.contentType,
'text/plain',
'The content type of the response created with a string should be text/plain');
assert_equals(
frame.contentDocument.characterSet,
'UTF-8',
'The character set of the response created with a string should be UTF-8');
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker responds to fetch event with string');
async_test(function(t) {
var scope = 'resources/simple.html?blob';
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(frame) {
assert_equals(
frame.contentDocument.body.textContent,
'Test blob',
'Service Worker should respond to fetch with a test string');
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker responds to fetch event with blob body');
async_test(function(t) {
var scope = 'resources/simple.html?referrer';
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(frame) {
assert_equals(
frame.contentDocument.body.textContent,
'Referrer: ' + document.location.href,
'Service Worker should respond to fetch with the referrer URL');
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker responds to fetch event with the referrer URL');
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) {
assert_equals(
response_text,
'Referrer: ' + href + '\n' +
'ReferrerPolicy: no-referrer-when-downgrade',
'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) {
assert_equals(
response_text,
'Referrer: about:client\n' +
'ReferrerPolicy: no-referrer-when-downgrade',
'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) {
assert_equals(
response_text,
'Referrer: ' + href + '\n' +
'ReferrerPolicy: no-referrer-when-downgrade',
'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) {
assert_equals(
response_text,
'Referrer: about:client\n' +
'ReferrerPolicy: no-referrer-when-downgrade',
'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) {
assert_equals(
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) {
assert_equals(
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) {
assert_equals(
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) {
assert_equals(
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) {
assert_equals(
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) {
assert_equals(
response_text,
'Referrer: about:client\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) {
assert_equals(
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) {
assert_equals(
response_text,
'Referrer: about:client\n' +
'ReferrerPolicy: no-referrer',
'Service Worker should respond to fetch with no referrer URL with "no-referrer"');
});
}
async_test(function(t) {
var scope = 'resources/simple.html?referrerPolicy';
var frame;
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(f) {
frame = f;
assert_equals(
frame.contentDocument.body.textContent,
'ReferrerPolicy: no-referrer-when-downgrade',
'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() + 'fake-referrer';
return run_referrer_policy_tests(frame, 'fake-referrer', referrer,
frame.contentDocument.location.origin);
})
.then(function() {
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker responds to fetch event with the referrer URL');
async_test(function(t) {
var scope = 'resources/simple.html?clientId';
var frame;
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(f) {
frame = f;
assert_equals(
frame.contentDocument.body.textContent,
'Client ID Not Found',
'Service Worker should respond to fetch with a client id');
return frame.contentWindow.fetch('resources/other.html?clientId');
})
.then(function(response) { return response.text(); })
.then(function(response_text) {
var new_client_id = response_text.substr(17);
assert_equals(
response_text.substr(0, 15),
'Client ID Found',
'Service Worker should respond to fetch with an existing client id');
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker responds to fetch event with an existing client id');
async_test(function(t) {
var scope = 'resources/simple.html?ignore';
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(frame) {
assert_equals(frame.contentDocument.body.textContent,
'Here\'s a simple html file.\n',
'Response should come from fallback to native fetch');
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker does not respond to fetch event');
async_test(function(t) {
var scope = 'resources/simple.html?null';
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(frame) {
assert_equals(frame.contentDocument.body.textContent,
'',
'Response should be the empty string');
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker responds to fetch event with null response body');
async_test(function(t) {
var scope = 'resources/simple.html?fetch';
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(frame) {
assert_equals(frame.contentDocument.body.textContent,
'Here\'s an other html file.\n',
'Response should come from fetched other file');
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker fetches other file in fetch event');
async_test(function(t) {
var scope = 'resources/simple.html?form-post';
var frame_name = 'xhr-post-frame';
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function(sw) {
return new Promise(function(resolve) {
var frame = document.createElement('iframe');
frame.name = frame_name;
document.body.appendChild(frame);
var form = document.createElement('form');
form.target = frame_name;
form.action = scope;
form.method = 'post';
var input1 = document.createElement('input');
input1.type = 'text';
input1.value = 'testValue1';
input1.name = 'testName1'
form.appendChild(input1);
var input2 = document.createElement('input');
input2.type = 'text';
input2.value = 'testValue2';
input2.name = 'testName2'
form.appendChild(input2);
document.body.appendChild(form);
frame.onload = function() {
document.body.removeChild(form);
resolve(frame);
};
form.submit();
});
})
.then(function(frame) {
assert_equals(frame.contentDocument.body.textContent,
'POST:application/x-www-form-urlencoded:' +
'testName1=testValue1&testName2=testValue2');
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker responds to fetch event with POST form');
async_test(function(t) {
var scope = 'resources/simple.html?multiple-respond-with';
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(frame) {
assert_equals(
frame.contentDocument.body.textContent,
'(0)(1)[InvalidStateError](2)[InvalidStateError]',
'Multiple calls of respondWith must throw InvalidStateErrors.');
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Multiple calls of respondWith must throw InvalidStateErrors');
async_test(function(t) {
var scope = 'resources/simple.html?used-check';
var first_frame;
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(frame) {
assert_equals(frame.contentDocument.body.textContent,
'Here\'s an other html file.\n',
'Response should come from fetched other file');
first_frame = frame;
return with_iframe(scope);
})
.then(function(frame) {
// When we access to the scope in the second time, the content of the
// response is generated inside the ServiceWorker. The body contains
// the value of bodyUsed of the first response which is already
// consumed by FetchEvent.respondWith method.
assert_equals(
frame.contentDocument.body.textContent,
'bodyUsed: true',
'event.respondWith must set the used flag.');
first_frame.remove();
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker event.respondWith must set the used flag');
async_test(function(t) {
var scope = 'resources/simple.html?fragment-check';
var fragment = '#/some/fragment';
var first_frame;
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope + fragment); })
.then(function(frame) {
assert_equals(
frame.contentDocument.body.textContent,
'Fragment Found :' + fragment,
'Service worker should expose URL fragments in request.');
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker must not expose FetchEvent URL fragments.');
async_test(function(t) {
var scope = 'resources/simple.html?cache';
var frame;
var cacheTypes = [
undefined, 'default', 'no-store', 'reload', 'no-cache', 'force-cache', 'only-if-cached'
];
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(f) {
frame = f;
assert_equals(frame.contentWindow.document.body.textContent, 'default');
var tests = cacheTypes.map(function(type) {
return new Promise(function(resolve, reject) {
var init = {cache: type};
if (type === 'only-if-cached') {
// For privacy reasons, for the time being, only-if-cached
// requires the mode to be same-origin.
init.mode = 'same-origin';
}
return frame.contentWindow.fetch(scope + '=' + type, init)
.then(function(response) { return response.text(); })
.then(function(response_text) {
var expected = (type === undefined) ? 'default' : type;
assert_equals(response_text, expected,
'Service Worker should respond to fetch with the correct type');
})
.then(resolve)
.catch(reject);
});
});
})
.then(function() {
return new Promise(function(resolve, reject) {
frame.addEventListener('load', function onLoad() {
frame.removeEventListener('load', onLoad);
try {
assert_equals(frame.contentWindow.document.body.textContent,
'no-cache');
resolve();
} catch (e) {
reject(e);
}
});
frame.contentWindow.location.reload();
});
})
.then(function() {
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker responds to fetch event with the correct cache types');
async_test(function(t) {
var scope = 'resources/simple.html?eventsource';
var frame;
function test_eventsource(opts) {
return new Promise(function(resolve, reject) {
var eventSource = new frame.contentWindow.EventSource(scope, opts);
eventSource.addEventListener('message', function(msg) {
eventSource.close();
try {
var data = JSON.parse(msg.data);
assert_equals(data.mode, 'cors',
'EventSource should make CORS requests.');
assert_equals(data.cache, 'no-store',
'EventSource should bypass the http cache.');
var expectedCredentials = opts.withCredentials ? 'include'
: 'same-origin';
assert_equals(data.credentials, expectedCredentials,
'EventSource should pass correct credentials mode.');
resolve();
} catch (e) {
reject(e);
}
});
eventSource.addEventListener('error', function(e) {
eventSource.close();
reject('The EventSource fired an error event.');
});
});
}
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(f) {
frame = f;
return test_eventsource({ withCredentials: false });
})
.then(function() {
return test_eventsource({ withCredentials: true });
})
.then(function() {
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker should intercept EventSource');
async_test(function(t) {
var scope = 'resources/simple.html?integrity';
var frame;
var integrity_metadata = 'gs0nqru8KbsrIt5YToQqS9fYao4GQJXtcId610g7cCU=';
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() { return with_iframe(scope); })
.then(function(f) {
frame = f;
// A request has associated integrity metadata (a string).
// Unless stated otherwise, it is the empty string.
assert_equals(
frame.contentDocument.body.textContent, '');
return new Promise(function(resolve, reject) {
return frame.contentWindow.fetch(scope,
{'integrity': integrity_metadata})
.then(function(response) {
return response.text();
})
.then(function(response_text) {
// Should get the same integrity metadata.
assert_equals(response_text, integrity_metadata,
'Service Worker should respond to fetch with the correct integrity');
})
.then(resolve())
.catch(reject());
});
})
.then(function() {
frame.remove();
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Service Worker responds to fetch event with the correct integrity_metadata');
</script>
</body>
|