2015-03-23 20:36:44 +03:00
|
|
|
var seenIndex = false;
|
|
|
|
|
2015-02-19 04:34:29 +03:00
|
|
|
onfetch = function(ev) {
|
2016-02-04 18:28:21 +03:00
|
|
|
if (ev.request.url.includes("ignore")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-28 17:47:35 +03:00
|
|
|
if (ev.request.url.includes("bare-synthesized.txt")) {
|
2015-03-17 18:48:31 +03:00
|
|
|
ev.respondWith(Promise.resolve(
|
|
|
|
new Response("synthesized response body", {})
|
|
|
|
));
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
2015-07-23 17:25:12 +03:00
|
|
|
else if (ev.request.url.includes('file_CrossSiteXHR_server.sjs')) {
|
|
|
|
// N.B. this response would break the rules of CORS if it were allowed, but
|
|
|
|
// this test relies upon the preflight request not being intercepted and
|
|
|
|
// thus this response should not be used.
|
|
|
|
if (ev.request.method == 'OPTIONS') {
|
|
|
|
ev.respondWith(new Response('', {headers: {'Access-Control-Allow-Origin': '*',
|
|
|
|
'Access-Control-Allow-Headers': 'X-Unsafe'}}))
|
2015-08-28 19:05:07 +03:00
|
|
|
} else if (ev.request.url.includes('example.org')) {
|
|
|
|
ev.respondWith(fetch(ev.request));
|
2015-07-23 17:25:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("synthesized-404.txt")) {
|
2015-03-17 18:48:31 +03:00
|
|
|
ev.respondWith(Promise.resolve(
|
|
|
|
new Response("synthesized response body", { status: 404 })
|
|
|
|
));
|
2015-03-17 18:48:30 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("synthesized-headers.txt")) {
|
2015-03-17 18:48:31 +03:00
|
|
|
ev.respondWith(Promise.resolve(
|
|
|
|
new Response("synthesized response body", {
|
2015-03-17 18:48:30 +03:00
|
|
|
headers: {
|
|
|
|
"X-Custom-Greeting": "Hello"
|
|
|
|
}
|
2015-03-17 18:48:31 +03:00
|
|
|
})
|
|
|
|
));
|
2015-03-17 18:48:30 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("test-respondwith-response.txt")) {
|
2015-03-20 18:44:20 +03:00
|
|
|
ev.respondWith(new Response("test-respondwith-response response body", {}));
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("synthesized-redirect-real-file.txt")) {
|
2015-02-26 19:53:51 +03:00
|
|
|
ev.respondWith(Promise.resolve(
|
|
|
|
Response.redirect("fetch/real-file.txt")
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-05-28 17:47:35 +03:00
|
|
|
else if (ev.request.url.includes("synthesized-redirect-twice-real-file.txt")) {
|
|
|
|
ev.respondWith(Promise.resolve(
|
|
|
|
Response.redirect("synthesized-redirect-real-file.txt")
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("synthesized-redirect-synthesized.txt")) {
|
2015-02-26 19:53:51 +03:00
|
|
|
ev.respondWith(Promise.resolve(
|
2015-05-28 17:47:35 +03:00
|
|
|
Response.redirect("bare-synthesized.txt")
|
2015-02-26 19:53:51 +03:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-05-28 17:47:35 +03:00
|
|
|
else if (ev.request.url.includes("synthesized-redirect-twice-synthesized.txt")) {
|
|
|
|
ev.respondWith(Promise.resolve(
|
|
|
|
Response.redirect("synthesized-redirect-synthesized.txt")
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("rejected.txt")) {
|
2015-03-17 18:48:31 +03:00
|
|
|
ev.respondWith(Promise.reject());
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("nonresponse.txt")) {
|
2015-03-17 18:48:31 +03:00
|
|
|
ev.respondWith(Promise.resolve(5));
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("nonresponse2.txt")) {
|
2015-03-17 18:48:31 +03:00
|
|
|
ev.respondWith(Promise.resolve({}));
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
2015-08-07 04:12:14 +03:00
|
|
|
else if (ev.request.url.includes("nonpromise.txt")) {
|
|
|
|
try {
|
|
|
|
// This should coerce to Promise(5) instead of throwing
|
|
|
|
ev.respondWith(5);
|
|
|
|
} catch (e) {
|
|
|
|
// test is expecting failure, so return a success if we get a thrown
|
|
|
|
// exception
|
|
|
|
ev.respondWith(new Response('respondWith(5) threw ' + e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("headers.txt")) {
|
2015-03-17 18:48:31 +03:00
|
|
|
var ok = true;
|
|
|
|
ok &= ev.request.headers.get("X-Test1") == "header1";
|
|
|
|
ok &= ev.request.headers.get("X-Test2") == "header2";
|
|
|
|
ev.respondWith(Promise.resolve(
|
|
|
|
new Response(ok.toString(), {})
|
|
|
|
));
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
else if (ev.request.url.includes("readable-stream.txt")) {
|
|
|
|
ev.respondWith(
|
|
|
|
new Response(
|
|
|
|
new ReadableStream({
|
|
|
|
start: function(controller) {
|
|
|
|
controller.enqueue(new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21]));
|
|
|
|
controller.close();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (ev.request.url.includes("readable-stream-locked.txt")) {
|
|
|
|
let stream = new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
controller.enqueue(new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21]));
|
|
|
|
controller.close();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
ev.respondWith(new Response(stream));
|
|
|
|
|
|
|
|
// This locks the stream.
|
|
|
|
stream.getReader();
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (ev.request.url.includes("readable-stream-with-exception.txt")) {
|
|
|
|
ev.respondWith(
|
|
|
|
new Response(
|
|
|
|
new ReadableStream({
|
|
|
|
start(controller) {},
|
|
|
|
pull() {
|
|
|
|
throw "EXCEPTION!";
|
|
|
|
}
|
|
|
|
})
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:56 +03:00
|
|
|
else if (ev.request.url.includes("readable-stream-with-exception2.txt")) {
|
|
|
|
ev.respondWith(
|
|
|
|
new Response(
|
|
|
|
new ReadableStream({
|
|
|
|
_controller: null,
|
|
|
|
_count: 0,
|
|
|
|
|
|
|
|
start(controller) { this._controller = controller; },
|
|
|
|
pull() {
|
|
|
|
if (++this._count == 5) { throw "EXCEPTION 2!"; }
|
|
|
|
this._controller.enqueue(new Uint8Array([this._count]));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:04:55 +03:00
|
|
|
else if (ev.request.url.includes("readable-stream-already-consumed.txt")) {
|
|
|
|
let r = new Response(
|
|
|
|
new ReadableStream({
|
|
|
|
start(controller) {
|
|
|
|
controller.enqueue(new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21]));
|
|
|
|
controller.close();
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
r.blob();
|
|
|
|
|
|
|
|
ev.respondWith(r);
|
|
|
|
}
|
|
|
|
|
2016-01-14 02:14:38 +03:00
|
|
|
else if (ev.request.url.includes('user-pass')) {
|
|
|
|
ev.respondWith(new Response(ev.request.url));
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("nonexistent_image.gif")) {
|
2015-08-12 09:50:28 +03:00
|
|
|
var imageAsBinaryString = atob("R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs");
|
|
|
|
var imageLength = imageAsBinaryString.length;
|
|
|
|
|
|
|
|
// If we just pass |imageAsBinaryString| to the Response constructor, an
|
|
|
|
// encoding conversion occurs that corrupts the image. Instead, we need to
|
|
|
|
// convert it to a typed array.
|
|
|
|
// typed array.
|
|
|
|
var imageAsArray = new Uint8Array(imageLength);
|
|
|
|
for (var i = 0; i < imageLength; ++i) {
|
|
|
|
imageAsArray[i] = imageAsBinaryString.charCodeAt(i);
|
|
|
|
}
|
|
|
|
|
2015-03-17 18:48:31 +03:00
|
|
|
ev.respondWith(Promise.resolve(
|
2015-08-12 09:50:28 +03:00
|
|
|
new Response(imageAsArray, { headers: { "Content-Type": "image/gif" } })
|
2015-03-17 18:48:31 +03:00
|
|
|
));
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("nonexistent_script.js")) {
|
2015-03-17 18:48:31 +03:00
|
|
|
ev.respondWith(Promise.resolve(
|
|
|
|
new Response("check_intercepted_script();", {})
|
|
|
|
));
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("nonexistent_stylesheet.css")) {
|
2015-03-17 18:48:31 +03:00
|
|
|
ev.respondWith(Promise.resolve(
|
|
|
|
new Response("#style-test { background-color: black !important; }", {
|
2015-03-17 18:48:30 +03:00
|
|
|
headers : {
|
|
|
|
"Content-Type": "text/css"
|
|
|
|
}
|
2015-03-17 18:48:31 +03:00
|
|
|
})
|
|
|
|
));
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("nonexistent_page.html")) {
|
2015-03-17 18:48:31 +03:00
|
|
|
ev.respondWith(Promise.resolve(
|
|
|
|
new Response("<script>window.frameElement.test_result = true;</script>", {
|
2015-03-17 18:48:30 +03:00
|
|
|
headers : {
|
|
|
|
"Content-Type": "text/html"
|
|
|
|
}
|
2015-03-17 18:48:31 +03:00
|
|
|
})
|
|
|
|
));
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
2016-02-24 21:46:38 +03:00
|
|
|
else if (ev.request.url.includes("navigate.html")) {
|
|
|
|
var requests = [ // should not throw
|
|
|
|
new Request(ev.request),
|
|
|
|
new Request(ev.request, undefined),
|
|
|
|
new Request(ev.request, null),
|
|
|
|
new Request(ev.request, {}),
|
|
|
|
new Request(ev.request, {someUnrelatedProperty: 42}),
|
2017-03-01 13:44:38 +03:00
|
|
|
new Request(ev.request, {method: "GET"}),
|
2016-02-24 21:46:38 +03:00
|
|
|
];
|
2017-03-01 13:44:38 +03:00
|
|
|
ev.respondWith(Promise.resolve(
|
|
|
|
new Response("<script>window.frameElement.test_result = true;</script>", {
|
|
|
|
headers : {
|
|
|
|
"Content-Type": "text/html"
|
|
|
|
}
|
|
|
|
})
|
|
|
|
));
|
2016-02-24 21:46:38 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("nonexistent_worker_script.js")) {
|
2015-03-17 18:48:31 +03:00
|
|
|
ev.respondWith(Promise.resolve(
|
|
|
|
new Response("postMessage('worker-intercept-success')", {})
|
|
|
|
));
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("nonexistent_imported_script.js")) {
|
2015-03-17 18:48:31 +03:00
|
|
|
ev.respondWith(Promise.resolve(
|
|
|
|
new Response("check_intercepted_script();", {})
|
|
|
|
));
|
2015-02-19 04:34:29 +03:00
|
|
|
}
|
2015-03-17 18:48:32 +03:00
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("deliver-gzip")) {
|
2015-03-17 18:48:32 +03:00
|
|
|
// Don't handle the request, this will make Necko perform a network request, at
|
|
|
|
// which point SetApplyConversion must be re-enabled, otherwise the request
|
|
|
|
// will fail.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("hello.gz")) {
|
2015-03-17 18:48:32 +03:00
|
|
|
ev.respondWith(fetch("fetch/deliver-gzip.sjs"));
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("hello-after-extracting.gz")) {
|
2015-03-17 18:48:32 +03:00
|
|
|
ev.respondWith(fetch("fetch/deliver-gzip.sjs").then(function(res) {
|
|
|
|
return res.text().then(function(body) {
|
|
|
|
return new Response(body, { status: res.status, statusText: res.statusText, headers: res.headers });
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
}
|
2015-03-23 20:36:44 +03:00
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes('opaque-on-same-origin')) {
|
2015-06-09 02:21:31 +03:00
|
|
|
var url = 'http://example.com/tests/dom/security/test/cors/file_CrossSiteXHR_server.sjs?status=200';
|
2015-03-17 18:47:03 +03:00
|
|
|
ev.respondWith(fetch(url, { mode: 'no-cors' }));
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes('opaque-no-cors')) {
|
2015-03-17 18:47:03 +03:00
|
|
|
if (ev.request.mode != "no-cors") {
|
|
|
|
ev.respondWith(Promise.reject());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-09 02:21:31 +03:00
|
|
|
var url = 'http://example.com/tests/dom/security/test/cors/file_CrossSiteXHR_server.sjs?status=200';
|
2015-03-17 18:47:03 +03:00
|
|
|
ev.respondWith(fetch(url, { mode: ev.request.mode }));
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes('cors-for-no-cors')) {
|
2015-03-17 18:47:03 +03:00
|
|
|
if (ev.request.mode != "no-cors") {
|
|
|
|
ev.respondWith(Promise.reject());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-09 02:21:31 +03:00
|
|
|
var url = 'http://example.com/tests/dom/security/test/cors/file_CrossSiteXHR_server.sjs?status=200&allowOrigin=*';
|
2015-03-17 18:47:03 +03:00
|
|
|
ev.respondWith(fetch(url));
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes('example.com')) {
|
2015-03-17 18:47:02 +03:00
|
|
|
ev.respondWith(fetch(ev.request));
|
|
|
|
}
|
|
|
|
|
2015-04-29 18:32:05 +03:00
|
|
|
else if (ev.request.url.includes("index.html")) {
|
2015-03-23 20:36:44 +03:00
|
|
|
if (seenIndex) {
|
|
|
|
var body = "<script>" +
|
|
|
|
"opener.postMessage({status: 'ok', result: " + ev.isReload + "," +
|
|
|
|
"message: 'reload status should be indicated'}, '*');" +
|
|
|
|
"opener.postMessage({status: 'done'}, '*');" +
|
|
|
|
"</script>";
|
|
|
|
ev.respondWith(new Response(body, {headers: {'Content-Type': 'text/html'}}));
|
|
|
|
} else {
|
|
|
|
seenIndex = true;
|
|
|
|
ev.respondWith(fetch(ev.request.url));
|
|
|
|
}
|
|
|
|
}
|
2015-05-04 23:54:22 +03:00
|
|
|
|
|
|
|
else if (ev.request.url.includes("body-")) {
|
|
|
|
ev.respondWith(ev.request.text().then(function (body) {
|
|
|
|
return new Response(body + body);
|
|
|
|
}));
|
|
|
|
}
|
2015-06-03 11:02:40 +03:00
|
|
|
|
|
|
|
else if (ev.request.url.includes('something.txt')) {
|
|
|
|
ev.respondWith(Response.redirect('fetch/somethingelse.txt'));
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (ev.request.url.includes('somethingelse.txt')) {
|
|
|
|
ev.respondWith(new Response('something else response body', {}));
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (ev.request.url.includes('redirect_serviceworker.sjs')) {
|
|
|
|
// The redirect_serviceworker.sjs server-side JavaScript file redirects to
|
2018-01-27 00:08:59 +03:00
|
|
|
// 'http://mochi.test:8888/tests/dom/serviceworkers/test/worker.js'
|
2015-06-03 11:02:40 +03:00
|
|
|
// The redirected fetch should not go through the SW since the original
|
|
|
|
// fetch was initiated from a SW.
|
|
|
|
ev.respondWith(fetch('redirect_serviceworker.sjs'));
|
|
|
|
}
|
2015-08-12 07:43:27 +03:00
|
|
|
|
|
|
|
else if (ev.request.url.includes('load_cross_origin_xml_document_synthetic.xml')) {
|
|
|
|
if (ev.request.mode != 'same-origin') {
|
|
|
|
ev.respondWith(Promise.reject());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ev.respondWith(Promise.resolve(
|
|
|
|
new Response("<response>body</response>", { headers: {'Content-Type': 'text/xtml'}})
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (ev.request.url.includes('load_cross_origin_xml_document_cors.xml')) {
|
|
|
|
if (ev.request.mode != 'same-origin') {
|
|
|
|
ev.respondWith(Promise.reject());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var url = 'http://example.com/tests/dom/security/test/cors/file_CrossSiteXHR_server.sjs?status=200&allowOrigin=*';
|
|
|
|
ev.respondWith(fetch(url, { mode: 'cors' }));
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (ev.request.url.includes('load_cross_origin_xml_document_opaque.xml')) {
|
|
|
|
if (ev.request.mode != 'same-origin') {
|
|
|
|
Promise.resolve(
|
|
|
|
new Response("<error>Invalid Request mode</error>", { headers: {'Content-Type': 'text/xtml'}})
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var url = 'http://example.com/tests/dom/security/test/cors/file_CrossSiteXHR_server.sjs?status=200';
|
|
|
|
ev.respondWith(fetch(url, { mode: 'no-cors' }));
|
|
|
|
}
|
2015-08-26 18:59:04 +03:00
|
|
|
|
|
|
|
else if (ev.request.url.includes('xhr-method-test.txt')) {
|
|
|
|
ev.respondWith(new Response('intercepted ' + ev.request.method));
|
|
|
|
}
|
2015-08-27 17:14:03 +03:00
|
|
|
|
|
|
|
else if (ev.request.url.includes('empty-header')) {
|
|
|
|
if (!ev.request.headers.has("emptyheader") ||
|
|
|
|
ev.request.headers.get("emptyheader") !== "") {
|
|
|
|
ev.respondWith(Promise.reject());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ev.respondWith(new Response("emptyheader"));
|
|
|
|
}
|
2015-10-25 03:24:45 +03:00
|
|
|
|
|
|
|
else if (ev.request.url.includes('fetchevent-extendable')) {
|
|
|
|
if (ev instanceof ExtendableEvent) {
|
|
|
|
ev.respondWith(new Response("extendable"));
|
|
|
|
} else {
|
|
|
|
ev.respondWith(Promise.reject());
|
|
|
|
}
|
|
|
|
}
|
2015-10-26 22:50:50 +03:00
|
|
|
|
|
|
|
else if (ev.request.url.includes('fetchevent-request')) {
|
2016-01-09 06:35:22 +03:00
|
|
|
var threw = false;
|
|
|
|
try {
|
|
|
|
new FetchEvent("foo");
|
|
|
|
} catch(e) {
|
|
|
|
if (e.name == "TypeError") {
|
|
|
|
threw = true;
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
ev.respondWith(new Response(threw ? "non-nullable" : "nullable"));
|
2015-10-26 22:50:50 +03:00
|
|
|
}
|
|
|
|
}
|
2015-06-03 11:02:40 +03:00
|
|
|
};
|