Skip to content

Support the allow_redirects option of aiohttp when playing responses #617

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions vcr/stubs/aiohttp_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ 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)

# 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"]))
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down