]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Add server.url property in tests (#300)
authorBakhtiyor Ruziev <32102033+theruziev@users.noreply.github.com>
Sun, 1 Sep 2019 07:07:22 +0000 (10:07 +0300)
committerFlorimond Manca <florimond.manca@gmail.com>
Sun, 1 Sep 2019 07:07:22 +0000 (09:07 +0200)
* Add server.url to test server

* Move a property to top of the class

tests/client/test_async_client.py
tests/client/test_client.py
tests/conftest.py
tests/dispatch/test_connection_pools.py
tests/dispatch/test_connections.py
tests/test_api.py
tests/test_concurrency.py
tests/test_timeouts.py
tests/test_utils.py

index bd297432140ed1e5fe07a37011d7db9bdea3b7ae..c028c1b3fb099e22fc09fab4e4f162b62bc98569 100644 (file)
@@ -4,7 +4,7 @@ import httpx
 
 
 async def test_get(server, backend):
-    url = "http://127.0.0.1:8000/"
+    url = server.url
     async with httpx.AsyncClient(backend=backend) as client:
         response = await client.get(url)
     assert response.status_code == 200
@@ -19,7 +19,7 @@ async def test_get_no_backend(server):
     """
     Verify that the client is capable of making a simple request if not given a backend.
     """
-    url = "http://127.0.0.1:8000/"
+    url = server.url
     async with httpx.AsyncClient() as client:
         response = await client.get(url)
     assert response.status_code == 200
@@ -30,14 +30,14 @@ async def test_get_no_backend(server):
 
 
 async def test_post(server, backend):
-    url = "http://127.0.0.1:8000/"
+    url = server.url
     async with httpx.AsyncClient(backend=backend) as client:
         response = await client.post(url, data=b"Hello, world!")
     assert response.status_code == 200
 
 
 async def test_post_json(server, backend):
-    url = "http://127.0.0.1:8000/"
+    url = server.url
     async with httpx.AsyncClient(backend=backend) as client:
         response = await client.post(url, json={"text": "Hello, world!"})
     assert response.status_code == 200
@@ -45,7 +45,7 @@ async def test_post_json(server, backend):
 
 async def test_stream_response(server, backend):
     async with httpx.AsyncClient(backend=backend) as client:
-        response = await client.request("GET", "http://127.0.0.1:8000/", stream=True)
+        response = await client.request("GET", server.url, stream=True)
     assert response.status_code == 200
     body = await response.read()
     assert body == b"Hello, world!"
@@ -54,7 +54,7 @@ async def test_stream_response(server, backend):
 
 async def test_access_content_stream_response(server, backend):
     async with httpx.AsyncClient(backend=backend) as client:
-        response = await client.request("GET", "http://127.0.0.1:8000/", stream=True)
+        response = await client.request("GET", server.url, stream=True)
     assert response.status_code == 200
     with pytest.raises(httpx.ResponseNotRead):
         response.content
@@ -66,9 +66,7 @@ async def test_stream_request(server, backend):
         yield b"world!"
 
     async with httpx.AsyncClient(backend=backend) as client:
-        response = await client.request(
-            "POST", "http://127.0.0.1:8000/", data=hello_world()
-        )
+        response = await client.request("POST", server.url, data=hello_world())
     assert response.status_code == 200
 
 
@@ -76,7 +74,7 @@ async def test_raise_for_status(server, backend):
     async with httpx.AsyncClient(backend=backend) as client:
         for status_code in (200, 400, 404, 500, 505):
             response = await client.request(
-                "GET", f"http://127.0.0.1:8000/status/{status_code}"
+                "GET", server.url.copy_with(path=f"/status/{status_code}")
             )
 
             if 400 <= status_code < 600:
@@ -88,50 +86,46 @@ async def test_raise_for_status(server, backend):
 
 
 async def test_options(server, backend):
-    url = "http://127.0.0.1:8000/"
     async with httpx.AsyncClient(backend=backend) as client:
-        response = await client.options(url)
+        response = await client.options(server.url)
     assert response.status_code == 200
     assert response.text == "Hello, world!"
 
 
 async def test_head(server, backend):
-    url = "http://127.0.0.1:8000/"
     async with httpx.AsyncClient(backend=backend) as client:
-        response = await client.head(url)
+        response = await client.head(server.url)
     assert response.status_code == 200
     assert response.text == ""
 
 
 async def test_put(server, backend):
-    url = "http://127.0.0.1:8000/"
     async with httpx.AsyncClient(backend=backend) as client:
-        response = await client.put(url, data=b"Hello, world!")
+        response = await client.put(server.url, data=b"Hello, world!")
     assert response.status_code == 200
 
 
 async def test_patch(server, backend):
-    url = "http://127.0.0.1:8000/"
     async with httpx.AsyncClient(backend=backend) as client:
-        response = await client.patch(url, data=b"Hello, world!")
+        response = await client.patch(server.url, data=b"Hello, world!")
     assert response.status_code == 200
 
 
 async def test_delete(server, backend):
-    url = "http://127.0.0.1:8000/"
     async with httpx.AsyncClient(backend=backend) as client:
-        response = await client.delete(url)
+        response = await client.delete(server.url)
     assert response.status_code == 200
     assert response.text == "Hello, world!"
 
 
 async def test_100_continue(server, backend):
-    url = "http://127.0.0.1:8000/echo_body"
     headers = {"Expect": "100-continue"}
     data = b"Echo request body"
 
     async with httpx.AsyncClient(backend=backend) as client:
-        response = await client.post(url, headers=headers, data=data)
+        response = await client.post(
+            server.url.copy_with(path="/echo_body"), headers=headers, data=data
+        )
 
     assert response.status_code == 200
     assert response.content == data
index 52d99e3b0c8c29256f21057810b141d3eb5a282a..73979209af3a484fbd2503ed144d3fa081b6583a 100644 (file)
@@ -4,16 +4,16 @@ import httpx
 
 
 def test_get(server):
-    url = "http://127.0.0.1:8000/"
+    url = server.url
     with httpx.Client() as http:
         response = http.get(url)
     assert response.status_code == 200
-    assert response.url == httpx.URL(url)
+    assert response.url == url
     assert response.content == b"Hello, world!"
     assert response.text == "Hello, world!"
     assert response.http_version == "HTTP/1.1"
     assert response.encoding == "iso-8859-1"
-    assert response.request.url == httpx.URL(url)
+    assert response.request.url == url
     assert response.headers
     assert response.is_redirect is False
     assert repr(response) == "<Response [200 OK]>"
@@ -21,21 +21,21 @@ def test_get(server):
 
 def test_post(server):
     with httpx.Client() as http:
-        response = http.post("http://127.0.0.1:8000/", data=b"Hello, world!")
+        response = http.post(server.url, data=b"Hello, world!")
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
 
 def test_post_json(server):
     with httpx.Client() as http:
-        response = http.post("http://127.0.0.1:8000/", json={"text": "Hello, world!"})
+        response = http.post(server.url, json={"text": "Hello, world!"})
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
 
 def test_stream_response(server):
     with httpx.Client() as http:
-        response = http.get("http://127.0.0.1:8000/", stream=True)
+        response = http.get(server.url, stream=True)
     assert response.status_code == 200
     content = response.read()
     assert content == b"Hello, world!"
@@ -43,7 +43,7 @@ def test_stream_response(server):
 
 def test_stream_iterator(server):
     with httpx.Client() as http:
-        response = http.get("http://127.0.0.1:8000/", stream=True)
+        response = http.get(server.url, stream=True)
     assert response.status_code == 200
     body = b""
     for chunk in response.stream():
@@ -53,7 +53,7 @@ def test_stream_iterator(server):
 
 def test_raw_iterator(server):
     with httpx.Client() as http:
-        response = http.get("http://127.0.0.1:8000/", stream=True)
+        response = http.get(server.url, stream=True)
     assert response.status_code == 200
     body = b""
     for chunk in response.raw():
@@ -66,7 +66,7 @@ def test_raise_for_status(server):
     with httpx.Client() as client:
         for status_code in (200, 400, 404, 500, 505):
             response = client.request(
-                "GET", "http://127.0.0.1:8000/status/{}".format(status_code)
+                "GET", server.url.copy_with(path="/status/{}".format(status_code))
             )
             if 400 <= status_code < 600:
                 with pytest.raises(httpx.exceptions.HTTPError) as exc_info:
@@ -78,45 +78,45 @@ def test_raise_for_status(server):
 
 def test_options(server):
     with httpx.Client() as http:
-        response = http.options("http://127.0.0.1:8000/")
+        response = http.options(server.url)
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
 
 def test_head(server):
     with httpx.Client() as http:
-        response = http.head("http://127.0.0.1:8000/")
+        response = http.head(server.url)
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
 
 def test_put(server):
     with httpx.Client() as http:
-        response = http.put("http://127.0.0.1:8000/", data=b"Hello, world!")
+        response = http.put(server.url, data=b"Hello, world!")
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
 
 def test_patch(server):
     with httpx.Client() as http:
-        response = http.patch("http://127.0.0.1:8000/", data=b"Hello, world!")
+        response = http.patch(server.url, data=b"Hello, world!")
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
 
 def test_delete(server):
     with httpx.Client() as http:
-        response = http.delete("http://127.0.0.1:8000/")
+        response = http.delete(server.url)
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
 
 def test_base_url(server):
-    base_url = "http://127.0.0.1:8000/"
+    base_url = server.url
     with httpx.Client(base_url=base_url) as http:
         response = http.get("/")
     assert response.status_code == 200
-    assert str(response.url) == base_url
+    assert response.url == base_url
 
 
 def test_merge_url():
index ec75e60093460f44944143f09e7144eaabd206eb..86a993426b00cbffb4f9f54f023f36a0830fd0d8 100644 (file)
@@ -12,7 +12,7 @@ from cryptography.hazmat.primitives.serialization import (
 from uvicorn.config import Config
 from uvicorn.main import Server
 
-from httpx import AsyncioBackend
+from httpx import URL, AsyncioBackend
 
 
 @pytest.fixture(params=[pytest.param(AsyncioBackend, marks=pytest.mark.asyncio)])
@@ -132,6 +132,11 @@ def cert_encrypted_private_key_file(example_cert):
 
 
 class TestServer(Server):
+    @property
+    def url(self) -> URL:
+        protocol = "https" if self.config.is_ssl else "http"
+        return URL(f"{protocol}://{self.config.host}:{self.config.port}/")
+
     def install_signal_handlers(self) -> None:
         # Disable the default installation of handlers for signals such as SIGTERM,
         # because it can only be done in the main thread.
@@ -145,7 +150,6 @@ class TestServer(Server):
             loop.create_task(super().serve(sockets=sockets)),
             loop.create_task(self.watch_restarts()),
         }
-
         await asyncio.wait(tasks)
 
     async def restart(self) -> None:
index 8f0ba70c93091de2cad1f801b607554d88263017..276580875fa3a8517fa1d0793df507b9f87dd7fb 100644 (file)
@@ -6,12 +6,12 @@ async def test_keepalive_connections(server, backend):
     Connections should default to staying in a keep-alive state.
     """
     async with httpx.ConnectionPool(backend=backend) as http:
-        response = await http.request("GET", "http://127.0.0.1:8000/")
+        response = await http.request("GET", server.url)
         await response.read()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
 
-        response = await http.request("GET", "http://127.0.0.1:8000/")
+        response = await http.request("GET", server.url)
         await response.read()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
@@ -22,7 +22,7 @@ async def test_differing_connection_keys(server, backend):
     Connections to differing connection keys should result in multiple connections.
     """
     async with httpx.ConnectionPool(backend=backend) as http:
-        response = await http.request("GET", "http://127.0.0.1:8000/")
+        response = await http.request("GET", server.url)
         await response.read()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
@@ -40,7 +40,7 @@ async def test_soft_limit(server, backend):
     pool_limits = httpx.PoolLimits(soft_limit=1)
 
     async with httpx.ConnectionPool(pool_limits=pool_limits, backend=backend) as http:
-        response = await http.request("GET", "http://127.0.0.1:8000/")
+        response = await http.request("GET", server.url)
         await response.read()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
@@ -56,7 +56,7 @@ async def test_streaming_response_holds_connection(server, backend):
     A streaming request should hold the connection open until the response is read.
     """
     async with httpx.ConnectionPool(backend=backend) as http:
-        response = await http.request("GET", "http://127.0.0.1:8000/")
+        response = await http.request("GET", server.url)
         assert len(http.active_connections) == 1
         assert len(http.keepalive_connections) == 0
 
@@ -71,11 +71,11 @@ async def test_multiple_concurrent_connections(server, backend):
     Multiple conncurrent requests should open multiple conncurrent connections.
     """
     async with httpx.ConnectionPool(backend=backend) as http:
-        response_a = await http.request("GET", "http://127.0.0.1:8000/")
+        response_a = await http.request("GET", server.url)
         assert len(http.active_connections) == 1
         assert len(http.keepalive_connections) == 0
 
-        response_b = await http.request("GET", "http://127.0.0.1:8000/")
+        response_b = await http.request("GET", server.url)
         assert len(http.active_connections) == 2
         assert len(http.keepalive_connections) == 0
 
@@ -94,7 +94,7 @@ async def test_close_connections(server, backend):
     """
     headers = [(b"connection", b"close")]
     async with httpx.ConnectionPool(backend=backend) as http:
-        response = await http.request("GET", "http://127.0.0.1:8000/", headers=headers)
+        response = await http.request("GET", server.url, headers=headers)
         await response.read()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 0
@@ -105,7 +105,7 @@ async def test_standard_response_close(server, backend):
     A standard close should keep the connection open.
     """
     async with httpx.ConnectionPool(backend=backend) as http:
-        response = await http.request("GET", "http://127.0.0.1:8000/")
+        response = await http.request("GET", server.url)
         await response.read()
         await response.close()
         assert len(http.active_connections) == 0
@@ -117,7 +117,7 @@ async def test_premature_response_close(server, backend):
     A premature close should close the connection.
     """
     async with httpx.ConnectionPool(backend=backend) as http:
-        response = await http.request("GET", "http://127.0.0.1:8000/")
+        response = await http.request("GET", server.url)
         await response.close()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 0
@@ -131,13 +131,13 @@ async def test_keepalive_connection_closed_by_server_is_reestablished(
     should be reestablished.
     """
     async with httpx.ConnectionPool(backend=backend) as http:
-        response = await http.request("GET", "http://127.0.0.1:8000/")
+        response = await http.request("GET", server.url)
         await response.read()
 
         # Shutdown the server to close the keep-alive connection
         await restart(server)
 
-        response = await http.request("GET", "http://127.0.0.1:8000/")
+        response = await http.request("GET", server.url)
         await response.read()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
@@ -151,13 +151,13 @@ async def test_keepalive_http2_connection_closed_by_server_is_reestablished(
     should be reestablished.
     """
     async with httpx.ConnectionPool(backend=backend) as http:
-        response = await http.request("GET", "http://127.0.0.1:8000/")
+        response = await http.request("GET", server.url)
         await response.read()
 
         # Shutdown the server to close the keep-alive connection
         await restart(server)
 
-        response = await http.request("GET", "http://127.0.0.1:8000/")
+        response = await http.request("GET", server.url)
         await response.read()
         assert len(http.active_connections) == 0
         assert len(http.keepalive_connections) == 1
@@ -169,11 +169,11 @@ async def test_connection_closed_free_semaphore_on_acquire(server, restart, back
     properly on a disconnected connection.
     """
     async with httpx.ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1)) as http:
