Drop private Origin model (#1070)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Drop private Origin model * Drop Origin from docs * Update tests/test_utils.py Co-authored-by: Florimond Manca <florimond.manca@gmail.com> * Drop full_path test
authored by Tom Christie · committed by GitHub · parent 20f4911 · Browse files · 6 files·+27 added−73 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
Diff stats
6 files+27 added−73 removed
- tests:+13 added−22 removed
- other:+14 added−51 removed
Provenance
- Commit
247ee0d- Tree
f739102- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
20f4911
6 files changed+27 added−73 removed
modified:docs/api.md-17 removed
118 unmodified linesExpand all11912012112212312312412512712612913013113213313413513613713813914014114214312712812913027 unmodified lines118 unmodified linesExpand all* `.query` - **str*** `.full_path` - **str*** `.fragment` - **str*** `.is_ssl` - **bool*** `.origin` - **Origin*** `.is_absolute_url` - **bool*** `.is_relative_url` - **bool*** `def .copy_with([scheme], [authority], [path], [query], [fragment])` - **URL*** `def .resolve_with(url)` - **URL**
## `Origin`
*A normalized, IDNA supporting set of scheme/host/port info.*
```python>>> Origin('https://example.org') == Origin('HTTPS://EXAMPLE.ORG:443')True```
* `def __init__(url)`* `.scheme` - **str*** `.is_ssl` - **bool*** `.host` - **str*** `.port` - **int**
## `Headers`
*A case-insensitive multi-dict.*
27 unmodified linesmodified:httpx/_client.py+3 added-2 removed
24 unmodified lines25 RequestBodyUnavailable,26 TooManyRedirects,27 map_exceptions,28 )29-from ._models import URL, Cookies, Headers, Origin, QueryParams, Request, Response29+from ._models import URL, Cookies, Headers, QueryParams, Request, Response30 from ._status_codes import codes31 from ._transports.asgi import ASGITransport32 from ._transports.wsgi import WSGITransport33 from ._types import (12 unmodified lines46 from ._utils import (47 NetRCInfo,48 get_environment_proxies,49 get_logger,50+ same_origin,51 should_not_be_proxied,52 )53 54 logger = get_logger(__name__)291 unmodified lines346 Return the headers that should be used for the redirect request.347 """348 headers = Headers(request.headers)349 349- if Origin(url) != Origin(request.url):350+ if not same_origin(url, request.url):351 # Strip Authorization headers when responses are redirected away from352 # the origin.353 headers.pop("Authorization", None)354 headers["Host"] = url.authority1166 unmodified linesmodified:httpx/_models.py-31 removed
232 unmodified lines233 )234 return f"{class_name}({url_str!r})"235 236 237-class Origin:238- """239- The URL scheme and authority information, as a comparable, hashable object.240- """241-242- def __init__(self, url: URLTypes) -> None:243- if not isinstance(url, URL):244- url = URL(url)245- self.scheme = url.scheme246- self.is_ssl = url.is_ssl247- self.host = url.host248- self.port = url.port249-250- def __eq__(self, other: typing.Any) -> bool:251- return (252- isinstance(other, self.__class__)253- and self.scheme == other.scheme254- and self.host == other.host255- and self.port == other.port256- )257-258- def __hash__(self) -> int:259- return hash((self.scheme, self.host, self.port))260-261- def __repr__(self) -> str:262- class_name = self.__class__.__name__263- return (264- f"{class_name}(scheme={self.scheme!r} host={self.host!r} port={self.port})"265- )266-267-237 class QueryParams(typing.Mapping[str, str]):238 """239 URL query parameters, as a multi-dict.240 """913 unmodified linesmodified:httpx/_utils.py+11 added-1 removed
258 unmodified lines259 260 return typing.cast(Logger, logger)261 262 263+def same_origin(url: "URL", other: "URL") -> bool:264+ """265+ Return 'True' if the given URLs share the same origin.266+ """267+ return (268+ url.scheme == other.scheme and url.host == other.host and url.port == other.port269+ )270+271+272 def should_not_be_proxied(url: "URL") -> bool:273+ """264- """ Return True if url should not be proxied,274+ Return True if url should not be proxied,275 return False otherwise.276 """277 no_proxy = getproxies().get("no")278 if not no_proxy:130 unmodified linesmodified:tests/models/test_url.py-22 removed
1 import pytest2 3 from httpx import URL, InvalidURL4-from httpx._models import Origin4 5 6 @pytest.mark.parametrize(7 "given,idna,host,scheme,port",167 unmodified lines175 176 assert all(url in urls for url in url_set)177 178 180-def test_origin_from_url_string():181- origin = Origin("https://example.com")182- assert origin.scheme == "https"183- assert origin.is_ssl184- assert origin.host == "example.com"185- assert origin.port == 443186-187-188-def test_origin_repr():189- origin = Origin("https://example.com:8080")190- assert str(origin) == "Origin(scheme='https' host='example.com' port=8080)"191-192-193-def test_origin_equal():194- origin1 = Origin("https://example.com")195- origin2 = Origin("https://example.com")196- assert origin1 is not origin2197- assert origin1 == origin2198- assert len({origin1, origin2}) == 1199-200-179 def test_url_copywith_for_authority():180 copy_with_kwargs = {181 "username": "username",182 "password": "password",8 unmodified linesmodified:tests/test_utils.py+13 added
10 unmodified lines11 get_environment_proxies,12 guess_json_utf,13 obfuscate_sensitive_headers,14 parse_header_links,15+ same_origin,16 should_not_be_proxied,17 )18 from tests.utils import override_log_level19 274 unmodified lines294 def test_should_not_be_proxied(url, no_proxy, expected):295 os.environ.update(no_proxy)296 parsed_url = httpx.URL(url)297 assert should_not_be_proxied(parsed_url) == expected298+299+300+def test_same_origin():301+ origin1 = httpx.URL("https://example.com")302+ origin2 = httpx.URL("HTTPS://EXAMPLE.COM:443")303+ assert same_origin(origin1, origin2)304+305+306+def test_not_same_origin():307+ origin1 = httpx.URL("https://example.com")308+ origin2 = httpx.URL("HTTP://EXAMPLE.COM")309+ assert not same_origin(origin1, origin2)