testing/web-platform/tests/fetch/api/request/resources/cache.py
author Ehsan Akhgari <ehsan@mozilla.com>
Tue, 01 Mar 2016 17:34:31 -0500
changeset 288104 c74866f52320fd314ae8c098d6eaae880c856e96
parent 287554 9391f22c08c23203e239165916a0715c52cf2df8
child 287555 f87a25ade31e5bcae9f2a78a91fc8ece50db60bd
permissions -rw-r--r--
Bug 1120715 - Part 4: Add tests for Request.cache; r=bkelly

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)
    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)

    server_state = request.server.stash.take(token)
    if not server_state:
        server_state = []
    state = dict()
    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)

    if ((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