Skip to content
Myr

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

9 files+78 added−82 removed

modified:tests/client/test_async_client.py+16 added-22 removed
2 unmodified lines
3import httpx456async 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 lines
18async 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 lines
29 assert repr(response) == "<Response [200 OK]>"303132async 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 == 200373839async 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 == 200444546async 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!"535455async 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 lines
65 yield b"Hello, "66 yield b"world!"6768 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 == 200717273async 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 )7980 if 400 <= status_code < 600:81 with pytest.raises(httpx.exceptions.HTTPError) as exc_info:
3 unmodified lines
85 assert response.raise_for_status() is None868788async 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!"939495async 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 == ""100101102async 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 == 200106107108async 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 == 200112113114async 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!"119120121async 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"124125 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 )129130 assert response.status_code == 200131 assert response.content == data
modified:tests/client/test_client.py+16 added-16 removed
2 unmodified lines
3import httpx456def 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]>"202122def 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"272829def 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"343536def 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!"424344def 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 += chunk
1 unmodified line
525354def 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 += chunk
4 unmodified lines
65def 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 lines
777879def 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"848586def 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"919293def 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"9899100def 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"105106107def 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"112113114def 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_url120121122def test_merge_url():123 client = httpx.Client(base_url="https://www.paypal.com/")
20 unmodified lines
modified:tests/dispatch/test_connection_pools.py+16 added-16 removed
4 unmodified lines
5 """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) == 11314 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 lines
21 """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 lines
39 """40 pool_limits = httpx.PoolLimits(soft_limit=1)4142 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 lines
55 """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) == 06263 await response.read()
6 unmodified lines
70 """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) == 07778 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) == 08182 await response_b.read()
10 unmodified lines
93 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 lines
104 """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) == 1
3 unmodified lines
116 """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 lines
130 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()136137 # Shutdown the server to close the keep-alive connection138 await restart(server)139140 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 lines
150 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()156157 # Shutdown the server to close the keep-alive connection158 await restart(server)159160 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 lines
168 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()174175 # Close the connection so we're forced to recycle it176 await restart(server)177178 response = await http.request("GET", "http://127.0.0.1:8000/")178 response = await http.request("GET", server.url)179 assert response.status_code == 200
modified:tests/dispatch/test_connections.py+8 added-12 removed
1from httpx import HTTPConnection234async 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!"101112async 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 == 200161718async 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 line
31async 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 lines
11)12from uvicorn.config import Config13from uvicorn.main import Server1415from httpx import AsyncioBackend15from httpx import URL, AsyncioBackend161718@pytest.fixture(params=[pytest.param(AsyncioBackend, marks=pytest.mark.asyncio)])19def backend(request):
111 unmodified lines
131 yield tmp132133134class 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}/")139140 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 pass
5 unmodified lines
149 tasks = {150 loop.create_task(super().serve(sockets=sockets)),151 loop.create_task(self.watch_restarts()),152 }148153 await asyncio.wait(tasks)154155 async def restart(self) -> None:156 # Ensure we are in an asyncio environment.
69 unmodified lines
modified:tests/test_api.py+8 added-8 removed
2 unmodified lines
3import httpx456def 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"121314def 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"1819
2 unmodified lines
22 yield b"Hello"23 yield b", "24 yield b"world!"2526 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"293031def 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"353637def 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"414243def 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"474849def 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"535455def 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"5960
3 unmodified lines
modified:tests/test_concurrency.py+4 added-2 removed
17 unmodified lines
18 backend = AsyncioBackend()19 ctx = SSLConfig().load_ssl_context_no_verify(HTTPVersionConfig())20 timeout = TimeoutConfig(5)2122 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 )2526 try:27 assert stream.is_connection_dropped() is False28 assert stream.stream_writer.get_extra_info("cipher", default=None) is None2928 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 None3334 await stream.write(b"GET / HTTP/1.1\r\n\r\n")
4 unmodified lines
modified:tests/test_timeouts.py+3 added-3 removed
14 unmodified lines
15 timeout = TimeoutConfig(read_timeout=1e-6)1617 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"))202122async def test_write_timeout(server, backend):23 timeout = TimeoutConfig(write_timeout=1e-6)2425 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)293031async def test_connect_timeout(server, backend):32 timeout = TimeoutConfig(connect_timeout=1e-6)
7 unmodified lines
40async def test_pool_timeout(server, backend):41 pool_limits = PoolLimits(hard_limit=1, pool_timeout=1e-6)4243 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)4546 with pytest.raises(PoolTimeout):47 await client.get("http://localhost:8000/")48
1 unmodified line
modified:tests/test_utils.py+1 added-1 removed
100 unmodified lines
101 utils._LOGGER_INITIALIZED = False102 utils.get_logger("httpx")103104 async with httpx.AsyncClient() as client:105 await client.get("http://127.0.0.1:8000/")105 await client.get(server.url)106107 if httpx_debug in ("1", "True"):108 assert "httpx.dispatch.connection_pool" in capsys.readouterr().err109 else:
4 unmodified lines