Commit 3a24e90c authored by Dave Tapuska's avatar Dave Tapuska Committed by Commit Bot

Reland add tentative WPT tests for stale while revalidate handling.

Add test to ensure that handling the fetch doesn't trigger stale
while revalidate loading.

Add test to ensure that scripts and css loaded trigger a stale
while revalidate cache hit.

The PR for the spec changes is here:
https://github.com/whatwg/fetch/pull/853

This reland removes the resource timing and moves to css (instead of
images) which were both unreliable.

BUG=348877

Change-Id: Ibabd8d3fd0295bedc8259594fc926da6ab5cfc43
Reviewed-on: https://chromium-review.googlesource.com/c/1434776
Commit-Queue: Dave Tapuska <dtapuska@chromium.org>
Reviewed-by: default avatarBen Kelly <wanderview@chromium.org>
Cr-Commit-Position: refs/heads/master@{#626264}
parent 5fda3d5d
<!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>
def main(request, response):
cookie = request.cookies.first("Count", None)
count = 0
if cookie != None:
count = int(cookie.value)
if request.GET.first("query", None) != None:
headers = [("Count", count)]
content = ""
return 200, headers, content
else:
count = count + 1
content = "body { background: rgb(0, 128, 0); }"
if count > 1:
content = "body { background: rgb(255, 0, 0); }"
headers = [("Content-Type", "text/css"),
("Set-Cookie", "Count={}".format(count)),
("Cache-Control", "private, max-age=0, stale-while-revalidate=10")]
return 200, headers, content
<!DOCTYPE html>
<!---
Tentative test against:
https://github.com/whatwg/fetch/pull/853
-->
<meta charset="utf-8">
<title>Tests Stale While Revalidate works for css</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(window.getComputedStyle(document.body).getPropertyValue('background-color'), "rgb(0, 128, 0)");
var link2 = document.createElement("link");
link2.onload = t.step_func(() => {
assert_equals(window.getComputedStyle(document.body).getPropertyValue('background-color'), "rgb(0, 128, 0)");
var checkResult = () => {
// We poll because we don't know when the revalidation will occur.
fetch("stale-css.py?query").then(t.step_func((response) => {
var count = response.headers.get("Count");
if (count == '2') {
t.done();
} else {
t.step_timeout(checkResult, 25);
}
}));
};
t.step_timeout(checkResult, 25);
});
link2.rel = "stylesheet";
link2.type = "text/css";
link2.href = "stale-css.py";
document.body.appendChild(link2);
}, 0);
});
}, 'Cache returns stale resource');
var link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = "stale-css.py";
document.body.appendChild(link);
</script>
</body>
import os.path
def main(request, response):
cookie = request.cookies.first("Count", None)
count = 0
if cookie != None:
count = int(cookie.value)
if request.GET.first("query", None) != None:
headers = [("Count", count)]
content = ""
return 200, headers, content
else:
count = count + 1
filename = "green-16x16.png"
if cookie > 1:
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", "Count={}".format(count))
response.writer.end_headers()
response.writer.write(body)
<!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>
<!--
Use a child document to load the second stale image into because
an image loaded into the same document will skip cache-control headers.
See: https://html.spec.whatwg.org/#the-list-of-available-images
-->
<iframe id="child" srcdoc=""></iframe>
<script>
async_test(t => {
window.onload = t.step_func(() => {
step_timeout(() => {
assert_equals(document.getElementById("firstimage").width, 16, "Width is 16");
var childDocument = document.getElementById('child').contentDocument;
var img2 = childDocument.createElement("img");
img2.onload = t.step_func(() => {
assert_equals(img2.width, 16, "image dimension");
var checkResult = () => {
// We poll because we don't know when the revalidation will occur.
fetch("stale-image.py?query").then(t.step_func((response) => {
var count = response.headers.get("Count");
if (count == '2') {
t.done();
} else {
t.step_timeout(checkResult, 25);
}
}));
};
t.step_timeout(checkResult, 25);
});
img2.src = "stale-image.py";
childDocument.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>
import random, string, datetime
def token():
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(20))
def main(request, response):
cookie = request.cookies.first("Count", None)
count = 0
if cookie != None:
count = int(cookie.value)
if request.GET.first("query", None) != None:
headers = [("Count", count)]
content = ""
return 200, headers, content
else:
count = count + 1
unique_id = token()
headers = [("Content-Type", "text/javascript"),
("Cache-Control", "private, max-age=0, stale-while-revalidate=10"),
("Set-Cookie", "Count={}".format(count)),
("Token", unique_id)]
content = "report('{}')".format(unique_id)
return 200, headers, content
<!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(() => {
assert_equals(last_modified_count, 2, "last modified");
var checkResult = () => {
// We poll because we don't know when the revalidation will occur.
fetch("stale-script.py?query").then(t.step_func((response) => {
var count = response.headers.get("Count");
if (count == '2') {
t.done();
} else {
t.step_timeout(checkResult, 25);
}
}));
};
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>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment