From f7bc9dc47131dc5b11f74ebc4c3745daf79dbe9d Mon Sep 17 00:00:00 2001 From: Scot Loach Date: Wed, 3 Nov 2021 22:29:36 -0400 Subject: [PATCH] Support the allow_redirects option of aiohttp when playing responses --- vcr/stubs/aiohttp_stubs.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/vcr/stubs/aiohttp_stubs.py b/vcr/stubs/aiohttp_stubs.py index ca82d75e..5bc13533 100644 --- a/vcr/stubs/aiohttp_stubs.py +++ b/vcr/stubs/aiohttp_stubs.py @@ -116,7 +116,7 @@ def _deserialize_headers(headers): return CIMultiDictProxy(deserialized_headers) -def play_responses(cassette, vcr_request): +def play_responses(cassette, vcr_request, allow_redirects): history = [] vcr_response = cassette.play_response(vcr_request) response = build_response(vcr_request, vcr_response, history) @@ -124,7 +124,7 @@ def play_responses(cassette, vcr_request): # If we're following redirects, continue playing until we reach # our final destination. while 300 <= response.status <= 399: - if "location" not in response.headers: + if "location" not in response.headers or allow_redirects is False: break next_url = URL(response.url).join(URL(response.headers["location"])) @@ -237,6 +237,7 @@ async def new_request(self, method, url, **kwargs): data = kwargs.get("data", kwargs.get("json")) params = kwargs.get("params") cookies = kwargs.get("cookies") + allow_redirects = kwargs.get("allow_redirects") if auth is not None: headers["AUTHORIZATION"] = auth.encode() @@ -256,7 +257,7 @@ async def new_request(self, method, url, **kwargs): if cassette.can_play_response_for(vcr_request): log.info("Playing response for {} from cassette".format(vcr_request)) - response = play_responses(cassette, vcr_request) + response = play_responses(cassette, vcr_request, allow_redirects) for redirect in response.history: self._cookie_jar.update_cookies(redirect.cookies, redirect.url) self._cookie_jar.update_cookies(response.cookies, response.url)