-        response = await http.request("GET", "http://127.0.0.1:8000/")
+        response = await http.request("GET", server.url)
         await response.read()
 
         # Close the connection so we're forced to recycle it
         await restart(server)
 
-        response = await http.request("GET", "http://127.0.0.1:8000/")
+        response = await http.request("GET", server.url)
         assert response.status_code == 200
index 0aa7741287ba49c81359fbd4e7fd2660bc8ab054..14386aaa7d1f74b5ad2c4717b611263350b57284 100644 (file)
@@ -2,18 +2,16 @@ from httpx import HTTPConnection
 
 
 async def test_get(server, backend):
-    async with HTTPConnection(origin="http://127.0.0.1:8000/", backend=backend) as conn:
-        response = await conn.request("GET", "http://127.0.0.1:8000/")
+    async with HTTPConnection(origin=server.url, backend=backend) as conn:
+        response = await conn.request("GET", server.url)
         await response.read()
         assert response.status_code == 200
         assert response.content == b"Hello, world!"
 
 
 async def test_post(server, backend):
-    async with HTTPConnection(origin="http://127.0.0.1:8000/", backend=backend) as conn:
-        response = await conn.request(
-            "GET", "http://127.0.0.1:8000/", data=b"Hello, world!"
-        )
+    async with HTTPConnection(origin=server.url, backend=backend) as conn:
+        response = await conn.request("GET", server.url, data=b"Hello, world!")
         assert response.status_code == 200
 
 
