diff options
author | Jordan Cook <jordan.cook@pioneer.com> | 2021-03-30 19:00:06 -0500 |
---|---|---|
committer | Jordan Cook <jordan.cook@pioneer.com> | 2021-03-31 21:30:35 -0500 |
commit | 7435cb64814809d072dceec3404b2f304d89a0a7 (patch) | |
tree | 4edc1a104ea6c5fd834afaf92d6ef10f616b8409 /tests/conftest.py | |
parent | 76a17d0fb4e3ea5a61d44ec933ed726bf4b0418b (diff) | |
download | requests-cache-7435cb64814809d072dceec3404b2f304d89a0a7.tar.gz |
Add mocking for requests with redirect history
Diffstat (limited to 'tests/conftest.py')
-rw-r--r-- | tests/conftest.py | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/tests/conftest.py b/tests/conftest.py index bb446ef..5f5ccd1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,21 +5,29 @@ Note: The protocol ``http(s)+mock://`` helps :py:class:`requests_mock.Adapter` p https://requests-mock.readthedocs.io/en/latest/adapter.html """ import pytest +from os import getenv from tempfile import NamedTemporaryFile from requests_mock import ANY as ANY_METHOD from requests_mock import Adapter -from requests_cache import ALL_METHODS, CachedSession +from requests_cache.core import ALL_METHODS, CachedSession MOCKED_URL = 'http+mock://requests-cache.com/text' MOCKED_URL_GZIP = 'https+mock://requests-cache.com/gzip' # TODO MOCKED_URL_HTTPS = 'https+mock://requests-cache.com/text' MOCKED_URL_JSON = 'http+mock://requests-cache.com/json' -MOCKED_URL_REDIRECT = 'http+mock://requests-cache.com/redirect' # TODO +MOCKED_URL_REDIRECT = 'http+mock://requests-cache.com/redirect' +MOCKED_URL_REDIRECT_TARGET = 'http+mock://requests-cache.com/redirect_target' MOCK_PROTOCOLS = ['mock://', 'http+mock://', 'https+mock://'] +def httpbin(path): + """Get the url for either a local or remote httpbin instance""" + base_url = getenv('HTTPBIN_URL', 'http://localhost:80/') + return base_url + path + + @pytest.fixture(scope='function') def mock_session() -> CachedSession: """Fixture for combining requests-cache with requests-mock. This will behave the same as a @@ -71,4 +79,18 @@ def get_mock_adapter() -> Adapter: json={'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', + status_code=302, + ) + adapter.register_uri( + ANY_METHOD, + MOCKED_URL_REDIRECT_TARGET, + headers={'Content-Type': 'text/plain'}, + text='mock redirected response', + status_code=200, + ) return adapter |