Skip to content

Commit

Permalink
support allow_redirects option
Browse files Browse the repository at this point in the history
  • Loading branch information
herdigiorgi authored and kevin1024 committed Jul 24, 2020
1 parent 8529c46 commit f387950
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 18 additions & 0 deletions tests/integration/test_httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,21 @@ def test_relative_redirects(tmpdir, scheme, do_request):
assert response.json()["url"].endswith("request")

assert cassette.play_count == 3


def test_redirect_wo_allow_redirects(do_request, yml):
url = "https://mockbin.org/redirect/308/5"

with vcr.use_cassette(yml):
response = do_request()("GET", url, allow_redirects=False)

assert str(response.url).endswith("308/5")
assert response.status_code == 308

with vcr.use_cassette(yml) as cassette:
response = do_request()("GET", url, allow_redirects=False)

assert str(response.url).endswith("308/5")
assert response.status_code == 308

assert cassette.play_count == 1
8 changes: 5 additions & 3 deletions vcr/stubs/httpx_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ def _make_vcr_request(httpx_request, **kwargs):

def _shared_vcr_send(cassette, real_send, *args, **kwargs):
real_request = args[1]

vcr_request = _make_vcr_request(real_request, **kwargs)

if cassette.can_play_response_for(vcr_request):
return vcr_request, _play_responses(cassette, real_request, vcr_request, args[0])
return vcr_request, _play_responses(cassette, real_request, vcr_request, args[0], kwargs)

if cassette.write_protected and cassette.filter_request(vcr_request):
raise CannotOverwriteExistingCassetteException(cassette=cassette, failed_request=vcr_request)
Expand All @@ -96,12 +97,13 @@ def _record_responses(cassette, vcr_request, real_response):
return real_response


def _play_responses(cassette, request, vcr_request, client):
def _play_responses(cassette, request, vcr_request, client, kwargs):
history = []
allow_redirects = kwargs.get("allow_redirects", True)
vcr_response = cassette.play_response(vcr_request)
response = _from_serialized_response(request, vcr_response)

while 300 <= response.status_code <= 399:
while allow_redirects and 300 <= response.status_code <= 399:
next_url = response.headers.get("location")
if not next_url:
break
Expand Down

0 comments on commit f387950

Please sign in to comment.