Skip to content
Myr

Move utility functions from _utils.py to _client.py (#3389)

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

authored by RafaelWO · committed by GitHub · parent b47d94c · Browse files · 4 files+92 added−100 removed
This commit

State

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

Diff stats

4 files+92 added−100 removed

  • tests:+57 added−60 removed
  • other:+35 added−40 removed

4 files+92 added−100 removed

modified:httpx/_client.py+35 added-8 removed
43 unmodified lines
44 SyncByteStream,45 TimeoutTypes,46)47from ._urls import URL, QueryParams48from ._utils import (49 URLPattern,50 get_environment_proxies,51 is_https_redirect,52 same_origin,53)48from ._utils import URLPattern, get_environment_proxies4950if typing.TYPE_CHECKING:51 import ssl # pragma: no cover52
4 unmodified lines
57T = typing.TypeVar("T", bound="Client")58U = typing.TypeVar("U", bound="AsyncClient")596061def _is_https_redirect(url: URL, location: URL) -> bool:62 """63 Return 'True' if 'location' is a HTTPS upgrade of 'url'64 """65 if url.host != location.host:66 return False6768 return (69 url.scheme == "http"70 and _port_or_default(url) == 8071 and location.scheme == "https"72 and _port_or_default(location) == 44373 )747576def _port_or_default(url: URL) -> int | None:77 if url.port is not None:78 return url.port79 return {"http": 80, "https": 443}.get(url.scheme)808182def _same_origin(url: URL, other: URL) -> bool:83 """84 Return 'True' if the given URLs share the same origin.85 """86 return (87 url.scheme == other.scheme88 and url.host == other.host89 and _port_or_default(url) == _port_or_default(other)90 )919293class UseClientDefault:94 """95 For some parameters such as `auth=...` and `timeout=...` we need to be able96 to indicate the default "unset" state, in a way that is distinctly different
450 unmodified lines
547 Return the headers that should be used for the redirect request.548 """549 headers = Headers(request.headers)550524 if not same_origin(url, request.url):525 if not is_https_redirect(request.url, url):551 if not _same_origin(url, request.url):552 if not _is_https_redirect(request.url, url):553 # Strip Authorization headers when responses are redirected554 # away from the origin. (Except for direct HTTP to HTTPS redirects.)555 headers.pop("Authorization", None)556
1438 unmodified lines
modified:httpx/_utils.py-32 removed
25 unmodified lines
26 return ""27 return str(value)282930def port_or_default(url: URL) -> int | None:31 if url.port is not None:32 return url.port33 return {"http": 80, "https": 443}.get(url.scheme)343536def same_origin(url: URL, other: URL) -> bool:37 """38 Return 'True' if the given URLs share the same origin.39 """40 return (41 url.scheme == other.scheme42 and url.host == other.host43 and port_or_default(url) == port_or_default(other)44 )454647def is_https_redirect(url: URL, location: URL) -> bool:48 """49 Return 'True' if 'location' is a HTTPS upgrade of 'url'50 """51 if url.host != location.host:52 return False5354 return (55 url.scheme == "http"56 and port_or_default(url) == 8057 and location.scheme == "https"58 and port_or_default(location) == 44359 )606130def get_environment_proxies() -> dict[str, str | None]:31 """Gets proxy information from the environment"""3233 # urllib.request.getproxies() falls back on System
209 unmodified lines
modified:tests/client/test_headers.py+56 added
233 unmodified lines
234235def test_request_auto_headers():236 request = httpx.Request("GET", "https://www.example.org/")237 assert "host" in request.headers238239240def test_same_origin():241 origin = httpx.URL("https://example.com")242 request = httpx.Request("GET", "HTTPS://EXAMPLE.COM:443")243244 client = httpx.Client()245 headers = client._redirect_headers(request, origin, "GET")246247 assert headers["Host"] == request.url.netloc.decode("ascii")248249250def test_not_same_origin():251 origin = httpx.URL("https://example.com")252 request = httpx.Request("GET", "HTTP://EXAMPLE.COM:80")253254 client = httpx.Client()255 headers = client._redirect_headers(request, origin, "GET")256257 assert headers["Host"] == origin.netloc.decode("ascii")258259260def test_is_https_redirect():261 url = httpx.URL("https://example.com")262 request = httpx.Request(263 "GET", "http://example.com", headers={"Authorization": "empty"}264 )265266 client = httpx.Client()267 headers = client._redirect_headers(request, url, "GET")268269 assert "Authorization" in headers270271272def test_is_not_https_redirect():273 url = httpx.URL("https://www.example.com")274 request = httpx.Request(275 "GET", "http://example.com", headers={"Authorization": "empty"}276 )277278 client = httpx.Client()279 headers = client._redirect_headers(request, url, "GET")280281 assert "Authorization" not in headers282283284def test_is_not_https_redirect_if_not_default_ports():285 url = httpx.URL("https://example.com:1337")286 request = httpx.Request(287 "GET", "http://example.com:9999", headers={"Authorization": "empty"}288 )289290 client = httpx.Client()291 headers = client._redirect_headers(request, url, "GET")292293 assert "Authorization" not in headers
modified:tests/test_utils.py+1 added-60 removed
4 unmodified lines
56import pytest78import httpx9from httpx._utils import (10 URLPattern,11 get_environment_proxies,12)9from httpx._utils import URLPattern, get_environment_proxies101112@pytest.mark.parametrize(13 "encoding",
97 unmodified lines
111112 assert get_environment_proxies() == proxies113114118def test_same_origin():119 origin = httpx.URL("https://example.com")120 request = httpx.Request("GET", "HTTPS://EXAMPLE.COM:443")121122 client = httpx.Client()123 headers = client._redirect_headers(request, origin, "GET")124125 assert headers["Host"] == request.url.netloc.decode("ascii")126127128def test_not_same_origin():129 origin = httpx.URL("https://example.com")130 request = httpx.Request("GET", "HTTP://EXAMPLE.COM:80")131132 client = httpx.Client()133 headers = client._redirect_headers(request, origin, "GET")134135 assert headers["Host"] == origin.netloc.decode("ascii")136137138def test_is_https_redirect():139 url = httpx.URL("https://example.com")140 request = httpx.Request(141 "GET", "http://example.com", headers={"Authorization": "empty"}142 )143144 client = httpx.Client()145 headers = client._redirect_headers(request, url, "GET")146147 assert "Authorization" in headers148149150def test_is_not_https_redirect():151 url = httpx.URL("https://www.example.com")152 request = httpx.Request(153 "GET", "http://example.com", headers={"Authorization": "empty"}154 )155156 client = httpx.Client()157 headers = client._redirect_headers(request, url, "GET")158159 assert "Authorization" not in headers160161162def test_is_not_https_redirect_if_not_default_ports():163 url = httpx.URL("https://example.com:1337")164 request = httpx.Request(165 "GET", "http://example.com:9999", headers={"Authorization": "empty"}166 )167168 client = httpx.Client()169 headers = client._redirect_headers(request, url, "GET")170171 assert "Authorization" not in headers172173115@pytest.mark.parametrize(116 ["pattern", "url", "expected"],117 [118 ("http://example.com", "http://example.com", True),
32 unmodified lines