File: test_chunked_response.py

package info (click to toggle)
python-cachecontrol 0.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 376 kB
  • sloc: python: 2,026; makefile: 167; sh: 8
file content (56 lines) | stat: -rw-r--r-- 1,457 bytes parent folder | download
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
# 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):
        sess.get(url + "stream", stream=True)
        resp = sess.get(url + "stream", stream=True)

        assert not resp.from_cache