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
content / test / data / cross_site_document_blocking / service_worker.js [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This service worker is used by the CrossSiteDocumentBlockingServiceWorkerTest
// browser test - please see the comments there for more details.
function createHtmlNoSniffResponse() {
var headers = new Headers();
headers.append('Content-Type', 'text/html');
headers.append('X-Content-Type-Options', 'nosniff');
return new Response('Response created by service worker',
{ status: 200, headers: headers });
}
self.addEventListener('fetch', function(event) {
// This handles response to the request issued in the
// CrossSiteDocumentBlockingServiceWorkerTest.NoNetwork test.
if (event.request.url.endsWith('data_from_service_worker')) {
event.respondWith(createHtmlNoSniffResponse());
return;
}
// This handles response to the request issued in the
// CrossSiteDocumentBlockingServiceWorkerTest.NetworkToServiceWorkerResponse
// test.
if (event.request.url.endsWith('nosniff.txt')) {
event.respondWith(
fetch(event.request).then(
response => Promise.reject('Expected error from service worker'),
error => Promise.reject('Unexpected error: ' + error)));
return;
}
// Let the request go to the network in all the other cases (e.g. when
// reloading the test page at cross_site_document_blocking/request.html).
});