Bug 1206298 - Part 2: Add a test to make sure that we respect Cache-Control headers on the synthesized response; r=jdm

This commit is contained in:
Ehsan Akhgari 2015-10-22 09:24:50 -04:00
Родитель 0549164d1b
Коммит 073195c513
8 изменённых файлов: 178 добавлений и 0 удалений

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 87 B

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 123 B

Просмотреть файл

@ -0,0 +1,29 @@
<!DOCTYPE html>
<script>
var width, url, width2, url2;
function maybeReport() {
if (width !== undefined && url !== undefined &&
width2 !== undefined && url2 !== undefined) {
window.parent.postMessage({status: "result",
width: width,
width2: width2,
url: url,
url2: url2}, "*");
}
}
onload = function() {
width = document.querySelector("img").width;
width2 = document.querySelector("img").width;
maybeReport();
};
navigator.serviceWorker.onmessage = function(event) {
if (event.data.suffix == "2") {
url2 = event.data.url;
} else {
url = event.data.url;
}
maybeReport();
};
</script>
<img src="image.png">
<img src="image2.png">

Просмотреть файл

@ -0,0 +1,41 @@
function synthesizeImage(suffix) {
// Serve image-20px for the first page, and image-40px for the second page.
return clients.matchAll().then(clients => {
var url = "image-20px.png";
clients.forEach(client => {
if (client.url.indexOf("?new") > 0) {
url = "image-40px.png";
}
client.postMessage({suffix: suffix, url: url});
});
return fetch(url);
}).then(response => {
return response.arrayBuffer();
}).then(ab => {
var headers;
if (suffix == "") {
headers = {
"Content-Type": "image/png",
"Date": "Tue, 1 Jan 1990 01:02:03 GMT",
"Cache-Control": "max-age=1",
};
} else {
headers = {
"Content-Type": "image/png",
"Cache-Control": "no-cache",
};
}
return new Response(ab, {
status: 200,
headers: headers,
});
});
}
self.addEventListener("fetch", function(event) {
if (event.request.url.indexOf("image.png") >= 0) {
event.respondWith(synthesizeImage(""));
} else if (event.request.url.indexOf("image2.png") >= 0) {
event.respondWith(synthesizeImage("2"));
}
});

Просмотреть файл

@ -0,0 +1,14 @@
<!DOCTYPE html>
<script>
function ok(v, msg) {
window.parent.postMessage({status: "ok", result: !!v, message: msg}, "*");
}
function done(reg) {
ok(reg.active, "The active worker should be available.");
window.parent.postMessage({status: "registrationdone"}, "*");
}
navigator.serviceWorker.ready.then(done);
navigator.serviceWorker.register("maxage_test.js", {scope: "."});
</script>

Просмотреть файл

@ -0,0 +1,12 @@
<!DOCTYPE html>
<script>
navigator.serviceWorker.getRegistration(".").then(function(registration) {
registration.unregister().then(function(success) {
if (success) {
window.parent.postMessage({status: "unregistrationdone"}, "*");
}
}, function(e) {
dump("Unregistering the SW failed with " + e + "\n");
});
});
</script>

Просмотреть файл

@ -59,6 +59,12 @@ support-files =
fetch/https/clonedresponse/register.html
fetch/https/clonedresponse/unregister.html
fetch/https/clonedresponse/https_test.js
fetch/imagecache-maxage/index.html
fetch/imagecache-maxage/image-20px.png
fetch/imagecache-maxage/image-40px.png
fetch/imagecache-maxage/maxage_test.js
fetch/imagecache-maxage/register.html
fetch/imagecache-maxage/unregister.html
fetch/interrupt.sjs
fetch/origin/index.sjs
fetch/origin/index-to-https.sjs
@ -282,3 +288,4 @@ skip-if = e10s # Bug 1214305
[test_serviceworker_header.html]
[test_openWindow.html]
skip-if = toolkit == "android" || toolkit == "gonk" || e10s
[test_imagecache_max_age.html]

Просмотреть файл

@ -0,0 +1,75 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
<title>Test that the image cache respects a synthesized image's Cache headers</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>
<div id="content">
<iframe></iframe>
</div>
<pre id="test"></pre>
<script class="testbody" type="text/javascript">
var iframe;
var framesLoaded = 0;
function runTest() {
iframe = document.querySelector("iframe");
iframe.src = "/tests/dom/workers/test/serviceworkers/fetch/imagecache-maxage/register.html";
window.onmessage = function(e) {
if (e.data.status == "ok") {
ok(e.data.result, e.data.message);
} else if (e.data.status == "registrationdone") {
iframe.src = "/tests/dom/workers/test/serviceworkers/fetch/imagecache-maxage/index.html";
} else if (e.data.status == "result") {
switch (++framesLoaded) {
case 1:
is(e.data.url, "image-20px.png", "Correct url expected");
is(e.data.url2, "image-20px.png", "Correct url expected");
is(e.data.width, 20, "Correct width expected");
is(e.data.width2, 20, "Correct width expected");
// Wait for 100ms so that the image gets expired.
setTimeout(function() {
iframe.src = "/tests/dom/workers/test/serviceworkers/fetch/imagecache-maxage/index.html?new"
}, 100);
break;
case 2:
is(e.data.url, "image-40px.png", "Correct url expected");
is(e.data.url2, "image-40px.png", "Correct url expected");
// TODO: Uncomment this check when bug 1217571 gets fixed.
// Currently because of bug 1217571, the QI in imgCacheValidator::OnStartRequest()
// to nsICachingChannel fails, which causes the check below to fail in non-e10s.
//is(e.data.width, 40, "Correct width expected");
//is(e.data.width2, 40, "Correct width expected");
iframe.src = "/tests/dom/workers/test/serviceworkers/fetch/imagecache-maxage/unregister.html";
break;
default:
ok(false, "This should never happen");
}
} else if (e.data.status == "unregistrationdone") {
window.onmessage = null;
SimpleTest.finish();
}
};
}
SimpleTest.requestFlakyTimeout("This test needs to simulate the passing of time");
SimpleTest.waitForExplicitFinish();
onload = function() {
SpecialPowers.pushPrefEnv({"set": [
["dom.serviceWorkers.exemptFromPerDomainMax", true],
["dom.serviceWorkers.enabled", true],
["dom.serviceWorkers.testing.enabled", true],
["dom.serviceWorkers.interception.enabled", true],
]}, runTest);
};
</script>
</pre>
</body>
</html>