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
Provenance
- Commit
7b19cd5- Tree
48da544- Author
RafaelWO <38643099+RafaelWO@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
b47d94c
4 files changed+92 added−100 removed
modified:httpx/_client.py+35 added-8 removed
43 unmodified lines44 SyncByteStream,45 TimeoutTypes,46 )47 from ._urls import URL, QueryParams48-from ._utils import (49- URLPattern,50- get_environment_proxies,51- is_https_redirect,52- same_origin,53-)48+from ._utils import URLPattern, get_environment_proxies49 50 if typing.TYPE_CHECKING:51 import ssl # pragma: no cover52 4 unmodified lines57 T = typing.TypeVar("T", bound="Client")58 U = typing.TypeVar("U", bound="AsyncClient")59 60 61+def _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 False67+68+ return (69+ url.scheme == "http"70+ and _port_or_default(url) == 8071+ and location.scheme == "https"72+ and _port_or_default(location) == 44373+ )74+75+76+def _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)80+81+82+def _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+ )91+92+93 class 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 different450 unmodified lines547 Return the headers that should be used for the redirect request.548 """549 headers = Headers(request.headers)550 524- 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 linesmodified:httpx/_utils.py-32 removed
25 unmodified lines26 return ""27 return str(value)28 29 30-def 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)34-35-36-def 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- )45-46-47-def 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 False53-54- return (55- url.scheme == "http"56- and port_or_default(url) == 8057- and location.scheme == "https"58- and port_or_default(location) == 44359- )60-61-30 def get_environment_proxies() -> dict[str, str | None]:31 """Gets proxy information from the environment"""32 33 # urllib.request.getproxies() falls back on System209 unmodified linesmodified:tests/client/test_headers.py+56 added
233 unmodified lines234 235 def test_request_auto_headers():236 request = httpx.Request("GET", "https://www.example.org/")237 assert "host" in request.headers238+239+240+def test_same_origin():241+ origin = httpx.URL("https://example.com")242+ request = httpx.Request("GET", "HTTPS://EXAMPLE.COM:443")243+244+ client = httpx.Client()245+ headers = client._redirect_headers(request, origin, "GET")246+247+ assert headers["Host"] == request.url.netloc.decode("ascii")248+249+250+def test_not_same_origin():251+ origin = httpx.URL("https://example.com")252+ request = httpx.Request("GET", "HTTP://EXAMPLE.COM:80")253+254+ client = httpx.Client()255+ headers = client._redirect_headers(request, origin, "GET")256+257+ assert headers["Host"] == origin.netloc.decode("ascii")258+259+260+def test_is_https_redirect():261+ url = httpx.URL("https://example.com")262+ request = httpx.Request(263+ "GET", "http://example.com", headers={"Authorization": "empty"}264+ )265+266+ client = httpx.Client()267+ headers = client._redirect_headers(request, url, "GET")268+269+ assert "Authorization" in headers270+271+272+def 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+ )277+278+ client = httpx.Client()279+ headers = client._redirect_headers(request, url, "GET")280+281+ assert "Authorization" not in headers282+283+284+def 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+ )289+290+ client = httpx.Client()291+ headers = client._redirect_headers(request, url, "GET")292+293+ assert "Authorization" not in headersmodified:tests/test_utils.py+1 added-60 removed
4 unmodified lines5 6 import pytest7 8 import httpx9-from httpx._utils import (10- URLPattern,11- get_environment_proxies,12-)9+from httpx._utils import URLPattern, get_environment_proxies10 11 12 @pytest.mark.parametrize(13 "encoding",97 unmodified lines111 112 assert get_environment_proxies() == proxies113 114 118-def test_same_origin():119- origin = httpx.URL("https://example.com")120- request = httpx.Request("GET", "HTTPS://EXAMPLE.COM:443")121-122- client = httpx.Client()123- headers = client._redirect_headers(request, origin, "GET")124-125- assert headers["Host"] == request.url.netloc.decode("ascii")126-127-128-def test_not_same_origin():129- origin = httpx.URL("https://example.com")130- request = httpx.Request("GET", "HTTP://EXAMPLE.COM:80")131-132- client = httpx.Client()133- headers = client._redirect_headers(request, origin, "GET")134-135- assert headers["Host"] == origin.netloc.decode("ascii")136-137-138-def test_is_https_redirect():139- url = httpx.URL("https://example.com")140- request = httpx.Request(141- "GET", "http://example.com", headers={"Authorization": "empty"}142- )143-144- client = httpx.Client()145- headers = client._redirect_headers(request, url, "GET")146-147- assert "Authorization" in headers148-149-150-def 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- )155-156- client = httpx.Client()157- headers = client._redirect_headers(request, url, "GET")158-159- assert "Authorization" not in headers160-161-162-def 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- )167-168- client = httpx.Client()169- headers = client._redirect_headers(request, url, "GET")170-171- assert "Authorization" not in headers172-173-115 @pytest.mark.parametrize(116 ["pattern", "url", "expected"],117 [118 ("http://example.com", "http://example.com", True),32 unmodified lines