Files
firefox/testing/web-platform/tests/fetch/api/request/resources/cache.py
T
Andrew Sutherland 1a4f099a52 Bug 1272436 - P1 Fetch "only-if-cached" cache mode tests. r=bkelly
request-error.html:
- Add test verifying we enforce only-if-cached requires same-origin mode.

request-cache.html (only-if-cached does not hit network):
- Add comment block explaining the test dictionary arguments.
- Eliminate duplicated code in populate_cache which seems to have existed only
  for historical reasons and is now moot.
- Add `response` optional test dictionary array for cases where a network error
  is expected as the result of the fetch.  Normalize server results to handle
  this case since test cases may no longer talk to the server at all.
- Add only-if-cached tests that verify the network is not touched in any of
  the fresh cached, stale cached, and not cached cases.

request-cache.html (cross-origin redirects in the cache are not traversed):
- Tests added to request/request-cache.html instead of under redirect/ because
  redirect/ and redirect.py explicitly disable caching.  Also, the issue is
  about cache behavior, not the redirect-following logic.
- Enhance cache.py to accept a "redirect" parameter that generates a 302
  redirect using a Location header when received.
- Make the "error" handling generate a test failure if we don't fail when
  expected.
- Add "redirect" argument to request-cache.html test info dicts to tell
  cache.py to generate a redirect.
- Add test cases for "only-if-cached" to first prime with redirect, expecting
  success on same-origin redirects and failure on cross-origin redirects.
2016-06-08 12:10:21 -04:00

63 lines
2.2 KiB
Python

def main(request, response):
token = request.GET.first("token", None)
if "querystate" in request.GET:
from json import JSONEncoder
response.headers.set("Content-Type", "text/plain")
return JSONEncoder().encode(request.server.stash.take(token))
content = request.GET.first("content", None)
tag = request.GET.first("tag", None)
date = request.GET.first("date", None)
expires = request.GET.first("expires", None)
vary = request.GET.first("vary", None)
cc = request.GET.first("cache_control", None)
redirect = request.GET.first("redirect", None)
inm = request.headers.get("If-None-Match", None)
ims = request.headers.get("If-Modified-Since", None)
pragma = request.headers.get("Pragma", None)
cache_control = request.headers.get("Cache-Control", None)
ignore = "ignore" in request.GET
server_state = request.server.stash.take(token)
if not server_state:
server_state = []
state = dict()
if not ignore:
if inm:
state["If-None-Match"] = inm
if ims:
state["If-Modified-Since"] = ims
if pragma:
state["Pragma"] = pragma
if cache_control:
state["Cache-Control"] = cache_control
server_state.append(state)
request.server.stash.put(token, server_state)
if tag:
response.headers.set("ETag", '"%s"' % tag)
elif date:
response.headers.set("Last-Modified", date)
if expires:
response.headers.set("Expires", expires)
if vary:
response.headers.set("Vary", vary)
if cc:
response.headers.set("Cache-Control", cc)
# The only-if-cached redirect tests wants CORS to be okay, the other tests
# are all same-origin anyways and don't care.
response.headers.set("Access-Control-Allow-Origin", "*");
if redirect:
response.headers.set("Location", redirect);
response.status = (302, "Redirect")
return ""
elif ((inm is not None and inm == tag) or
(ims is not None and ims == date)):
response.status = (304, "Not Modified")
return ""
else:
response.status = (200, "OK")
response.headers.set("Content-Type", "text/plain")
return content