Bug 1210581: Test controlled worker loads (XHR, fetch, importScripts). r=ehsan

This commit is contained in:
Kyle Huey 2015-10-01 16:44:17 -07:00
parent c3a387fd88
commit 59a266ed13
4 changed files with 109 additions and 0 deletions

View File

@ -0,0 +1,29 @@
self.onmessage = function (evt) {
if (evt.data == "xhr") {
var xhr = new XMLHttpRequest();
xhr.open("GET", "synthesized-response.txt", true);
xhr.responseType = "text";
xhr.send();
xhr.onload = function (evt) {
postMessage(xhr.responseText);
};
xhr.onerror = function() {
postMessage("XHR failed!");
};
} else if (evt.data == "fetch") {
fetch("synthesized-response.txt")
.then(function(response) {
return response.text();
})
.then(function(data) {
postMessage(data);
})
.catch(function(error) {
postMessage("Fetch failed!");
});
} else if (evt.data == "importScripts") {
importScripts("synthesized-response.js");
} else {
throw "Unexpected message! " + evt.data;
}
};

View File

@ -0,0 +1,40 @@
<script src="/resources/testharness.js"></script>
<script src="../resources/get-host-info.sub.js"></script>
<script src="test-helpers.sub.js?pipe=sub"></script>
<script>
var host_info = get_host_info();
function boilerplate_test(msg) {
return new Promise(function(resolve, reject) {
var worker = new Worker("load_worker.js");
worker.onmessage = function(e) { resolve(e.data) };
worker.onerror = function(e) { reject(e) };
worker.postMessage(msg);
})
.then(function(data) {
assert_equals(data, "This load was successfully intercepted.");
});
}
function xhr_test() {
return boilerplate_test("xhr");
}
function fetch_test() {
return boilerplate_test("fetch");
}
function importScripts_test() {
return boilerplate_test("importScripts");
}
window.addEventListener('message', function(evt) {
var port = evt.ports[0];
xhr_test()
.then(fetch_test)
.then(importScripts_test)
.then(function() { port.postMessage({results: 'finish'}); })
.catch(function(e) { port.postMessage({results: 'failure:' + e}); });
});
</script>

View File

@ -0,0 +1,14 @@
importScripts('get-host-info.sub.js');
var response_text = "This load was successfully intercepted.";
var response_script = "postMessage(\"This load was successfully intercepted.\");";
self.onfetch = function(event) {
var url = event.request.url;
if (url.indexOf("synthesized-response.txt") != -1) {
event.respondWith(new Response(response_text));
} else if (url.indexOf("synthesized-response.js") != -1) {
event.respondWith(new Response(response_script));
}
};

View File

@ -118,5 +118,31 @@ promise_test(function(t) {
});
}, 'Verify worker script intercepted by no-cors cross-origin response fails');
promise_test(function(t) {
var subdoc_url = 'resources/worker-interception-iframe.https.html?bypass';
var service_worker = 'resources/worker-load-interceptor.js';
var scope = 'resources/';
return service_worker_unregister_and_register(t, service_worker, scope)
.then(function(r) {
return wait_for_state(t, r.installing, 'activated');
})
.then(function() { return with_iframe(subdoc_url); })
.then(function(frame) {
return new Promise(function(resolve, reject) {
var channel = new MessageChannel();
channel.port1.onmessage = function(e) {
resolve(e.data);
}
frame.contentWindow.postMessage("GO", "*", [channel.port2]);
});
})
.then(function(data) {
assert_equals(data.results, 'finish');
service_worker_unregister_and_done(t, scope);
});
}, 'Verify worker loads from controlled document are intercepted by Service Worker');
</script>
</body>