]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Increased test coverage & cleanup (#1003)
authorJosep Cugat <jcugat@gmail.com>
Sat, 30 May 2020 21:22:03 +0000 (23:22 +0200)
committerGitHub <noreply@github.com>
Sat, 30 May 2020 21:22:03 +0000 (23:22 +0200)
* Remove unused/untested headers copy() method

Last usage was removed in #804

* Remove unused premature_close server endpoint

Last usage was removed in #804

* Increased test coverage

* Revert removal of headers copy() method

Documented and added tests for it.

Co-authored-by: Florimond Manca <florimond.manca@gmail.com>
docs/api.md
tests/client/test_async_client.py
tests/client/test_client.py
tests/client/test_proxies.py
tests/conftest.py
tests/models/test_headers.py
tests/models/test_url.py

index c63d4ecd3940a62bf78dc8afd46747b5478143dc..2ad1f9f24f02fb11dc6db1fc47f6c61b7d9c8f52 100644 (file)
@@ -151,7 +151,8 @@ True
 'application/json'
 ```
 
-* `def __init__(self, headers)`
+* `def __init__(self, headers, encoding=None)`
+* `def copy()` - **Headers**
 
 ## `Cookies`
 
index 8f0e9d6f56d77b3a9036e69e24ccd5baea330fb7..649e428f5e4a3dc8714bc003ad46c172f57125df 100644 (file)
@@ -1,8 +1,10 @@
 from datetime import timedelta
 
+import httpcore
 import pytest
 
 import httpx
+from httpx import ASGIDispatch
 
 
 @pytest.mark.usefixtures("async_environment")
@@ -18,6 +20,13 @@ async def test_get(server):
     assert response.elapsed > timedelta(seconds=0)
 
 
+@pytest.mark.usefixtures("async_environment")
+async def test_get_invalid_url(server):
+    async with httpx.AsyncClient() as client:
+        with pytest.raises(httpx.InvalidURL):
+            await client.get("invalid://example.org")
+
+
 @pytest.mark.usefixtures("async_environment")
 async def test_build_request(server):
     url = server.url.copy_with(path="/echo_headers")
@@ -148,3 +157,28 @@ async def test_100_continue(server):
 
     assert response.status_code == 200
     assert response.content == data
+
+
+def test_dispatch_deprecated():
+    dispatch = httpcore.AsyncHTTPTransport()
+
+    with pytest.warns(DeprecationWarning) as record:
+        client = httpx.AsyncClient(dispatch=dispatch)
+
+    assert client.transport is dispatch
+    assert len(record) == 1
+    assert record[0].message.args[0] == (
+        "The dispatch argument is deprecated since v0.13 and will be "
+        "removed in a future release, please use 'transport'"
+    )
+
+
+def test_asgi_dispatch_deprecated():
+    with pytest.warns(DeprecationWarning) as record:
+        ASGIDispatch(None)
+
+    assert len(record) == 1
+    assert (
+        record[0].message.args[0]
+        == "ASGIDispatch is deprecated, please use ASGITransport"
+    )
index 5271e733799fda0056fe93c978fcd10b8589f033..02f78f6999c2fe058272ac5cbc47f43c9f450546 100644 (file)
@@ -1,8 +1,10 @@
 from datetime import timedelta
 
+import httpcore
 import pytest
 
 import httpx
+from httpx import WSGIDispatch
 
 
 def test_get(server):
@@ -103,12 +105,13 @@ def test_raise_for_status(server):
     with httpx.Client() as client:
         for status_code in (200, 400, 404, 500, 505):
             response = client.request(
-                "GET", server.url.copy_with(path="/status/{}".format(status_code))
+                "GET", server.url.copy_with(path=f"/status/{status_code}")
             )
             if 400 <= status_code < 600:
                 with pytest.raises(httpx.HTTPError) as exc_info:
                     response.raise_for_status()
                 assert exc_info.value.response == response
+                assert exc_info.value.request.url.path == f"/status/{status_code}"
             else:
                 assert response.raise_for_status() is None  # type: ignore
 
@@ -162,3 +165,28 @@ def test_merge_url():
 
     assert url.scheme == "https"
     assert url.is_ssl
+
+
+def test_dispatch_deprecated():
+    dispatch = httpcore.SyncHTTPTransport()
+
+    with pytest.warns(DeprecationWarning) as record:
+        client = httpx.Client(dispatch=dispatch)
+
+    assert client.transport is dispatch
+    assert len(record) == 1
+    assert record[0].message.args[0] == (
+        "The dispatch argument is deprecated since v0.13 and will be "
+        "removed in a future release, please use 'transport'"
+    )
+
+
+def test_wsgi_dispatch_deprecated():
+    with pytest.warns(DeprecationWarning) as record:
+        WSGIDispatch(None)
+
+    assert len(record) == 1
+    assert (
+        record[0].message.args[0]
+        == "WSGIDispatch is deprecated, please use WSGITransport"
+    )
index ce8f403b5ed22580cf2b896d0b705ec3ab129f0a..1b6253b2a22489b8b00df3c117c4591bb0591da1 100644 (file)
@@ -105,11 +105,12 @@ def test_unsupported_proxy_scheme():
         ),
     ],
 )
-def test_proxies_environ(monkeypatch, url, env, expected):
+@pytest.mark.parametrize("client_class", [httpx.Client, httpx.AsyncClient])
+def test_proxies_environ(monkeypatch, client_class, url, env, expected):
     for name, value in env.items():
         monkeypatch.setenv(name, value)
 
-    client = httpx.AsyncClient()
+    client = client_class()
     transport = client.transport_for_url(httpx.URL(url))
 
     if expected is None:
index d5be55f4b29e4b6066eadd9bb42d568e4a7f1f67..61c06f240a67141ef9d73dfc42a595fe6990ba30 100644 (file)
@@ -76,8 +76,6 @@ async def app(scope, receive, send):
     assert scope["type"] == "http"
     if scope["path"].startswith("/slow_response"):
         await slow_response(scope, receive, send)
-    elif scope["path"].startswith("/premature_close"):
-        await premature_close(scope, receive, send)
     elif scope["path"].startswith("/status"):
         await status_code(scope, receive, send)
     elif scope["path"].startswith("/echo_body"):
@@ -113,16 +111,6 @@ async def slow_response(scope, receive, send):
     await send({"type": "http.response.body", "body": b"Hello, world!"})
 
 
-async def premature_close(scope, receive, send):
-    await send(
-        {
-            "type": "http.response.start",
-            "status": 200,
-            "headers": [[b"content-type", b"text/plain"]],
-        }
-    )
-
-
 async def status_code(scope, receive, send):
     status_code = int(scope["path"].replace("/status/", ""))
     await send(
index 959d782c72ad02ac11a593e2276c835514908a8d..088f5c8a1fe51dcbb980ea968a33d27ee239d9c6 100644 (file)
@@ -46,7 +46,14 @@ def test_header_mutations():
     assert h.raw == [(b"b", b"4")]
 
 
-def test_copy_headers():
+def test_copy_headers_method():
+    headers = httpx.Headers({"custom": "example"})
+    headers_copy = headers.copy()
+    assert headers == headers_copy
+    assert headers is not headers_copy
+
+
+def test_copy_headers_init():
     headers = httpx.Headers({"custom": "example"})
     headers_copy = httpx.Headers(headers)
     assert headers == headers_copy
index cb2f3ee162ad7f41fb1a6b5eb980f7a1f48dbd12..f902be521cc28a4f6099304e5ea2b75d973c3f7c 100644 (file)
@@ -192,6 +192,11 @@ def test_origin_from_url_string():
     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_url_copywith_for_authority():
     copy_with_kwargs = {
         "username": "username",