]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Get test suite back to ~100% line coverage (#406)
authorSeth Michael Larson <sethmichaellarson@gmail.com>
Sun, 29 Sep 2019 18:58:22 +0000 (13:58 -0500)
committerGitHub <noreply@github.com>
Sun, 29 Sep 2019 18:58:22 +0000 (13:58 -0500)
httpx/concurrency/asyncio/backend.py
httpx/concurrency/trio.py
httpx/dispatch/http2.py
httpx/models.py
tests/client/test_auth.py
tests/client/test_proxies.py
tests/client/test_queryparams.py
tests/dispatch/test_http2.py
tests/dispatch/test_proxy_http.py
tests/models/test_queryparams.py

index 51082ad93ba3725b3bae55737da4d4f316d2546d..00887409b584f2773e8510ca29e2d44ca7db9bf2 100644 (file)
@@ -54,10 +54,6 @@ class TCPStream(BaseTCPStream):
             return "HTTP/1.1"
 
         ident = ssl_object.selected_alpn_protocol()
-
-        if ident is None:
-            return "HTTP/1.1"
-
         return "HTTP/2" if ident == "h2" else "HTTP/1.1"
 
     async def read(
index 14d7986ef5b44fde7f75c38b421f765b8116903f..2c36f186f5bb05f54fcd8371c7dce578eb8649b1 100644 (file)
@@ -39,9 +39,6 @@ class TCPStream(BaseTCPStream):
             return "HTTP/1.1"
 
         ident = self.stream.selected_alpn_protocol()
-        if ident is None:
-            return "HTTP/1.1"
-
         return "HTTP/2" if ident == "h2" else "HTTP/1.1"
 
     async def read(
@@ -78,7 +75,7 @@ class TCPStream(BaseTCPStream):
         return stream.socket.is_readable()
 
     def write_no_block(self, data: bytes) -> None:
-        self.write_buffer += data
+        self.write_buffer += data  # pragma: no cover
 
     async def write(
         self, data: bytes, timeout: TimeoutConfig = None, flag: TimeoutFlag = None
index 361ddf9a445a1e19ea5c3856ef56456ef8de68bc..5d8765ca4e4bdb59e188e948e63b3b8ff85c5d04 100644 (file)
@@ -221,7 +221,7 @@ class HTTP2Connection:
                     else:
                         try:
                             self.window_update_received[event_stream_id].set()
-                        except KeyError:
+                        except KeyError:  # pragma: no cover
                             # the window_update_received dictionary is only relevant
                             # when sending data, which should never raise a KeyError
                             # here.
index 7b256684555c8976f8a64ae5c95c1ca4a8c925d8..887d8aedb6940ed48c155e2db385555077bdbc2b 100644 (file)
@@ -41,7 +41,7 @@ from .utils import (
     str_query_param,
 )
 
-if typing.TYPE_CHECKING:
+if typing.TYPE_CHECKING:  # pragma: no cover
     from .middleware.base import BaseMiddleware  # noqa: F401
     from .dispatch.base import AsyncDispatcher  # noqa: F401
 
index fa57ef528ec2cf1178236233773efd8fc5bff0db..1d5b9d5c1ffe34e2ac305a47aeddcb59d2eebd99 100644 (file)
@@ -91,9 +91,6 @@ class MockDigestAuthDispatch(AsyncDispatcher):
         ]
         return AsyncResponse(401, headers=headers, content=b"", request=request)
 
-    def reset(self) -> None:
-        self._response_count = 0
-
 
 def test_basic_auth():
     url = "https://example.org/"
index ba59677d7a12274f4a2b1cf590b86278e9ec31f0..32025666acabe24d40479e9196e78d232e410efc 100644 (file)
@@ -30,7 +30,14 @@ def test_proxies_parameter(proxies, expected_proxies):
 
 
 def test_proxies_has_same_properties_as_dispatch():
-    client = httpx.AsyncClient(proxies="http://127.0.0.1")
+    client = httpx.AsyncClient(
+        proxies="http://127.0.0.1",
+        verify="/path/to/verify",
+        cert="/path/to/cert",
+        trust_env=False,
+        timeout=30,
+        http_versions=["HTTP/1.1"],
+    )
     pool = client.dispatch
     proxy = client.proxies["all"]
 
@@ -46,3 +53,66 @@ def test_proxies_has_same_properties_as_dispatch():
         "backend",
     ]:
         assert getattr(pool, prop) == getattr(proxy, prop)
+
+
+PROXY_URL = "http://[::1]"
+
+
+@pytest.mark.parametrize(
+    ["url", "proxies", "expected"],
+    [
+        ("http://example.com", None, None),
+        ("http://example.com", {}, None),
+        ("http://example.com", {"https": PROXY_URL}, None),
+        ("http://example.com", {"http://example.net": PROXY_URL}, None),
+        ("http://example.com:443", {"http://example.com": PROXY_URL}, None),
+        ("http://example.com", {"all": PROXY_URL}, PROXY_URL),
+        ("http://example.com", {"http": PROXY_URL}, PROXY_URL),
+        ("http://example.com", {"all://example.com": PROXY_URL}, PROXY_URL),
+        ("http://example.com", {"all://example.com:80": PROXY_URL}, PROXY_URL),
+        ("http://example.com", {"http://example.com": PROXY_URL}, PROXY_URL),
+        ("http://example.com", {"http://example.com:80": PROXY_URL}, PROXY_URL),
+        ("http://example.com:8080", {"http://example.com:8080": PROXY_URL}, PROXY_URL),
+        ("http://example.com:8080", {"http://example.com": PROXY_URL}, None),
+        (
+            "http://example.com",
+            {
+                "all": PROXY_URL + ":1",
+                "http": PROXY_URL + ":2",
+                "all://example.com": PROXY_URL + ":3",
+                "http://example.com": PROXY_URL + ":4",
+            },
+            PROXY_URL + ":4",
+        ),
+        (
+            "http://example.com",
+            {
+                "all": PROXY_URL + ":1",
+                "http": PROXY_URL + ":2",
+                "all://example.com": PROXY_URL + ":3",
+            },
+            PROXY_URL + ":3",
+        ),
+        (
+            "http://example.com",
+            {"all": PROXY_URL + ":1", "http": PROXY_URL + ":2"},
+            PROXY_URL + ":2",
+        ),
+    ],
+)
+def test_dispatcher_for_request(url, proxies, expected):
+    client = httpx.AsyncClient(proxies=proxies)
+    request = httpx.AsyncRequest("GET", url)
+    dispatcher = client._dispatcher_for_request(request, client.proxies)
+
+    if expected is None:
+        assert isinstance(dispatcher, httpx.ConnectionPool)
+        assert dispatcher is client.dispatch
+    else:
+        assert isinstance(dispatcher, httpx.HTTPProxy)
+        assert dispatcher.proxy_url == expected
+
+
+def test_unsupported_proxy_scheme():
+    with pytest.raises(ValueError):
+        httpx.AsyncClient(proxies="ftp://127.0.0.1")
index 0558dd2a1547a1a687499a581a4ffb67e499b9fe..852593c0d14611cea3d5e10eb8df0f54325ae9a4 100644 (file)
@@ -37,6 +37,11 @@ def test_client_queryparams_string():
     assert isinstance(client.params, QueryParams)
     assert client.params["a"] == "b"
 
+    client = Client()
+    client.params = "a=b"
+    assert isinstance(client.params, QueryParams)
+    assert client.params["a"] == "b"
+
 
 def test_client_queryparams_echo():
     url = "http://example.org/echo_queryparams"
index 776ad8da396a480c380952c6646aa0cbe196e70b..e7ee68ef4c22d9c0a933bc5c5292bcac320da150 100644 (file)
@@ -204,3 +204,11 @@ async def test_http2_settings_in_handshake(backend):
     for setting_code, changed_setting in settings.changed_settings.items():
         assert isinstance(changed_setting, h2.settings.ChangedSetting)
         assert changed_setting.new_value == expected_settings[setting_code]
+
+
+async def test_http2_live_request(backend):
+    async with AsyncClient(backend=backend) as client:
+        resp = await client.get("https://nghttp2.org/httpbin/anything")
+
+        assert resp.status_code == 200
+        assert resp.http_version == "HTTP/2"
index 845f363208e854c68fd727444f37a11dc159cdfe..2d93871565e0602e0ef7423cacafc5bc1309865a 100644 (file)
@@ -166,9 +166,14 @@ async def test_proxy_forwarding(backend, proxy_mode):
         backend=backend,
     )
     async with httpx.HTTPProxy(
-        proxy_url="http://127.0.0.1:8000", backend=raw_io, proxy_mode=proxy_mode
+        proxy_url="http://127.0.0.1:8000",
+        backend=raw_io,
+        proxy_mode=proxy_mode,
+        proxy_headers={"Proxy-Authorization": "test", "Override": "2"},
     ) as proxy:
-        response = await proxy.request("GET", f"http://example.com")
+        response = await proxy.request(
+            "GET", f"http://example.com", headers={"override": "1"}
+        )
 
         assert response.status_code == 200
         assert response.headers["Server"] == "origin-server"
@@ -184,6 +189,8 @@ async def test_proxy_forwarding(backend, proxy_mode):
     assert recv[1].startswith(
         b"GET http://example.com HTTP/1.1\r\nhost: example.com\r\n"
     )
+    assert b"proxy-authorization: test" in recv[1]
+    assert b"override: 1" in recv[1]
 
 
 def test_proxy_url_with_username_and_password():
index fbb559fb629e3d7df0c9f76d204e90e7ab741ba0..8c4df4907ffe31120a719a0a93ba0490e769e7a5 100644 (file)
@@ -51,3 +51,14 @@ def test_queryparam_types():
 
     q = QueryParams({"a": 123})
     assert str(q) == "a=123"
+
+
+def test_queryparam_setters():
+    q = QueryParams({"a": 1})
+    q.update([])
+
+    assert str(q) == "a=1"
+
+    q = QueryParams([("a", 1), ("a", 2)])
+    q["a"] = "3"
+    assert str(q) == "a=3"