Bug 1200337 - Part 2: Add a Web Platform Test to check the visibility of default headers during interception, r=bkelly

This commit is contained in:
Michael Layzell 2015-09-02 12:49:37 -04:00
parent 3bf16f96e5
commit 038c6f554c
4 changed files with 103 additions and 1 deletions

View File

@ -166,6 +166,12 @@
"url": "/_mozilla/service-workers/service-worker/fetch-frame-resource.https.html"
}
],
"service-workers/service-worker/fetch-header-visibility.https.html": [
{
"path": "service-workers/service-worker/fetch-header-visibility.https.html",
"url": "/_mozilla/service-workers/service-worker/fetch-header-visibility.https.html"
}
],
"service-workers/service-worker/fetch-mixed-content-to-inscope.https.html": [
{
"path": "service-workers/service-worker/fetch-mixed-content-to-inscope.https.html",

View File

@ -0,0 +1,50 @@
<!DOCTYPE html>
<title>Service Worker: Visibility of headers during fetch.</title>
<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-rewrite-worker.js';
var path = base_path() + 'resources/fetch-access-control.py';
var host_info = get_host_info();
async_test(function(t) {
var scope = 'resources/fetch-header-visibility-iframe.html';
service_worker_unregister_and_register(t, worker, scope)
.then(function(reg) {
return wait_for_state(t, reg.installing, 'activated');
})
.then(function() {
var frame = document.createElement('iframe');
frame.src = scope;
document.body.appendChild(frame);
// Resolve a promise when we recieve 2 success messages
return new Promise(function(resolve, reject) {
var remaining = 2;
function onMessage(e) {
if (e.data == 'PASS') {
remaining--;
if (remaining == 0) {
resolve();
} else {
return;
}
} else {
reject(e.data);
}
window.removeEventListener('message', onMessage);
}
window.addEventListener('message', onMessage);
});
})
.then(function(result) {
return service_worker_unregister_and_done(t, scope);
})
.catch(unreached_rejection(t));
}, 'Visibility of defaulted headers during interception');
</script>
</body>

View File

@ -0,0 +1,35 @@
<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();
var uri = document.location + '?check-ua-header';
var headers = new Headers();
headers.set('User-Agent', 'custom_ua');
// Check the custom UA case
fetch(uri, { headers: headers }).then(function(response) {
return response.text();
}).then(function(text) {
if (text == 'custom_ua') {
parent.postMessage('PASS', '*');
} else {
parent.postMessage('withUA FAIL - expected "custom_ua", got "' + text + '"', '*');
}
}).catch(function(err) {
parent.postMessage('withUA FAIL - unexpected error: ' + err, '*');
});
// Check the default UA case
fetch(uri, {}).then(function(response) {
return response.text();
}).then(function(text) {
if (text == 'NO_UA') {
parent.postMessage('PASS', '*');
} else {
parent.postMessage('noUA FAIL - expected "NO_UA", got "' + text + '"', '*');
}
}).catch(function(err) {
parent.postMessage('noUA FAIL - unexpected error: ' + err, '*');
});
</script>

View File

@ -47,11 +47,22 @@ self.addEventListener('fetch', function(event) {
'gQLABKXJBqMGjBoAAqMGDLwBDAwAEsoCTFWunmQAAAAASUVORK5CYII=');
var array = new Uint8Array(binary.length);
for(var i = 0; i < binary.length; i++) {
array[i] = binary.charCodeAt(i)
array[i] = binary.charCodeAt(i);
};
event.respondWith(new Response(new Blob([array], {type: 'image/png'})));
return;
}
if (params['check-ua-header']) {
var ua = event.request.headers.get('User-Agent');
if (ua) {
// We have a user agent!
event.respondWith(new Response(new Blob([ua])));
} else {
// We don't have a user-agent!
event.respondWith(new Response(new Blob(["NO_UA"])));
}
return;
}
event.respondWith(new Promise(function(resolve, reject) {
var request = event.request;
if (url) {