Skip to content

Commit

Permalink
Add tentative WPT tests for stale while revalidate handling.
Browse files Browse the repository at this point in the history
Add test to ensure that handling the fetch doesn't trigger stale
while revalidate loading.

Add test to ensure that scripts loaded trigger a stale while revalidate
cache hit and resource timing entries are correct.

The PR for the spec changes is here:
whatwg/fetch#853

BUG=348877

Change-Id: Ib07b98d0d2595b6b99857161f830343bf7516518
  • Loading branch information
dtapuska authored and chromium-wpt-export-bot committed Jan 22, 2019
1 parent 538eac8 commit 12e47c4
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 0 deletions.
17 changes: 17 additions & 0 deletions fetch/stale-while-revalidate/fetch.tentative.html
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<!---
Tentative test against:
https://github.com/whatwg/fetch/pull/853
-->
<meta charset="utf-8">
<title>Tests Stale While Revalidate is not executed for fetch API</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>
promise_test(async (test) => {
const response = await fetch(`stale-script.py`);
const response2 = await fetch(`stale-script.py`);

assert_not_equals(response.headers.get('Token'), response2.headers.get('Token'));
}, 'Second fetch does not return same response');
</script>
20 changes: 20 additions & 0 deletions fetch/stale-while-revalidate/stale-image.py
@@ -0,0 +1,20 @@
import os.path

def main(request, response):

filename = "green-16x16.png"
for cookie in request.cookies.get_list('a'):
filename = "green-256x256.png"

path = os.path.join(os.path.dirname(__file__), "../../images", filename)
body = open(path, "rb").read()

response.add_required_headers = False
response.writer.write_status(200)
response.writer.write_header("content-length", len(body))
response.writer.write_header("Cache-Control", "private, max-age=0, stale-while-revalidate=10")
response.writer.write_header("content-type", "image/png")
response.writer.write_header("Set-Cookie", "a=b")
response.writer.end_headers()

response.writer.write(body)
64 changes: 64 additions & 0 deletions fetch/stale-while-revalidate/stale-image.tentative.html
@@ -0,0 +1,64 @@
<!DOCTYPE html>
<!---
Tentative test against:
https://github.com/whatwg/fetch/pull/853
-->
<meta charset="utf-8">
<title>Tests Stale While Revalidate works for images</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>

async_test(t => {
window.onload = t.step_func(() => {
step_timeout(() => {
assert_equals(document.getElementById("firstimage").width, 16, "Width is 16");
var img2 = document.createElement("img");
img2.onload = t.step_func(() => {
var resources = performance.getEntriesByType("resource");
var count = 0;
for (var i=0; i < resources.length; i++) {
if (resources[i].name.indexOf('stale-image.py') != -1) {
count++;
}
}
// We shouldn't have the stale while revalidate resource load yet
// as it should be issued during an idle period.
assert_greater_than_equal(count, 1, "performance entries");
assert_equals(img.width, 16, "image dimension");

// Ensure that we get a performance resource entry for the stale
// while revalidate load.
var checkResult = () => {
var resources = performance.getEntriesByType("resource");
var count = 0;
for (var i=0; i < resources.length; i++) {
if (resources[i].name.indexOf('stale-image.py') != -1) {
count++;
}
}

if (count < 2) {
t.step_timeout(checkResult, 25);
return;
}

assert_equals(count, 2, "Performance count");
t.done();
};

t.step_timeout(checkResult, 25);
});
img2.src = "stale-image.py";
document.body.appendChild(img2);
}, 0);
});
}, 'Cache returns stale resource');

var img = document.createElement("img");
img.src = "stale-image.py";
img.id = "firstimage";
document.body.appendChild(img);
</script>
</body>
13 changes: 13 additions & 0 deletions fetch/stale-while-revalidate/stale-script.py
@@ -0,0 +1,13 @@
import random, string

def token():
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(20))

def main(request, response):
unique_id = token()
headers = [("Content-Type", "text/javascript"),
("Cache-Control", "private, max-age=0, stale-while-revalidate=10"),
("Token", unique_id)]
content = "report('{}')".format(unique_id)
return 200, headers, content
72 changes: 72 additions & 0 deletions fetch/stale-while-revalidate/stale-script.tentative.html
@@ -0,0 +1,72 @@
<!DOCTYPE html>
<!---
Tentative test against:
https://github.com/whatwg/fetch/pull/853
-->
<meta charset="utf-8">
<title>Tests Stale While Revalidate works for scripts</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<script>
var last_modified;
var last_modified_count = 0;

// The script will call report via a uniquely generated ID on the subresource.
// If it is a cache hit the ID will be the same and the test will pass.
function report(mod) {
if (!last_modified) {
last_modified = mod;
last_modified_count = 1;
} else if (last_modified == mod) {
last_modified_count++;
}
}

async_test(t => {
window.onload = t.step_func(() => {
step_timeout(() => {
var script = document.createElement("script");
script.src = "stale-script.py";
document.body.appendChild(script);
script.onload = t.step_func(() => {
var resources = performance.getEntriesByType("resource");
var count = 0;
for (var i=0; i < resources.length; i++) {
if (resources[i].name.indexOf('stale-script.py') != -1) {
count++;
}
}
assert_greater_than_equal(count, 1, "performance entries");
assert_equals(last_modified_count, 2, "last modified");

// Ensure that we get a performance resource entry for the stale
// while revalidate load.
var checkResult = () => {
var resources = performance.getEntriesByType("resource");
var count = 0;
for (var i=0; i < resources.length; i++) {
if (resources[i].name.indexOf('stale-script.py') != -1) {
count++;
}
}
if (count < 2) {
t.step_timeout(checkResult, 25);
return;
}

assert_equals(count, 2, "Performance count");
t.done();
};

t.step_timeout(checkResult, 25);
});
}, 0);
});
}, 'Cache returns stale resource');

var script = document.createElement("script");
script.src = "stale-script.py";
document.body.appendChild(script);
</script>
</body>

0 comments on commit 12e47c4

Please sign in to comment.