aboutsummaryrefslogtreecommitdiffstats
path: root/tests/middleware/test_if_none_match.py
blob: 8456c05162591362cd27db8e3f9f3e475a004044 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from wsgiref.validate import validator

import pytest

from paste import if_none_match

from ..common_wsgi import call_app

ETAG = "1234"


@pytest.fixture
def app():
    @validator
    @if_none_match
    @validator
    def app(environ, start_response):
        body = b"Hello, world!"
        etag_header = ("ETag", f'"{ETAG}"')
        if environ["REQUEST_METHOD"] == "HEAD":
            start_response(
                "200 OK",
                [
                    ("Content-Type", "text/plain"),
                    ("Content-Length", str(len(body))),
                    etag_header,
                ],
            )
            return []
        start_response("200 OK", [("Content-Type", "text/plain"), etag_header])
        return [body]

    return app


@pytest.mark.parametrize("method", ["POST", "PUT", "DELETE"])
def test_non_get_or_head_request_passthrough(app, method):
    environ = {"REQUEST_METHOD": method, "HTTP_IF_NONE_MATCH": f'"{ETAG}"'}
    response = call_app(app, environ)
    assert response.data == b"Hello, world!"
    assert response.status == "200 OK"
    assert ("Content-Type", "text/plain") in response.headers


def test_if_none_match_header_not_set(app):
    environ = {"REQUEST_METHOD": "GET"}
    response = call_app(app, environ)
    assert response.data == b"Hello, world!"
    assert response.status == "200 OK"
    assert ("Content-Type", "text/plain") in response.headers


@pytest.mark.parametrize("value", [f'"{ETAG}"', f'W/"{ETAG}"'])
def test_etag_matches_if_none_match_header(app, value):
    environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": value}
    response = call_app(app, environ)
    assert response.data == b""
    assert response.status == "304 Not Modified"


@pytest.mark.parametrize(
    "value",
    [
        f'"{ETAG}", "54321"',
        f'"54321", "{ETAG}", "098765"',
        f'"54321", "{ETAG}"',
        f'W/"{ETAG}", "54321"',
        f'"54321", W/"{ETAG}", "098765"',
        f'"54321", W/"{ETAG}"',
    ],
)
def test_etag_matches_if_none_match_header_list(app, value):
    environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": value}
    response = call_app(app, environ)
    assert response.data == b""
    assert response.status == "304 Not Modified"


def test_etag_matches_if_none_match_header_list2(app):
    environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": f'"54321", "{ETAG}"'}
    response = call_app(app, environ)
    assert response.data == b""
    assert response.status == "304 Not Modified"


def test_etag_matches_if_none_match_header_star(app):
    environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": "*"}
    response = call_app(app, environ)
    assert response.data == b""
    assert response.status == "304 Not Modified"


def test_missing_etag_does_not_match_if_none_match_header_star():
    @validator
    @if_none_match
    @validator
    def app(environ, start_response):
        body = b"Hello, world!"
        if environ["REQUEST_METHOD"] == "HEAD":
            start_response(
                "200 OK",
                [
                    ("Content-Type", "text/plain"),
                    ("Content-Length", str(len(body))),
                ],
            )
            return []
        start_response("200 OK", [("Content-Type", "text/plain")])
        return [body]

    environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": "*"}
    response = call_app(app, environ)
    assert response.data == b"Hello, world!"
    assert response.status == "200 OK"


def test_etag_does_not_match_if_none_match_header(app):
    environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": '"54321"'}
    response = call_app(app, environ)
    assert response.data == b"Hello, world!"
    assert response.status == "200 OK"
    assert ("Content-Type", "text/plain") in response.headers


def test_etag_does_not_match_if_none_match_header_list(app):
    environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": '"123","4567","*"'}
    response = call_app(app, environ)
    assert response.data == b"Hello, world!"
    assert response.status == "200 OK"
    assert ("Content-Type", "text/plain") in response.headers


def test_etag_does_not_match_if_none_match_malformed_header(app):
    environ = {"REQUEST_METHOD": "GET", "HTTP_IF_NONE_MATCH": "{ETAG}"}
    response = call_app(app, environ)
    assert response.data == b"400 Bad Request\n"
    assert response.status == "400 Bad Request"
    assert ("Content-Type", "text/plain") in response.headers