testing/web-platform/tests/fetch/stale-while-revalidate/stale-image.tentative.html
author Philip Jägenstedt <philip@foolip.org>
Tue, 05 Mar 2019 11:09:13 +0000
changeset 464498 83d693e14a497587112f99198db036e97a97031e
parent 464361 66b1aba3fb4372ec47986e728b1da9b32dd3d368
child 464559 1774d9a9e839869b7995207f543bb5f6a6fe8a1b
permissions -rw-r--r--
Bug 1527478 [wpt PR 15350] - Fetch: use `t.step_timeout()` instead of bare `step_timeout()`, a=testonly Automatic update from web-platform-tests Fetch: use `t.step_timeout()` instead of bare `step_timeout()` (#15350) This avoid a harness error in the case of failure: https://wpt.fyi/results/fetch/stale-while-revalidate?run_id=6045131320852480&run_id=4797529354928128&run_id=6524192509919232&run_id=6492651545165824 -- wpt-commits: 8686e3e2268c7746bdfe88e892cb986b0ba7b779 wpt-pr: 15350

<!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(() => {
    t.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>