-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
173 lines (121 loc) · 3.67 KB
/
Copy pathtest.py
File metadata and controls
173 lines (121 loc) · 3.67 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
from contextvars import ContextVar
import uuid
import pytest
import requests
BASE_URL = 'http://localhost:8080'
cache_key = ContextVar('cache_key')
@pytest.fixture(autouse=True)
def nginx_cache_clean():
value = str(uuid.uuid4())
token = cache_key.set(value)
yield
cache_key.reset(token)
def _get(path, params=None, *, cookies=None):
headers = {
'X-Cache-Key': cache_key.get(),
}
return requests.get(
f'{BASE_URL}{path}', params=params, cookies=cookies, headers=headers)
def assert_miss(response):
assert response.headers['X-Cache-Status'] in {'MISS', 'EXPIRED'}
def assert_hit(response):
assert response.headers['X-Cache-Status'] == 'HIT'
def test_nginx_no_cache_header():
path = '/response-headers'
response = _get(path)
response = _get(path)
assert 'Cache-Control' not in response.headers
assert_miss(response)
def test_nginx_respects_cache_control_max_age():
path = '/response-headers'
params = {
'Cache-Control': 'max-age=10',
}
response = _get(path, params)
assert_miss(response)
response = _get(path, params)
assert_hit(response)
def test_nginx_respects_cache_control_s_maxage():
path = '/response-headers'
params = {
'Cache-Control': 's-maxage=10',
}
response = _get(path, params)
assert_miss(response)
response = _get(path, params)
assert_hit(response)
def test_nginx_respects_cache_control_prefer_s_maxage():
path = '/response-headers'
params = {
'Cache-Control': 'max-age=0 s-maxage=10',
}
response = _get(path, params)
assert_miss(response)
response = _get(path, params)
assert_hit(response)
def test_nginx_respects_cache_control_prefer_s_maxage_zero():
path = '/response-headers'
params = {
'Cache-Control': 'max-age=10 s-maxage=0',
}
response = _get(path, params)
assert_miss(response)
response = _get(path, params)
assert_miss(response)
def test_nginx_proxy_cache_valid():
path = '/proxy-cache-valid/status/200'
response = _get(path)
assert_miss(response)
response = _get(path)
assert_hit(response)
def test_nginx_proxy_cache_valid_ignored_when_max_age_is_defined():
path = '/proxy-cache-valid/response-headers'
params = {
'Cache-Control': 'max-age=0',
}
response = _get(path, params)
assert_miss(response)
response = _get(path, params)
assert_miss(response)
def test_nginx_proxy_cache_valid_ignored_when_s_maxage_is_defined():
path = '/proxy-cache-valid/response-headers'
params = {
'Cache-Control': 's-maxage=0',
}
response = _get(path, params)
assert_miss(response)
response = _get(path, params)
assert_miss(response)
def test_nginx_cache_with_cookies_in_request():
path = '/response-headers'
params = {
'Cache-Control': 'max-age=10',
}
cookies = {
'foo': 'bar',
}
response = _get(path, params, cookies=cookies)
assert_miss(response)
response = _get(path, params, cookies=cookies)
assert_hit(response)
def test_nginx_never_cache_with_set_cookie_in_response():
path = '/proxy-cache-valid/cookies/set'
params = {
'Cache-Control': 'max-age=10',
}
cookies = {
'foo': 'bar',
}
response = _get(path, params, cookies=cookies)
assert_miss(response)
response = _get(path, params, cookies=cookies)
assert_miss(response)
def test_nginx_cache_control_private_never_cache():
path = '/response-headers'
params = {
'Cache-Control': 'private, max-age=10',
}
response = _get(path, params)
assert_miss(response)
response = _get(path, params)
assert_miss(response)