]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Drop private Origin model (#1070)
authorTom Christie <tom@tomchristie.com>
Thu, 23 Jul 2020 08:54:45 +0000 (09:54 +0100)
committerGitHub <noreply@github.com>
Thu, 23 Jul 2020 08:54:45 +0000 (09:54 +0100)
* 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

Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
docs/api.md
httpx/_client.py
httpx/_models.py
httpx/_utils.py
tests/models/test_url.py
tests/test_utils.py

index 2ad1f9f24f02fb11dc6db1fc47f6c61b7d9c8f52..a34d366d5615e005fe6a334961da2c46eb743537 100644 (file)
@@ -120,26 +120,9 @@ what gets sent over the wire.*
 * `.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`
 
index d4ec7aaa540dcdeb5e7fbe1c4e535699ad70c418..66b4805d85d5278e63f22445cb060d5ccd613063 100644 (file)
@@ -26,7 +26,7 @@ from ._exceptions import (
     TooManyRedirects,
     map_exceptions,
 )
-from ._models import URL, Cookies, Headers, Origin, QueryParams, Request, Response
+from ._models import URL, Cookies, Headers, QueryParams, Request, Response
 from ._status_codes import codes
 from ._transports.asgi import ASGITransport
 from ._transports.wsgi import WSGITransport
@@ -47,6 +47,7 @@ from ._utils import (
     NetRCInfo,
     get_environment_proxies,
     get_logger,
+    same_origin,
     should_not_be_proxied,
 )
 
@@ -346,7 +347,7 @@ class BaseClient:
         """
         headers = Headers(request.headers)
 
-        if Origin(url) != Origin(request.url):
+        if not same_origin(url, request.url):
             # Strip Authorization headers when responses are redirected away from
             # the origin.
             headers.pop("Authorization", None)
index dca3eff9484c3526cd23afba83252f3c874872e6..b992d9d7842fff7d0c14577efcf855b5c4efdec6 100644 (file)
@@ -234,37 +234,6 @@ class URL:
         return f"{class_name}({url_str!r})"
 
 
-class Origin:
-    """
-    The URL scheme and authority information, as a comparable, hashable object.
-    """
-
-    def __init__(self, url: URLTypes) -> None:
-        if not isinstance(url, URL):
-            url = URL(url)
-        self.scheme = url.scheme
-        self.is_ssl = url.is_ssl
-        self.host = url.host
-        self.port = url.port
-
-    def __eq__(self, other: typing.Any) -> bool:
-        return (
-            isinstance(other, self.__class__)
-            and self.scheme == other.scheme
-            and self.host == other.host
-            and self.port == other.port
-        )
-
-    def __hash__(self) -> int:
-        return hash((self.scheme, self.host, self.port))
-
-    def __repr__(self) -> str:
-        class_name = self.__class__.__name__
-        return (
-            f"{class_name}(scheme={self.scheme!r} host={self.host!r} port={self.port})"
-        )
-
-
 class QueryParams(typing.Mapping[str, str]):
     """
     URL query parameters, as a multi-dict.
index 8c01eb16c4c4fe30d27d6766f47e2415e4f6f609..b85ec758547359082f14a9e6da4f30d75527b37c 100644 (file)
@@ -260,8 +260,18 @@ def get_logger(name: str) -> Logger:
     return typing.cast(Logger, logger)
 
 
+def same_origin(url: "URL", other: "URL") -> bool:
+    """
+    Return 'True' if the given URLs share the same origin.
+    """
+    return (
+        url.scheme == other.scheme and url.host == other.host and url.port == other.port
+    )
+
+
 def should_not_be_proxied(url: "URL") -> bool:
-    """ Return True if url should not be proxied,
+    """
+    Return True if url should not be proxied,
     return False otherwise.
     """
     no_proxy = getproxies().get("no")
index 7910a8e904931c59fd784ad3ae9dda243afd38b3..de9da7d2f7d49db6a409e09a73e3bec11f142ba8 100644 (file)
@@ -1,7 +1,6 @@
 import pytest
 
 from httpx import URL, InvalidURL
-from httpx._models import Origin
 
 
 @pytest.mark.parametrize(
@@ -177,27 +176,6 @@ def test_url_set():
     assert all(url in urls for url in url_set)
 
 
-def test_origin_from_url_string():
-    origin = Origin("https://example.com")
-    assert origin.scheme == "https"
-    assert origin.is_ssl
-    assert origin.host == "example.com"
-    assert origin.port == 443
-
-
-def test_origin_repr():
-    origin = Origin("https://example.com:8080")
-    assert str(origin) == "Origin(scheme='https' host='example.com' port=8080)"
-
-
-def test_origin_equal():
-    origin1 = Origin("https://example.com")
-    origin2 = Origin("https://example.com")
-    assert origin1 is not origin2
-    assert origin1 == origin2
-    assert len({origin1, origin2}) == 1
-
-
 def test_url_copywith_for_authority():
     copy_with_kwargs = {
         "username": "username",
index 70fc1d622941119a1e178203c9b5cdc4d0d44566..fa30ee87cd9bfb320254926e04c71b70826cca3a 100644 (file)
@@ -12,6 +12,7 @@ from httpx._utils import (
     guess_json_utf,
     obfuscate_sensitive_headers,
     parse_header_links,
+    same_origin,
     should_not_be_proxied,
 )
 from tests.utils import override_log_level
@@ -294,3 +295,15 @@ def test_should_not_be_proxied(url, no_proxy, expected):
     os.environ.update(no_proxy)
     parsed_url = httpx.URL(url)
     assert should_not_be_proxied(parsed_url) == expected
+
+
+def test_same_origin():
+    origin1 = httpx.URL("https://example.com")
+    origin2 = httpx.URL("HTTPS://EXAMPLE.COM:443")
+    assert same_origin(origin1, origin2)
+
+
+def test_not_same_origin():
+    origin1 = httpx.URL("https://example.com")
+    origin2 = httpx.URL("HTTP://EXAMPLE.COM")
+    assert not same_origin(origin1, origin2)