summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/resource-timing/resources/fake_responses.py
blob: 66b17be154b8c71bca1378aba44635eaada46b21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# /xhr/resources/conditional.py -- to fake a 304 response

def main(request, response):
    if request.method == "OPTIONS":
        # Assume this is a CORS preflight
        response.headers.set(b"Access-Control-Allow-Headers", "*")
        response.headers.set(b"Access-Control-Allow-Origin", "*")
        response.status = (204, "No Content")
        return b""
    tag = request.GET.first(b"tag", None)
    redirect = request.GET.first(b"redirect", None)
    match = request.headers.get(b"If-None-Match", None)
    date = request.GET.first(b"date", b"")
    modified = request.headers.get(b"If-Modified-Since", None)
    url = request.GET.first(b"url", None)
    response.headers.set(b"Access-Control-Allow-Origin", b"*");
    response.headers.set(b"Timing-Allow-Origin", b"*");
    if tag:
        response.headers.set(b"ETag", b'"%s"' % tag)
    elif date:
        response.headers.set(b"Last-Modified", date)
    if redirect:
        response.headers.set(b"Location", redirect)
        response.status = (302, b"Moved")
        return b""

    if url:
        filename = url.decode('utf-8').split("?")[0]
        filepath = "./resource-timing/resources/{}".format(filename)
        response.headers.set(b"Content-Type", b"text/javascript")
        with open(filepath, 'rb') as f:
            filedata = f.read()
        return filedata

    if ((match is not None and match == tag) or
        (modified is not None and modified == date)):
        response.status = (304, b"SUPERCOOL")
        return b""
    else:
        response.headers.set(b"Content-Type", b"text/plain")
        return b"MAYBE NOT"