Skip to content
Myr

Improve error when redirecting with custom schemes (#1002)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fixes #822

authored by Josep Cugat · committed by GitHub · parent 21d7e16 · Browse files · 2 files+24 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+24 added−0 removed

  • tests:+20 added−0 removed
  • other:+4 added−0 removed

2 files+24 added−0 removed

modified:httpx/_client.py+4 added
320 unmodified lines
321 location = response.headers["Location"]322323 url = URL(location, allow_relative=True)324325 # Check that we can handle the scheme326 if url.scheme and url.scheme not in ("http", "https"):327 raise InvalidURL(f'Scheme "{url.scheme}" not supported.')328329 # Handle malformed 'Location' headers that are "absolute" form, have no host.330 # See: https://github.com/encode/httpx/issues/771331 if url.scheme and not url.host:332 url = url.copy_with(host=request.url.host)
1205 unmodified lines
modified:tests/client/test_redirects.py+20 added
6 unmodified lines
78from httpx import (9 URL,10 AsyncClient,11 InvalidURL,12 NotRedirectResponse,13 RequestBodyUnavailable,14 TooManyRedirects,15 codes,
124 unmodified lines
140 )141 else:142 return b"HTTP/1.1", 200, b"OK", [], ByteStream(b"Hello, world!")143144 elif path == b"/redirect_custom_scheme":145 status_code = codes.MOVED_PERMANENTLY146 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 )154155 return b"HTTP/1.1", 200, b"OK", [], ByteStream(b"Hello, world!")156157158@pytest.mark.usefixtures("async_environment")
283 unmodified lines
442 # The client is not logged in.443 response = await client.get("https://example.com/")444 assert response.url == "https://example.com/"445 assert response.text == "Not logged in"446447448@pytest.mark.usefixtures("async_environment")449async 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.'