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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
import email.utils
import httplib2
import pytest
import re
import tests
import time
dummy_url = "http://127.0.0.1:1"
def test_get_only_if_cached_cache_hit():
# Test that can do a GET with cache and 'only-if-cached'
http = httplib2.Http(cache=tests.get_cache_path())
with tests.server_const_http(add_etag=True) as uri:
http.request(uri, "GET")
response, content = http.request(
uri, "GET", headers={"cache-control": "only-if-cached"}
)
assert response.fromcache
assert response.status == 200
def test_get_only_if_cached_cache_miss():
# Test that can do a GET with no cache with 'only-if-cached'
http = httplib2.Http(cache=tests.get_cache_path())
with tests.server_const_http(request_count=0) as uri:
response, content = http.request(
uri, "GET", headers={"cache-control": "only-if-cached"}
)
assert not response.fromcache
assert response.status == 504
def test_get_only_if_cached_no_cache_at_all():
# Test that can do a GET with no cache with 'only-if-cached'
# Of course, there might be an intermediary beyond us
# that responds to the 'only-if-cached', so this
# test can't really be guaranteed to pass.
http = httplib2.Http()
with tests.server_const_http(request_count=0) as uri:
response, content = http.request(
uri, "GET", headers={"cache-control": "only-if-cached"}
)
assert not response.fromcache
assert response.status == 504
@pytest.mark.skip(reason="was commented in legacy code")
def test_TODO_vary_no():
pass
# when there is no vary, a different Accept header (e.g.) should not
# impact if the cache is used
# test that the vary header is not sent
# uri = urllib.parse.urljoin(base, "vary/no-vary.asis")
# response, content = http.request(uri, 'GET', headers={'Accept': 'text/plain'})
# assert response.status == 200
# assert 'vary' not in response
#
# response, content = http.request(uri, 'GET', headers={'Accept': 'text/plain'})
# assert response.status == 200
# assert response.fromcache, "Should be from cache"
#
# response, content = http.request(uri, 'GET', headers={'Accept': 'text/html'})
# assert response.status == 200
# assert response.fromcache, "Should be from cache"
def test_vary_header_is_sent():
# Verifies RFC 2616 13.6.
# See https://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html.
http = httplib2.Http(cache=tests.get_cache_path())
response = tests.http_response_bytes(
headers={"vary": "Accept", "cache-control": "max-age=300"}, add_date=True
)
with tests.server_const_bytes(response, request_count=3) as uri:
response, content = http.request(uri, "GET", headers={"accept": "text/plain"})
assert response.status == 200
assert "vary" in response
# get the resource again, from the cache since accept header in this
# request is the same as the request
response, content = http.request(uri, "GET", headers={"Accept": "text/plain"})
assert response.status == 200
assert response.fromcache, "Should be from cache"
# get the resource again, not from cache since Accept headers does not match
response, content = http.request(uri, "GET", headers={"Accept": "text/html"})
assert response.status == 200
assert not response.fromcache, "Should not be from cache"
# get the resource again, without any Accept header, so again no match
response, content = http.request(uri, "GET")
assert response.status == 200
assert not response.fromcache, "Should not be from cache"
def test_vary_header_double():
http = httplib2.Http(cache=tests.get_cache_path())
response = tests.http_response_bytes(
headers={"vary": "Accept, Accept-Language", "cache-control": "max-age=300"},
add_date=True,
)
with tests.server_const_bytes(response, request_count=3) as uri:
response, content = http.request(
uri,
"GET",
headers={
"Accept": "text/plain",
"Accept-Language": "da, en-gb;q=0.8, en;q=0.7",
},
)
assert response.status == 200
assert "vary" in response
# we are from cache
response, content = http.request(
uri,
"GET",
headers={
"Accept": "text/plain",
"Accept-Language": "da, en-gb;q=0.8, en;q=0.7",
},
)
assert response.fromcache, "Should be from cache"
response, content = http.request(uri, "GET", headers={"Accept": "text/plain"})
assert response.status == 200
assert not response.fromcache
# get the resource again, not from cache, varied headers don't match exact
response, content = http.request(uri, "GET", headers={"Accept-Language": "da"})
assert response.status == 200
assert not response.fromcache, "Should not be from cache"
def test_vary_unused_header():
http = httplib2.Http(cache=tests.get_cache_path())
response = tests.http_response_bytes(
headers={"vary": "X-No-Such-Header", "cache-control": "max-age=300"},
add_date=True,
)
with tests.server_const_bytes(response, request_count=1) as uri:
# A header's value is not considered to vary if it's not used at all.
response, content = http.request(uri, "GET", headers={"Accept": "text/plain"})
assert response.status == 200
assert "vary" in response
# we are from cache
response, content = http.request(uri, "GET", headers={"Accept": "text/plain"})
assert response.fromcache, "Should be from cache"
def test_get_cache_control_no_cache():
# Test Cache-Control: no-cache on requests
http = httplib2.Http(cache=tests.get_cache_path())
with tests.server_const_http(
add_date=True,
add_etag=True,
headers={"cache-control": "max-age=300"},
request_count=2,
) as uri:
response, _ = http.request(uri, "GET", headers={"accept-encoding": "identity"})
assert response.status == 200
assert response["etag"] != ""
assert not response.fromcache
response, _ = http.request(uri, "GET", headers={"accept-encoding": "identity"})
assert response.status == 200
assert response.fromcache
response, _ = http.request(
uri,
"GET",
headers={"accept-encoding": "identity", "Cache-Control": "no-cache"},
)
assert response.status == 200
assert not response.fromcache
def test_get_cache_control_pragma_no_cache():
# Test Pragma: no-cache on requests
http = httplib2.Http(cache=tests.get_cache_path())
with tests.server_const_http(
add_date=True,
add_etag=True,
headers={"cache-control": "max-age=300"},
request_count=2,
) as uri:
response, _ = http.request(uri, "GET", headers={"accept-encoding": "identity"})
assert response["etag"] != ""
response, _ = http.request(uri, "GET", headers={"accept-encoding": "identity"})
assert response.status == 200
assert response.fromcache
response, _ = http.request(
uri, "GET", headers={"accept-encoding": "identity", "Pragma": "no-cache"}
)
assert response.status == 200
assert not response.fromcache
def test_get_cache_control_no_store_request():
# A no-store request means that the response should not be stored.
http = httplib2.Http(cache=tests.get_cache_path())
with tests.server_const_http(
add_date=True,
add_etag=True,
headers={"cache-control": "max-age=300"},
request_count=2,
) as uri:
response, _ = http.request(uri, "GET", headers={"Cache-Control": "no-store"})
assert response.status == 200
assert not response.fromcache
response, _ = http.request(uri, "GET", headers={"Cache-Control": "no-store"})
assert response.status == 200
assert not response.fromcache
def test_get_cache_control_no_store_response():
# A no-store response means that the response should not be stored.
http = httplib2.Http(cache=tests.get_cache_path())
with tests.server_const_http(
add_date=True,
add_etag=True,
headers={"cache-control": "max-age=300, no-store"},
request_count=2,
) as uri:
response, _ = http.request(uri, "GET")
assert response.status == 200
assert not response.fromcache
response, _ = http.request(uri, "GET")
assert response.status == 200
assert not response.fromcache
def test_get_cache_control_no_cache_no_store_request():
# Test that a no-store, no-cache clears the entry from the cache
# even if it was cached previously.
http = httplib2.Http(cache=tests.get_cache_path())
with tests.server_const_http(
add_date=True,
add_etag=True,
headers={"cache-control": "max-age=300"},
request_count=3,
) as uri:
response, _ = http.request(uri, "GET")
response, _ = http.request(uri, "GET")
assert response.fromcache
response, _ = http.request(
uri, "GET", headers={"Cache-Control": "no-store, no-cache"}
)
assert response.status == 200
assert not response.fromcache
response, _ = http.request(
uri, "GET", headers={"Cache-Control": "no-store, no-cache"}
)
assert response.status == 200
assert not response.fromcache
def test_update_invalidates_cache():
# Test that calling PUT or DELETE on a
# URI that is cache invalidates that cache.
http = httplib2.Http(cache=tests.get_cache_path())
def handler(request):
if request.method in ("PUT", "PATCH", "DELETE"):
return tests.http_response_bytes(status=405)
return tests.http_response_bytes(
add_date=True, add_etag=True, headers={"cache-control": "max-age=300"}
)
with tests.server_request(handler, request_count=3) as uri:
response, _ = http.request(uri, "GET")
response, _ = http.request(uri, "GET")
assert response.fromcache
response, _ = http.request(uri, "DELETE")
assert response.status == 405
assert not response.fromcache
response, _ = http.request(uri, "GET")
assert not response.fromcache
def handler_conditional_update(request):
respond = tests.http_response_bytes
if request.method == "GET":
if request.headers.get("if-none-match", "") == "12345":
return respond(status=304)
return respond(
add_date=True, headers={"etag": "12345", "cache-control": "max-age=300"}
)
elif request.method in ("PUT", "PATCH", "DELETE"):
if request.headers.get("if-match", "") == "12345":
return respond(status=200)
return respond(status=412)
return respond(status=405)
@pytest.mark.parametrize("method", ("PUT", "PATCH"))
def test_update_uses_cached_etag(method):
# Test that we natively support http://www.w3.org/1999/04/Editing/
http = httplib2.Http(cache=tests.get_cache_path())
with tests.server_request(handler_conditional_update, request_count=3) as uri:
response, _ = http.request(uri, "GET")
assert response.status == 200
assert not response.fromcache
response, _ = http.request(uri, "GET")
assert response.status == 200
assert response.fromcache
response, _ = http.request(uri, method, body=b"foo")
assert response.status == 200
response, _ = http.request(uri, method, body=b"foo")
assert response.status == 412
def test_update_uses_cached_etag_and_oc_method():
# Test that we natively support http://www.w3.org/1999/04/Editing/
http = httplib2.Http(cache=tests.get_cache_path())
with tests.server_request(handler_conditional_update, request_count=2) as uri:
response, _ = http.request(uri, "GET")
assert response.status == 200
assert not response.fromcache
response, _ = http.request(uri, "GET")
assert response.status == 200
assert response.fromcache
http.optimistic_concurrency_methods.append("DELETE")
response, _ = http.request(uri, "DELETE")
assert response.status == 200
def test_update_uses_cached_etag_overridden():
# Test that we natively support http://www.w3.org/1999/04/Editing/
http = httplib2.Http(cache=tests.get_cache_path())
with tests.server_request(handler_conditional_update, request_count=2) as uri:
response, content = http.request(uri, "GET")
assert response.status == 200
assert not response.fromcache
response, content = http.request(uri, "GET")
assert response.status == 200
assert response.fromcache
response, content = http.request(
uri, "PUT", body=b"foo", headers={"if-match": "fred"}
)
assert response.status == 412
@pytest.mark.parametrize(
"data",
(
({}, {}),
({"cache-control": " no-cache"}, {"no-cache": 1}),
(
{"cache-control": " no-store, max-age = 7200"},
{"no-store": 1, "max-age": "7200"},
),
({"cache-control": " , "}, {"": 1}), # FIXME
(
{"cache-control": "Max-age=3600;post-check=1800,pre-check=3600"},
{"max-age": "3600;post-check=1800", "pre-check": "3600"},
),
),
ids=lambda data: str(data[0]),
)
def test_parse_cache_control(data):
header, expected = data
assert httplib2._parse_cache_control(header) == expected
def test_normalize_headers():
# Test that we normalize headers to lowercase
h = httplib2._normalize_headers({"Cache-Control": "no-cache", "Other": "Stuff"})
assert "cache-control" in h
assert "other" in h
assert h["other"] == "Stuff"
@pytest.mark.parametrize(
"data",
(
(
{"cache-control": "no-cache"},
{"cache-control": "max-age=7200"},
"TRANSPARENT",
),
({}, {"cache-control": "max-age=fred, min-fresh=barney"}, "STALE"),
({}, {"date": "{now}", "expires": "{now+3}"}, "FRESH"),
(
{},
{"date": "{now}", "expires": "{now+3}", "cache-control": "no-cache"},
"STALE",
),
({"cache-control": "must-revalidate"}, {}, "STALE"),
({}, {"cache-control": "must-revalidate"}, "STALE"),
({}, {"date": "{now}", "cache-control": "max-age=0"}, "STALE"),
({"cache-control": "only-if-cached"}, {}, "FRESH"),
({}, {"date": "{now}", "expires": "0"}, "STALE"),
({}, {"data": "{now+3}"}, "STALE"),
(
{"cache-control": "max-age=0"},
{"date": "{now}", "cache-control": "max-age=2"},
"STALE",
),
(
{"cache-control": "min-fresh=2"},
{"date": "{now}", "expires": "{now+2}"},
"STALE",
),
(
{"cache-control": "min-fresh=2"},
{"date": "{now}", "expires": "{now+4}"},
"FRESH",
),
),
ids=lambda data: str(data),
)
def test_entry_disposition(data):
now = time.time()
nowre = re.compile(r"{now([\+\-]\d+)?}")
def render(s):
m = nowre.match(s)
if m:
offset = int(m.expand(r"\1")) if m.group(1) else 0
s = email.utils.formatdate(now + offset, usegmt=True)
return s
request, response, expected = data
request = {k: render(v) for k, v in request.items()}
response = {k: render(v) for k, v in response.items()}
assert httplib2._entry_disposition(response, request) == expected
def test_expiration_model_fresh():
response_headers = {
"date": email.utils.formatdate(usegmt=True),
"cache-control": "max-age=2",
}
assert httplib2._entry_disposition(response_headers, {}) == "FRESH"
# TODO: add current time as _entry_disposition argument to avoid sleep in tests
time.sleep(3)
assert httplib2._entry_disposition(response_headers, {}) == "STALE"
def test_expiration_model_date_and_expires():
now = time.time()
response_headers = {
"date": email.utils.formatdate(now, usegmt=True),
"expires": email.utils.formatdate(now + 2, usegmt=True),
}
assert httplib2._entry_disposition(response_headers, {}) == "FRESH"
time.sleep(3)
assert httplib2._entry_disposition(response_headers, {}) == "STALE"
# TODO: Repeat all cache tests with memcache. pytest.mark.parametrize
# cache = memcache.Client(['127.0.0.1:11211'], debug=0)
# #cache = memcache.Client(['10.0.0.4:11211'], debug=1)
# http = httplib2.Http(cache)
|