-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathtest_chunked_response.py
More file actions
55 lines (42 loc) · 1.45 KB
/
test_chunked_response.py
File metadata and controls
55 lines (42 loc) · 1.45 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
# SPDX-FileCopyrightText: 2015 Eric Larson
#
# SPDX-License-Identifier: Apache-2.0
"""
Test for supporting streamed responses (Transfer-Encoding: chunked)
"""
import pytest
import requests
from cachecontrol import CacheControl
@pytest.fixture()
def sess():
sess = CacheControl(requests.Session())
yield sess
# closing session object
sess.close()
class TestChunkedResponses:
def test_cache_chunked_response(self, url, sess):
"""
Verify that an otherwise cacheable response is cached when the
response is chunked.
"""
url = url + "stream"
r = sess.get(url)
from pprint import pprint
pprint(dict(r.headers))
pprint(dict(r.request.headers))
print(r.content)
assert r.headers.get("transfer-encoding") == "chunked"
r = sess.get(url, headers={"Cache-Control": "max-age=3600"})
assert r.from_cache is True
def test_stream_is_cached(self, url, sess):
resp_1 = sess.get(url + "stream")
content_1 = resp_1.content
resp_2 = sess.get(url + "stream")
content_2 = resp_1.content
assert not resp_1.from_cache
assert resp_2.from_cache
assert content_1 == content_2
def test_stream_is_not_cached_when_content_is_not_read(self, url, sess):
with sess.get(url + "stream", stream=True):
with sess.get(url + "stream", stream=True) as resp:
assert not resp.from_cache