diff options
author | shiftinv <me@shiftinv.cc> | 2021-04-18 21:13:10 +0200 |
---|---|---|
committer | shiftinv <me@shiftinv.cc> | 2021-04-18 21:13:10 +0200 |
commit | df8d1d1ec87d1a02d1733b065d048050b0700f5d (patch) | |
tree | 87143d22d8b9654e5ed35ade8d481ec28304887b | |
parent | e3deb4b2d7a388550d004d0109fac805dd91db41 (diff) | |
download | requests-cache-df8d1d1ec87d1a02d1733b065d048050b0700f5d.tar.gz |
Read manually instead of using _body, move decode tests to integration tests
-rwxr-xr-x | requests_cache/response.py | 10 | ||||
-rw-r--r-- | tests/integration/test_cache.py | 21 | ||||
-rw-r--r-- | tests/unit/test_response.py | 22 |
3 files changed, 27 insertions, 26 deletions
diff --git a/requests_cache/response.py b/requests_cache/response.py index a9d733b..0f2edaf 100755 --- a/requests_cache/response.py +++ b/requests_cache/response.py @@ -52,16 +52,16 @@ class CachedResponse(Response): # Read content to support streaming requests, and reset file pointer on original request if hasattr(original_response.raw, '_fp') and not original_response.raw.isclosed(): - # Cache raw data in `_body` - original_response.raw.read(decode_content=False, cache_content=True) + # Cache raw data + raw_data = original_response.raw.read(decode_content=False) # Reset `_fp` - original_response.raw._fp = BytesIO(original_response.raw._body) + original_response.raw._fp = BytesIO(raw_data) # Read and store (decoded) data self._content = original_response.content # Reset `_fp` again - original_response.raw._fp = BytesIO(original_response.raw._body) + original_response.raw._fp = BytesIO(raw_data) original_response.raw._fp_bytes_read = 0 - original_response.raw.length_remaining = len(original_response.raw._body) + original_response.raw.length_remaining = len(raw_data) else: self._content = original_response.content diff --git a/tests/integration/test_cache.py b/tests/integration/test_cache.py index 18574bd..4b2fb33 100644 --- a/tests/integration/test_cache.py +++ b/tests/integration/test_cache.py @@ -2,6 +2,7 @@ import json import pytest +from requests_cache import CachedResponse from tests.conftest import USE_PYTEST_HTTPBIN, httpbin HTTPBIN_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'] @@ -73,3 +74,23 @@ def test_cookies(tempfile_session): with tempfile_session.cache_disabled(): response_3 = get_json(httpbin('cookies/set/test3/test4')) assert response_3 == get_json(httpbin('cookies')) + + +def test_response_decode(tempfile_session): + """Test that a gzip-compressed raw response can be manually uncompressed with decode_content""" + response = tempfile_session.get(httpbin('gzip')) + assert b'gzipped' in response.content + + cached = CachedResponse(response) + assert b'gzipped' in cached.content + assert b'gzipped' in cached.raw.read(None, decode_content=True) + + +def test_response_decode_stream(tempfile_session): + """Test that streamed gzip-compressed responses can be uncompressed with decode_content""" + response_uncached = tempfile_session.get(httpbin('gzip'), stream=True) + response_cached = tempfile_session.get(httpbin('gzip'), stream=True) + + for res in (response_uncached, response_cached): + assert b'gzipped' in res.content + assert b'gzipped' in res.raw.read(None, decode_content=True) diff --git a/tests/unit/test_response.py b/tests/unit/test_response.py index f92391e..cc0d16b 100644 --- a/tests/unit/test_response.py +++ b/tests/unit/test_response.py @@ -6,7 +6,7 @@ from time import sleep from urllib3.response import HTTPResponse from requests_cache import CachedHTTPResponse, CachedResponse -from tests.conftest import MOCKED_URL, httpbin +from tests.conftest import MOCKED_URL def test_basic_attrs(mock_session): @@ -71,26 +71,6 @@ def test_raw_response__reset(mock_session): assert response.raw.read(None) == b'mock response' -def test_raw_response__decode(tempfile_session): - """Test that a gzip-compressed raw response can be manually uncompressed with decode_content""" - response = tempfile_session.get(httpbin('gzip')) - assert b'gzipped' in response.content - - cached = CachedResponse(response) - assert b'gzipped' in cached.content - assert b'gzipped' in cached.raw.read(None, decode_content=True) - - -def test_raw_response__decode_stream(tempfile_session): - """Test that streamed gzip-compressed responses can be uncompressed with decode_content""" - response_uncached = tempfile_session.get(httpbin('gzip'), stream=True) - response_cached = tempfile_session.get(httpbin('gzip'), stream=True) - - for res in (response_uncached, response_cached): - assert b'gzipped' in res.content - assert b'gzipped' in res.raw.read(None, decode_content=True) - - def test_raw_response__stream(mock_session): response = CachedResponse(mock_session.get(MOCKED_URL)) data = b'' |