testing/web-platform/tests/fetch/stale-while-revalidate/stale-image.py
author Dave Tapuska <dtapuska@chromium.org>
Tue, 05 Mar 2019 12:16:11 +0000
changeset 525454 1774d9a9e839869b7995207f543bb5f6a6fe8a1b
parent 525311 66b1aba3fb4372ec47986e728b1da9b32dd3d368
child 529156 374a0fc6fe9d457d7c99a635b3cf0ce70eb5b284
permissions -rw-r--r--
Bug 1528211 [wpt PR 15362] - Fix flakiness with stale-while-revalidate tests., a=testonly Automatic update from web-platform-tests Fix flakiness with stale-while-revalidate tests. (#15362) Using cookies didn't seem reliable in the wpt test runner when it was re-run. Stop using cookies and use the server side stash instead like the rest of the fetch tests do. Increase timeout on staleness. -- wpt-commits: 2801597ed275a33f31b8e54081a7e891eda0ff07 wpt-pr: 15362

import os.path

def main(request, response):

    token = request.GET.first("token", None)
    value = request.server.stash.take(token)
    count = 0
    if value != None:
      count = int(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 count > 1:
        filename = "green-256x256.png"

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

      request.server.stash.put(token, count)
      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=60")
      response.writer.write_header("content-type", "image/png")
      response.writer.end_headers()

      response.writer.write(body)