diff options
author | Jordan Cook <jordan.cook.git@proton.me> | 2023-05-08 12:27:21 -0500 |
---|---|---|
committer | Jordan Cook <jordan.cook.git@proton.me> | 2023-05-08 12:29:57 -0500 |
commit | 914b62502cf510a6c67b61453a67f90c86222f6e (patch) | |
tree | 9b664b13956cee79a6f018f6c763420c1359e815 /tests | |
parent | e5d54b4f930956bbfdaf21d819e269a9857fc28a (diff) | |
download | requests-cache-914b62502cf510a6c67b61453a67f90c86222f6e.tar.gz |
Fix loading cached JSON content when decode_content=True and the root element is a list
Diffstat (limited to 'tests')
-rw-r--r-- | tests/conftest.py | 8 | ||||
-rw-r--r-- | tests/integration/base_cache_test.py | 19 |
2 files changed, 25 insertions, 2 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index e4ca8a6..099ddb3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -82,6 +82,7 @@ MOCKED_URL = 'http+mock://requests-cache.com/text' MOCKED_URL_ETAG = 'http+mock://requests-cache.com/etag' MOCKED_URL_HTTPS = 'https+mock://requests-cache.com/text' MOCKED_URL_JSON = 'http+mock://requests-cache.com/json' +MOCKED_URL_JSON_LIST = 'http+mock://requests-cache.com/json_list' MOCKED_URL_REDIRECT = 'http+mock://requests-cache.com/redirect' MOCKED_URL_REDIRECT_TARGET = 'http+mock://requests-cache.com/redirect_target' MOCKED_URL_VARY = 'http+mock://requests-cache.com/vary' @@ -208,6 +209,13 @@ def get_mock_adapter() -> Adapter: ) adapter.register_uri( ANY_METHOD, + MOCKED_URL_JSON_LIST, + headers={'Content-Type': 'application/json'}, + json=['item_1', 'item_2', {'message': 'mock json response'}], + status_code=200, + ) + adapter.register_uri( + ANY_METHOD, MOCKED_URL_REDIRECT, headers={'Content-Type': 'text/plain', 'Location': MOCKED_URL_REDIRECT_TARGET}, text='mock redirect response', diff --git a/tests/integration/base_cache_test.py b/tests/integration/base_cache_test.py index 7801570..3e4d230 100644 --- a/tests/integration/base_cache_test.py +++ b/tests/integration/base_cache_test.py @@ -25,12 +25,15 @@ from tests.conftest import ( HTTPBIN_METHODS, HTTPDATE_STR, LAST_MODIFIED, + MOCKED_URL_JSON, + MOCKED_URL_JSON_LIST, N_ITERATIONS, N_REQUESTS_PER_ITERATION, N_WORKERS, USE_PYTEST_HTTPBIN, assert_delta_approx_equal, httpbin, + mount_mock_adapter, skip_pypy, ) @@ -259,9 +262,9 @@ class BaseCacheTest: assert response.is_expired is False @pytest.mark.parametrize('stream', [True, False]) - def test_response_decode(self, stream): + def test_decode_gzip_response(self, stream): """Test that gzip-compressed raw responses (including streamed responses) can be manually - decompressed with decode_content=True + decompressed with `decode_content=True` """ session = self.init_session() response = session.get(httpbin('gzip'), stream=stream) @@ -274,6 +277,18 @@ class BaseCacheTest: assert b'gzipped' in cached_response.content assert b'gzipped' in cached_response.raw.read(None, decode_content=True) + @pytest.mark.parametrize('decode_content', [True, False]) + @pytest.mark.parametrize('url', [MOCKED_URL_JSON, MOCKED_URL_JSON_LIST]) + def test_decode_json_response(self, decode_content, url): + """Test that JSON responses (with both dict and list root) are correctly returned from the + cache, regardless of `decode_content` setting""" + session = self.init_session(decode_content=decode_content) + session = mount_mock_adapter(session) + + r1 = session.get(url) + r2 = session.get(url) + assert r1.json() == r2.json() + def test_multipart_upload(self): session = self.init_session() session.post(httpbin('post'), files={'file1': BytesIO(b'10' * 1024)}) |