Skip to content
This repository was archived by the owner on Sep 24, 2020. It is now read-only.

Commit c57f992

Browse files
committedMay 27, 2020
Improve error when redirecting with custom schemes
Fixes encode#822
1 parent 21d7e16 commit c57f992

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed
 

‎httpx/_client.py

+4
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ def redirect_url(self, request: Request, response: Response) -> URL:
322322

323323
url = URL(location, allow_relative=True)
324324

325+
# Check that we can handle the scheme
326+
if url.scheme and url.scheme not in ("http", "https"):
327+
raise InvalidURL(f'Scheme "{url.scheme}" not supported.')
328+
325329
# Handle malformed 'Location' headers that are "absolute" form, have no host.
326330
# See: https://github.com/encode/httpx/issues/771
327331
if url.scheme and not url.host:

‎tests/client/test_redirects.py

+20
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from httpx import (
99
URL,
1010
AsyncClient,
11+
InvalidURL,
1112
NotRedirectResponse,
1213
RequestBodyUnavailable,
1314
TooManyRedirects,
@@ -140,6 +141,17 @@ async def body():
140141
else:
141142
return b"HTTP/1.1", 200, b"OK", [], ByteStream(b"Hello, world!")
142143

144+
elif path == b"/redirect_custom_scheme":
145+
status_code = codes.MOVED_PERMANENTLY
146+
headers = [(b"location", b"market://details?id=42")]
147+
return (
148+
b"HTTP/1.1",
149+
status_code,
150+
b"Moved Permanently",
151+
headers,
152+
ByteStream(b""),
153+
)
154+
143155
return b"HTTP/1.1", 200, b"OK", [], ByteStream(b"Hello, world!")
144156

145157

@@ -431,3 +443,11 @@ async def test_redirect_cookie_behavior():
431443
response = await client.get("https://example.com/")
432444
assert response.url == "https://example.com/"
433445
assert response.text == "Not logged in"
446+
447+
448+
@pytest.mark.usefixtures("async_environment")
449+
async def test_redirect_custom_scheme():
450+
client = AsyncClient(dispatch=MockDispatch())
451+
with pytest.raises(InvalidURL) as e:
452+
await client.post("https://example.org/redirect_custom_scheme")
453+
assert str(e.value) == 'Scheme "market" not supported.'

0 commit comments

Comments
 (0)