Commit 6c66f4c2 authored by arthursonzogni's avatar arthursonzogni Committed by Commit Bot

Rework WPT: /fetch/stale-while-revalidate/stale-script.html

Mainly for improving my own understanding. I would like to make some
tests about CSP vs stale-while-revalidate for issue 1070117

This patch:
1) Use Promise(s) instead of callback(s). This unwrap the logic flow.

2) Make "last_modified_count" to count the number of new version being
   served. Previously, it was a bit confusing to me.

3) Extend the test to check the "revalidated" version is served on the
   3rd load.

4) Small misc refactorings.

Bug: 1070117
Change-Id: Ifba23d78e539eb50262244f59e7c16e1b9ca56ab
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2154786Reviewed-by: default avatarDave Tapuska <dtapuska@chromium.org>
Commit-Queue: Arthur Sonzogni <arthursonzogni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#760550}
parent 29382fcf
...@@ -6,48 +6,54 @@ ...@@ -6,48 +6,54 @@
<script src="/common/utils.js"></script> <script src="/common/utils.js"></script>
<body> <body>
<script> <script>
var last_modified;
var last_modified_count = 0;
var request_token = token();
// The script will call report via a uniquely generated ID on the subresource. const request_token = token();
// If it is a cache hit the ID will be the same and the test will pass. const script_src = "./resources/stale-script.py?token=" + request_token;
// The script above will call report() via a uniquely generated ID on the
// subresource. If it is a cache hit, the ID will be the same and
// |last_modified_count| won't be incremented.
let last_modified;
let last_modified_count = 0;
function report(mod) { function report(mod) {
if (!last_modified) { if (last_modified == mod)
return;
last_modified = mod; last_modified = mod;
last_modified_count = 1;
} else if (last_modified == mod) {
last_modified_count++; last_modified_count++;
}
} }
async_test(t => { let loadScript = async () => {
window.onload = t.step_func(() => { let script = document.createElement("script");
step_timeout(() => { let script_loaded = new Promise(r => script.onload = r);
var script = document.createElement("script"); script.src = script_src;
script.src = "resources/stale-script.py?token=" + request_token;
document.body.appendChild(script); document.body.appendChild(script);
script.onload = t.step_func(() => { await script_loaded;
assert_equals(last_modified_count, 2, "last modified"); };
var checkResult = () => {
// We poll because we don't know when the revalidation will occur. promise_test(async t => {
fetch("resources/stale-script.py?query&token=" + request_token).then(t.step_func((response) => { await new Promise(r => window.onload = r);
var count = response.headers.get("Count");
if (count == '2') { await loadScript();
t.done(); assert_equals(last_modified_count, 1, '(initial version loaded)');
} else {
t.step_timeout(checkResult, 25); await loadScript();
assert_equals(last_modified_count, 1, '(stale version loaded)');
// Query the server again and again. At some point it must have received the
// revalidation request. We poll, because we don't know when the revalidation
// will occur.
while(true) {
await new Promise(r => step_timeout(r, 25));
let response = await fetch(script_src + "&query");
let count = response.headers.get("Count");
if (count == '2')
break;
} }
}));
}; await loadScript();
t.step_timeout(checkResult, 25); assert_equals(last_modified_count, 2, '(revalidated version loaded)');
});
}, 0);
});
}, 'Cache returns stale resource'); }, 'Cache returns stale resource');
var script = document.createElement("script");
script.src = "resources/stale-script.py?token=" + request_token;
document.body.appendChild(script);
</script> </script>
</body> </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