Skip to content

Redirect resolved even though allow_redirects is set to False causing exception for unsupported connection adapter #4362

@fevertree

Description

@fevertree

I'm currently mocking an API that is consumed by an Android application.
One of the API responses contains a redirect to a custom URL schema app:// in the Location header

Since I am not interested in my mocked API client in following the redirect (which would raise an InvalidSchema exception anyway), I have set allow_redirects = False on my session object. Although the redirect is not fired, it is still being resolved, causing an exception due to the missing url adapter.

Expected Result

Since I'm not interested in the redirects being followed, it would make sense to not resolve the redirected request in the first place.

How to reproduce

import requests S = requests.session() S.allow_redirects = False destination = https://somesite.example # URL with a redirect to a non-standard protocol S.get(url=destination)

Response headers (example):

HTTP/1.1 302 Found Server: Apache-Coyote Cache-Control: no-cache, no-store, max-age=0, must-revalidate Pragma: no-cache Expires: 0 Strict-Transport-Security: max-age=31536000 ; includeSubDomains Location: app://api-test.somesite.example?q=example Content-Language: en Content-Length: 0 Date: Tue, 31 Oct 2017 12:39:17 GMT Connection: close 

Exception thrown by code:

Traceback (most recent call last): File "example.py", line 6, in <module> S.get(url=destination) in get return self.request('GET', url, **kwargs) File ~/.local/lib/python2.7/site-packages/requests/sessions.py", line 518, in request resp = self.send(prep, **send_kwargs) File "~/.local/lib/python2.7/site-packages/requests/sessions.py", line 661, in send history = [resp for resp in gen] if allow_redirects else [] File "~/.local/lib/python2.7/site-packages/requests/sessions.py", line 214, in resolve_redirects **adapter_kwargs File "~/.local/lib/python2.7/site-packages/requests/sessions.py", line 633, in send adapter = self.get_adapter(url=request.url) File "~/.local/lib/python2.7/site-packages/requests/sessions.py", line 717, in get_adapter raise InvalidSchema("No connection adapters were found for '%s'" % url) requests.exceptions.InvalidSchema: No connection adapters were found for 'app://api-test.somesite.com?q=example' 

Actual Result

InvalidSchema exception

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions