Add server.url property in tests (#300)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Add server.url to test server * Move a property to top of the class
authored by Bakhtiyor Ruziev · committed by Florimond Manca · parent a464627 · Browse files · 9 files·+78 added−82 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+78 added−82 removed
- tests:+78 added−82 removed
Provenance
- Commit
e34d280- Tree
c7a9faa- Author
Bakhtiyor Ruziev <32102033+theruziev@users.noreply.github.com>- Committer
Florimond Manca <florimond.manca@gmail.com>- Parent
a464627
9 files changed+78 added−82 removed
modified:tests/client/test_async_client.py+16 added-22 removed
2 unmodified lines3 import httpx4 5 6 async def test_get(server, backend):7- url = "http://127.0.0.1:8000/"7+ url = server.url8 async with httpx.AsyncClient(backend=backend) as client:9 response = await client.get(url)10 assert response.status_code == 20011 assert response.text == "Hello, world!"6 unmodified lines18 async def test_get_no_backend(server):19 """20 Verify that the client is capable of making a simple request if not given a backend.21 """22- url = "http://127.0.0.1:8000/"22+ url = server.url23 async with httpx.AsyncClient() as client:24 response = await client.get(url)25 assert response.status_code == 20026 assert response.text == "Hello, world!"2 unmodified lines29 assert repr(response) == "<Response [200 OK]>"30 31 32 async def test_post(server, backend):33- url = "http://127.0.0.1:8000/"33+ url = server.url34 async with httpx.AsyncClient(backend=backend) as client:35 response = await client.post(url, data=b"Hello, world!")36 assert response.status_code == 20037 38 39 async def test_post_json(server, backend):40- url = "http://127.0.0.1:8000/"40+ url = server.url41 async with httpx.AsyncClient(backend=backend) as client:42 response = await client.post(url, json={"text": "Hello, world!"})43 assert response.status_code == 20044 45 46 async def test_stream_response(server, backend):47 async with httpx.AsyncClient(backend=backend) as client:48- response = await client.request("GET", "http://127.0.0.1:8000/", stream=True)48+ response = await client.request("GET", server.url, stream=True)49 assert response.status_code == 20050 body = await response.read()51 assert body == b"Hello, world!"52 assert response.content == b"Hello, world!"53 54 55 async def test_access_content_stream_response(server, backend):56 async with httpx.AsyncClient(backend=backend) as client:57- response = await client.request("GET", "http://127.0.0.1:8000/", stream=True)57+ response = await client.request("GET", server.url, stream=True)58 assert response.status_code == 20059 with pytest.raises(httpx.ResponseNotRead):60 response.content61 3 unmodified lines65 yield b"Hello, "66 yield b"world!"67 68 async with httpx.AsyncClient(backend=backend) as client:69- response = await client.request(70- "POST", "http://127.0.0.1:8000/", data=hello_world()71- )69+ response = await client.request("POST", server.url, data=hello_world())70 assert response.status_code == 20071 72 73 async def test_raise_for_status(server, backend):74 async with httpx.AsyncClient(backend=backend) as client:75 for status_code in (200, 400, 404, 500, 505):76 response = await client.request(79- "GET", f"http://127.0.0.1:8000/status/{status_code}"77+ "GET", server.url.copy_with(path=f"/status/{status_code}")78 )79 80 if 400 <= status_code < 600:81 with pytest.raises(httpx.exceptions.HTTPError) as exc_info:3 unmodified lines85 assert response.raise_for_status() is None86 87 88 async def test_options(server, backend):91- url = "http://127.0.0.1:8000/"89 async with httpx.AsyncClient(backend=backend) as client:93- response = await client.options(url)90+ response = await client.options(server.url)91 assert response.status_code == 20092 assert response.text == "Hello, world!"93 94 95 async def test_head(server, backend):99- url = "http://127.0.0.1:8000/"96 async with httpx.AsyncClient(backend=backend) as client:101- response = await client.head(url)97+ response = await client.head(server.url)98 assert response.status_code == 20099 assert response.text == ""100 101 102 async def test_put(server, backend):107- url = "http://127.0.0.1:8000/"103 async with httpx.AsyncClient(backend=backend) as client:109- response = await client.put(url, data=b"Hello, world!")104+ response = await client.put(server.url, data=b"Hello, world!")105 assert response.status_code == 200106 107 108 async def test_patch(server, backend):114- url = "http://127.0.0.1:8000/"109 async with httpx.AsyncClient(backend=backend) as client:116- response = await client.patch(url, data=b"Hello, world!")110+ response = await client.patch(server.url, data=b"Hello, world!")111 assert response.status_code == 200112 113 114 async def test_delete(server, backend):121- url = "http://127.0.0.1:8000/"115 async with httpx.AsyncClient(backend=backend) as client:123- response = await client.delete(url)116+ response = await client.delete(server.url)117 assert response.status_code == 200118 assert response.text == "Hello, world!"119 120 121 async def test_100_continue(server, backend):129- url = "http://127.0.0.1:8000/echo_body"122 headers = {"Expect": "100-continue"}123 data = b"Echo request body"124 125 async with httpx.AsyncClient(backend=backend) as client:134- response = await client.post(url, headers=headers, data=data)126+ response = await client.post(127+ server.url.copy_with(path="/echo_body"), headers=headers, data=data128+ )129 130 assert response.status_code == 200131 assert response.content == datamodified:tests/client/test_client.py+16 added-16 removed
2 unmodified lines3 import httpx4 5 6 def test_get(server):7- url = "http://127.0.0.1:8000/"7+ url = server.url8 with httpx.Client() as http:9 response = http.get(url)10 assert response.status_code == 20011- assert response.url == httpx.URL(url)11+ assert response.url == url12 assert response.content == b"Hello, world!"13 assert response.text == "Hello, world!"14 assert response.http_version == "HTTP/1.1"15 assert response.encoding == "iso-8859-1"16- assert response.request.url == httpx.URL(url)16+ assert response.request.url == url17 assert response.headers18 assert response.is_redirect is False19 assert repr(response) == "<Response [200 OK]>"20 21 22 def test_post(server):23 with httpx.Client() as http:24- response = http.post("http://127.0.0.1:8000/", data=b"Hello, world!")24+ response = http.post(server.url, data=b"Hello, world!")25 assert response.status_code == 20026 assert response.reason_phrase == "OK"27 28 29 def test_post_json(server):30 with httpx.Client() as http:31- response = http.post("http://127.0.0.1:8000/", json={"text": "Hello, world!"})31+ response = http.post(server.url, json={"text": "Hello, world!"})32 assert response.status_code == 20033 assert response.reason_phrase == "OK"34 35 36 def test_stream_response(server):37 with httpx.Client() as http:38- response = http.get("http://127.0.0.1:8000/", stream=True)38+ response = http.get(server.url, stream=True)39 assert response.status_code == 20040 content = response.read()41 assert content == b"Hello, world!"42 43 44 def test_stream_iterator(server):45 with httpx.Client() as http:46- response = http.get("http://127.0.0.1:8000/", stream=True)46+ response = http.get(server.url, stream=True)47 assert response.status_code == 20048 body = b""49 for chunk in response.stream():50 body += chunk1 unmodified line52 53 54 def test_raw_iterator(server):55 with httpx.Client() as http:56- response = http.get("http://127.0.0.1:8000/", stream=True)56+ response = http.get(server.url, stream=True)57 assert response.status_code == 20058 body = b""59 for chunk in response.raw():60 body += chunk4 unmodified lines65 def test_raise_for_status(server):66 with httpx.Client() as client:67 for status_code in (200, 400, 404, 500, 505):68 response = client.request(69- "GET", "http://127.0.0.1:8000/status/{}".format(status_code)69+ "GET", server.url.copy_with(path="/status/{}".format(status_code))70 )71 if 400 <= status_code < 600:72 with pytest.raises(httpx.exceptions.HTTPError) as exc_info:73 response.raise_for_status()3 unmodified lines77 78 79 def test_options(server):80 with httpx.Client() as http:81- response = http.options("http://127.0.0.1:8000/")81+ response = http.options(server.url)82 assert response.status_code == 20083 assert response.reason_phrase == "OK"84 85 86 def test_head(server):87 with httpx.Client() as http:88- response = http.head("http://127.0.0.1:8000/")88+ response = http.head(server.url)89 assert response.status_code == 20090 assert response.reason_phrase == "OK"91 92 93 def test_put(server):94 with httpx.Client() as http:95- response = http.put("http://127.0.0.1:8000/", data=b"Hello, world!")95+ response = http.put(server.url, data=b"Hello, world!")96 assert response.status_code == 20097 assert response.reason_phrase == "OK"98 99 100 def test_patch(server):101 with httpx.Client() as http:102- response = http.patch("http://127.0.0.1:8000/", data=b"Hello, world!")102+ response = http.patch(server.url, data=b"Hello, world!")103 assert response.status_code == 200104 assert response.reason_phrase == "OK"105 106 107 def test_delete(server):108 with httpx.Client() as http:109- response = http.delete("http://127.0.0.1:8000/")109+ response = http.delete(server.url)110 assert response.status_code == 200111 assert response.reason_phrase == "OK"112 113 114 def test_base_url(server):115- base_url = "http://127.0.0.1:8000/"115+ base_url = server.url116 with httpx.Client(base_url=base_url) as http:117 response = http.get("/")118 assert response.status_code == 200119- assert str(response.url) == base_url119+ assert response.url == base_url120 121 122 def test_merge_url():123 client = httpx.Client(base_url="https://www.paypal.com/")20 unmodified linesmodified:tests/dispatch/test_connection_pools.py+16 added-16 removed
4 unmodified lines5 """6 Connections should default to staying in a keep-alive state.7 """8 async with httpx.ConnectionPool(backend=backend) as http:9- response = await http.request("GET", "http://127.0.0.1:8000/")9+ response = await http.request("GET", server.url)10 await response.read()11 assert len(http.active_connections) == 012 assert len(http.keepalive_connections) == 113 14- response = await http.request("GET", "http://127.0.0.1:8000/")14+ response = await http.request("GET", server.url)15 await response.read()16 assert len(http.active_connections) == 017 assert len(http.keepalive_connections) == 118 2 unmodified lines21 """22 Connections to differing connection keys should result in multiple connections.23 """24 async with httpx.ConnectionPool(backend=backend) as http:25- response = await http.request("GET", "http://127.0.0.1:8000/")25+ response = await http.request("GET", server.url)26 await response.read()27 assert len(http.active_connections) == 028 assert len(http.keepalive_connections) == 129 9 unmodified lines39 """40 pool_limits = httpx.PoolLimits(soft_limit=1)41 42 async with httpx.ConnectionPool(pool_limits=pool_limits, backend=backend) as http:43- response = await http.request("GET", "http://127.0.0.1:8000/")43+ response = await http.request("GET", server.url)44 await response.read()45 assert len(http.active_connections) == 046 assert len(http.keepalive_connections) == 147 7 unmodified lines55 """56 A streaming request should hold the connection open until the response is read.57 """58 async with httpx.ConnectionPool(backend=backend) as http:59- response = await http.request("GET", "http://127.0.0.1:8000/")59+ response = await http.request("GET", server.url)60 assert len(http.active_connections) == 161 assert len(http.keepalive_connections) == 062 63 await response.read()6 unmodified lines70 """71 Multiple conncurrent requests should open multiple conncurrent connections.72 """73 async with httpx.ConnectionPool(backend=backend) as http:74- response_a = await http.request("GET", "http://127.0.0.1:8000/")74+ response_a = await http.request("GET", server.url)75 assert len(http.active_connections) == 176 assert len(http.keepalive_connections) == 077 78- response_b = await http.request("GET", "http://127.0.0.1:8000/")78+ response_b = await http.request("GET", server.url)79 assert len(http.active_connections) == 280 assert len(http.keepalive_connections) == 081 82 await response_b.read()10 unmodified lines93 Using a `Connection: close` header should close the connection.94 """95 headers = [(b"connection", b"close")]96 async with httpx.ConnectionPool(backend=backend) as http:97- response = await http.request("GET", "http://127.0.0.1:8000/", headers=headers)97+ response = await http.request("GET", server.url, headers=headers)98 await response.read()99 assert len(http.active_connections) == 0100 assert len(http.keepalive_connections) == 0101 2 unmodified lines104 """105 A standard close should keep the connection open.106 """107 async with httpx.ConnectionPool(backend=backend) as http:108- response = await http.request("GET", "http://127.0.0.1:8000/")108+ response = await http.request("GET", server.url)109 await response.read()110 await response.close()111 assert len(http.active_connections) == 0112 assert len(http.keepalive_connections) == 13 unmodified lines116 """117 A premature close should close the connection.118 """119 async with httpx.ConnectionPool(backend=backend) as http:120- response = await http.request("GET", "http://127.0.0.1:8000/")120+ response = await http.request("GET", server.url)121 await response.close()122 assert len(http.active_connections) == 0123 assert len(http.keepalive_connections) == 0124 5 unmodified lines130 Upon keep-alive connection closed by remote a new connection131 should be reestablished.132 """133 async with httpx.ConnectionPool(backend=backend) as http:134- response = await http.request("GET", "http://127.0.0.1:8000/")134+ response = await http.request("GET", server.url)135 await response.read()136 137 # Shutdown the server to close the keep-alive connection138 await restart(server)139 140- response = await http.request("GET", "http://127.0.0.1:8000/")140+ response = await http.request("GET", server.url)141 await response.read()142 assert len(http.active_connections) == 0143 assert len(http.keepalive_connections) == 1144 5 unmodified lines150 Upon keep-alive connection closed by remote a new connection151 should be reestablished.152 """153 async with httpx.ConnectionPool(backend=backend) as http:154- response = await http.request("GET", "http://127.0.0.1:8000/")154+ response = await http.request("GET", server.url)155 await response.read()156 157 # Shutdown the server to close the keep-alive connection158 await restart(server)159 160- response = await http.request("GET", "http://127.0.0.1:8000/")160+ response = await http.request("GET", server.url)161 await response.read()162 assert len(http.active_connections) == 0163 assert len(http.keepalive_connections) == 1164 3 unmodified lines168 Verify that max_connections semaphore is released169 properly on a disconnected connection.170 """171 async with httpx.ConnectionPool(pool_limits=httpx.PoolLimits(hard_limit=1)) as http:172- response = await http.request("GET", "http://127.0.0.1:8000/")172+ response = await http.request("GET", server.url)173 await response.read()174 175 # Close the connection so we're forced to recycle it176 await restart(server)177 178- response = await http.request("GET", "http://127.0.0.1:8000/")178+ response = await http.request("GET", server.url)179 assert response.status_code == 200modified:tests/dispatch/test_connections.py+8 added-12 removed
1 from httpx import HTTPConnection2 3 4 async def test_get(server, backend):5- async with HTTPConnection(origin="http://127.0.0.1:8000/", backend=backend) as conn:6- response = await conn.request("GET", "http://127.0.0.1:8000/")5+ async with HTTPConnection(origin=server.url, backend=backend) as conn:6+ response = await conn.request("GET", server.url)7 await response.read()8 assert response.status_code == 2009 assert response.content == b"Hello, world!"10 11 12 async def test_post(server, backend):13- async with HTTPConnection(origin="http://127.0.0.1:8000/", backend=backend) as conn:14- response = await conn.request(15- "GET", "http://127.0.0.1:8000/", data=b"Hello, world!"16- )13+ async with HTTPConnection(origin=server.url, backend=backend) as conn:14+ response = await conn.request("GET", server.url, data=b"Hello, world!")15 assert response.status_code == 20016 17 18 async def test_https_get_with_ssl_defaults(https_server, backend):19 """20 An HTTPS request, with default SSL configuration set on the client.21 """22 async with HTTPConnection(25- origin="https://127.0.0.1:8001/", verify=False, backend=backend23+ origin=https_server.url, verify=False, backend=backend24 ) as conn:27- response = await conn.request("GET", "https://127.0.0.1:8001/")25+ response = await conn.request("GET", https_server.url)26 await response.read()27 assert response.status_code == 20028 assert response.content == b"Hello, world!"29 1 unmodified line31 async def test_https_get_with_sll_overrides(https_server, backend):32 """33 An HTTPS request, with SSL configuration set on the request.34 """37- async with HTTPConnection(38- origin="https://127.0.0.1:8001/", backend=backend39- ) as conn:40- response = await conn.request("GET", "https://127.0.0.1:8001/", verify=False)35+ async with HTTPConnection(origin=https_server.url, backend=backend) as conn:36+ response = await conn.request("GET", https_server.url, verify=False)37 await response.read()38 assert response.status_code == 20039 assert response.content == b"Hello, world!"modified:tests/conftest.py+6 added-2 removed
10 unmodified lines11 )12 from uvicorn.config import Config13 from uvicorn.main import Server14 15-from httpx import AsyncioBackend15+from httpx import URL, AsyncioBackend16 17 18 @pytest.fixture(params=[pytest.param(AsyncioBackend, marks=pytest.mark.asyncio)])19 def backend(request):111 unmodified lines131 yield tmp132 133 134 class TestServer(Server):135+ @property136+ def url(self) -> URL:137+ protocol = "https" if self.config.is_ssl else "http"138+ return URL(f"{protocol}://{self.config.host}:{self.config.port}/")139+140 def install_signal_handlers(self) -> None:141 # Disable the default installation of handlers for signals such as SIGTERM,142 # because it can only be done in the main thread.143 pass5 unmodified lines149 tasks = {150 loop.create_task(super().serve(sockets=sockets)),151 loop.create_task(self.watch_restarts()),152 }148-153 await asyncio.wait(tasks)154 155 async def restart(self) -> None:156 # Ensure we are in an asyncio environment.69 unmodified linesmodified:tests/test_api.py+8 added-8 removed
2 unmodified lines3 import httpx4 5 6 def test_get(server):7- response = httpx.get("http://127.0.0.1:8000/")7+ response = httpx.get(server.url)8 assert response.status_code == 2009 assert response.reason_phrase == "OK"10 assert response.text == "Hello, world!"11 assert response.http_version == "HTTP/1.1"12 13 14 def test_post(server):15- response = httpx.post("http://127.0.0.1:8000/", data=b"Hello, world!")15+ response = httpx.post(server.url, data=b"Hello, world!")16 assert response.status_code == 20017 assert response.reason_phrase == "OK"18 19 2 unmodified lines22 yield b"Hello"23 yield b", "24 yield b"world!"25 26- response = httpx.post("http://127.0.0.1:8000/", data=data())26+ response = httpx.post(server.url, data=data())27 assert response.status_code == 20028 assert response.reason_phrase == "OK"29 30 31 def test_options(server):32- response = httpx.options("http://127.0.0.1:8000/")32+ response = httpx.options(server.url)33 assert response.status_code == 20034 assert response.reason_phrase == "OK"35 36 37 def test_head(server):38- response = httpx.head("http://127.0.0.1:8000/")38+ response = httpx.head(server.url)39 assert response.status_code == 20040 assert response.reason_phrase == "OK"41 42 43 def test_put(server):44- response = httpx.put("http://127.0.0.1:8000/", data=b"Hello, world!")44+ response = httpx.put(server.url, data=b"Hello, world!")45 assert response.status_code == 20046 assert response.reason_phrase == "OK"47 48 49 def test_patch(server):50- response = httpx.patch("http://127.0.0.1:8000/", data=b"Hello, world!")50+ response = httpx.patch(server.url, data=b"Hello, world!")51 assert response.status_code == 20052 assert response.reason_phrase == "OK"53 54 55 def test_delete(server):56- response = httpx.delete("http://127.0.0.1:8000/")56+ response = httpx.delete(server.url)57 assert response.status_code == 20058 assert response.reason_phrase == "OK"59 60 3 unmodified linesmodified:tests/test_concurrency.py+4 added-2 removed
17 unmodified lines18 backend = AsyncioBackend()19 ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig())20 timeout = TimeoutConfig(5)21 22- stream = await backend.connect("127.0.0.1", 8001, None, timeout)22+ stream = await backend.connect(23+ https_server.url.host, https_server.url.port, None, timeout24+ )25 26 try:27 assert stream.is_connection_dropped() is False28 assert stream.stream_writer.get_extra_info("cipher", default=None) is None29 28- stream = await backend.start_tls(stream, "127.0.0.1", ctx, timeout)30+ stream = await backend.start_tls(stream, https_server.url.host, ctx, timeout)31 assert stream.is_connection_dropped() is False32 assert stream.stream_writer.get_extra_info("cipher", default=None) is not None33 34 await stream.write(b"GET / HTTP/1.1\r\n\r\n")4 unmodified linesmodified:tests/test_timeouts.py+3 added-3 removed
14 unmodified lines15 timeout = TimeoutConfig(read_timeout=1e-6)16 17 async with AsyncClient(timeout=timeout, backend=backend) as client:18 with pytest.raises(ReadTimeout):19- await client.get("http://127.0.0.1:8000/slow_response")19+ await client.get(server.url.copy_with(path="/slow_response"))20 21 22 async def test_write_timeout(server, backend):23 timeout = TimeoutConfig(write_timeout=1e-6)24 25 async with AsyncClient(timeout=timeout, backend=backend) as client:26 with pytest.raises(WriteTimeout):27 data = b"*" * 1024 * 1024 * 10028- await client.put("http://127.0.0.1:8000/slow_response", data=data)28+ await client.put(server.url.copy_with(path="/slow_response"), data=data)29 30 31 async def test_connect_timeout(server, backend):32 timeout = TimeoutConfig(connect_timeout=1e-6)7 unmodified lines40 async def test_pool_timeout(server, backend):41 pool_limits = PoolLimits(hard_limit=1, pool_timeout=1e-6)42 43 async with AsyncClient(pool_limits=pool_limits, backend=backend) as client:44- response = await client.get("http://127.0.0.1:8000/", stream=True)44+ response = await client.get(server.url, stream=True)45 46 with pytest.raises(PoolTimeout):47 await client.get("http://localhost:8000/")48 1 unmodified linemodified:tests/test_utils.py+1 added-1 removed
100 unmodified lines101 utils._LOGGER_INITIALIZED = False102 utils.get_logger("httpx")103 104 async with httpx.AsyncClient() as client:105- await client.get("http://127.0.0.1:8000/")105+ await client.get(server.url)106 107 if httpx_debug in ("1", "True"):108 assert "httpx.dispatch.connection_pool" in capsys.readouterr().err109 else:4 unmodified lines