@@ -22,9 +20,9 @@ async def test_https_get_with_ssl_defaults(https_server, backend):
     An HTTPS request, with default SSL configuration set on the client.
     """
     async with HTTPConnection(
-        origin="https://127.0.0.1:8001/", verify=False, backend=backend
+        origin=https_server.url, verify=False, backend=backend
     ) as conn:
-        response = await conn.request("GET", "https://127.0.0.1:8001/")
+        response = await conn.request("GET", https_server.url)
         await response.read()
         assert response.status_code == 200
         assert response.content == b"Hello, world!"
@@ -34,10 +32,8 @@ async def test_https_get_with_sll_overrides(https_server, backend):
     """
     An HTTPS request, with SSL configuration set on the request.
     """
-    async with HTTPConnection(
-        origin="https://127.0.0.1:8001/", backend=backend
-    ) as conn:
-        response = await conn.request("GET", "https://127.0.0.1:8001/", verify=False)
+    async with HTTPConnection(origin=https_server.url, backend=backend) as conn:
+        response = await conn.request("GET", https_server.url, verify=False)
         await response.read()
         assert response.status_code == 200
         assert response.content == b"Hello, world!"
index cb9eb20c66e656e54025ab88fd5d60e8717f4667..1500869eb00945e2c63ee03a3dc54eecbfb29b9a 100644 (file)
@@ -4,7 +4,7 @@ import httpx
 
 
 def test_get(server):
-    response = httpx.get("http://127.0.0.1:8000/")
+    response = httpx.get(server.url)
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
     assert response.text == "Hello, world!"
@@ -12,7 +12,7 @@ def test_get(server):
 
 
 def test_post(server):
-    response = httpx.post("http://127.0.0.1:8000/", data=b"Hello, world!")
+    response = httpx.post(server.url, data=b"Hello, world!")
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
@@ -23,37 +23,37 @@ def test_post_byte_iterator(server):
         yield b", "
         yield b"world!"
 
-    response = httpx.post("http://127.0.0.1:8000/", data=data())
+    response = httpx.post(server.url, data=data())
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
 
 def test_options(server):
-    response = httpx.options("http://127.0.0.1:8000/")
+    response = httpx.options(server.url)
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
 
 def test_head(server):
-    response = httpx.head("http://127.0.0.1:8000/")
+    response = httpx.head(server.url)
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
 
 def test_put(server):
-    response = httpx.put("http://127.0.0.1:8000/", data=b"Hello, world!")
+    response = httpx.put(server.url, data=b"Hello, world!")
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
 
 def test_patch(server):
-    response = httpx.patch("http://127.0.0.1:8000/", data=b"Hello, world!")
+    response = httpx.patch(server.url, data=b"Hello, world!")
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
 
 def test_delete(server):
-    response = httpx.delete("http://127.0.0.1:8000/")
+    response = httpx.delete(server.url)
     assert response.status_code == 200
     assert response.reason_phrase == "OK"
 
index 2fa0161510dcbd1b7a9712eeb730d91ab8809f5a..b9312eaa139b4c3d5c322442295c90389e722c2e 100644 (file)
@@ -19,13 +19,15 @@ async def test_start_tls_on_socket_stream(https_server):
     ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig())
     timeout = TimeoutConfig(5)
 
-    stream = await backend.connect("127.0.0.1", 8001, None, timeout)
+    stream = await backend.connect(
+        https_server.url.host, https_server.url.port, None, timeout
+    )
 
     try:
         assert stream.is_connection_dropped() is False
         assert stream.stream_writer.get_extra_info("cipher", default=None) is None
 
-        stream = await backend.start_tls(stream, "127.0.0.1", ctx, timeout)
+        stream = await backend.start_tls(stream, https_server.url.host, ctx, timeout)
         assert stream.is_connection_dropped() is False
         assert stream.stream_writer.get_extra_info("cipher", default=None) is not None
 
index 540fa918562901e84c45e41dbda2ad3515ac29dd..765c8f7d3a5cdcd1d746fa3426b3bed93d2736da 100644 (file)
@@ -16,7 +16,7 @@ async def test_read_timeout(server, backend):
 
     async with AsyncClient(timeout=timeout, backend=backend) as client:
         with pytest.raises(ReadTimeout):
-            await client.get("http://127.0.0.1:8000/slow_response")
+            await client.get(server.url.copy_with(path="/slow_response"))
 
 
 async def test_write_timeout(server, backend):
@@ -25,7 +25,7 @@ async def test_write_timeout(server, backend):
     async with AsyncClient(timeout=timeout, backend=backend) as client:
         with pytest.raises(WriteTimeout):
             data = b"*" * 1024 * 1024 * 100
-            await client.put("http://127.0.0.1:8000/slow_response", data=data)
+            await client.put(server.url.copy_with(path="/slow_response"), data=data)
 
 
 async def test_connect_timeout(server, backend):
@@ -41,7 +41,7 @@ async def test_pool_timeout(server, backend):
     pool_limits = PoolLimits(hard_limit=1, pool_timeout=1e-6)
 
     async with AsyncClient(pool_limits=pool_limits, backend=backend) as client:
-        response = await client.get("http://127.0.0.1:8000/", stream=True)
+        response = await client.get(server.url, stream=True)
 
         with pytest.raises(PoolTimeout):
             await client.get("http://localhost:8000/")
index bb2abb4e04781d073000eac48b453ab8d87e92e9..d3dad731f2c2572d0a03e1db1775efcf36d50a9c 100644 (file)
@@ -102,7 +102,7 @@ async def test_httpx_debug_enabled_stderr_logging(server, capsys, httpx_debug):
     utils.get_logger("httpx")
 
     async with httpx.AsyncClient() as client:
-        await client.get("http://127.0.0.1:8000/")
+        await client.get(server.url)
 
     if httpx_debug in ("1", "True"):
         assert "httpx.dispatch.connection_pool" in capsys.readouterr().err