Skip to content
Myr

Increased test coverage & cleanup (#1003)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* 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.

authored by Josep Cugat · committed by GitHub · parent 89a8100 · Browse files · 7 files+81 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Florimond Manca <florimond.manca@gmail.com>

Diff stats

7 files+81 added−17 removed

  • tests:+79 added−16 removed
  • other:+2 added−1 removed

7 files+81 added−17 removed

modified:docs/api.md+2 added-1 removed
modified:tests/client/test_async_client.py+34 added
1from datetime import timedelta23import httpcore4import pytest56import httpx7from httpx import ASGIDispatch8910@pytest.mark.usefixtures("async_environment")11async def test_get(server):
8 unmodified lines
20 assert response.elapsed > timedelta(seconds=0)212223@pytest.mark.usefixtures("async_environment")24async def test_get_invalid_url(server):25 async with httpx.AsyncClient() as client:26 with pytest.raises(httpx.InvalidURL):27 await client.get("invalid://example.org")282930@pytest.mark.usefixtures("async_environment")31async def test_build_request(server):32 url = server.url.copy_with(path="/echo_headers")33 headers = {"Custom-header": "value"}34 async with httpx.AsyncClient() as client:
121 unmodified lines
156 )157158 assert response.status_code == 200159 assert response.content == data160161162def test_dispatch_deprecated():163 dispatch = httpcore.AsyncHTTPTransport()164165 with pytest.warns(DeprecationWarning) as record:166 client = httpx.AsyncClient(dispatch=dispatch)167168 assert client.transport is dispatch169 assert len(record) == 1170 assert record[0].message.args[0] == (171 "The dispatch argument is deprecated since v0.13 and will be "172 "removed in a future release, please use 'transport'"173 )174175176def test_asgi_dispatch_deprecated():177 with pytest.warns(DeprecationWarning) as record:178 ASGIDispatch(None)179180 assert len(record) == 1181 assert (182 record[0].message.args[0]183 == "ASGIDispatch is deprecated, please use ASGITransport"184 )
modified:tests/client/test_client.py+29 added-1 removed
1from datetime import timedelta23import httpcore4import pytest56import httpx7from httpx import WSGIDispatch8910def test_get(server):11 url = server.url
92 unmodified lines
104def test_raise_for_status(server):105 with httpx.Client() as client:106 for status_code in (200, 400, 404, 500, 505):107 response = client.request(106 "GET", server.url.copy_with(path="/status/{}".format(status_code))108 "GET", server.url.copy_with(path=f"/status/{status_code}")109 )110 if 400 <= status_code < 600:111 with pytest.raises(httpx.HTTPError) as exc_info:112 response.raise_for_status()113 assert exc_info.value.response == response114 assert exc_info.value.request.url.path == f"/status/{status_code}"115 else:116 assert response.raise_for_status() is None # type: ignore117118
45 unmodified lines
164 url = client.merge_url("http://www.paypal.com")165166 assert url.scheme == "https"167 assert url.is_ssl168169170def test_dispatch_deprecated():171 dispatch = httpcore.SyncHTTPTransport()172173 with pytest.warns(DeprecationWarning) as record:174 client = httpx.Client(dispatch=dispatch)175176 assert client.transport is dispatch177 assert len(record) == 1178 assert record[0].message.args[0] == (179 "The dispatch argument is deprecated since v0.13 and will be "180 "removed in a future release, please use 'transport'"181 )182183184def test_wsgi_dispatch_deprecated():185 with pytest.warns(DeprecationWarning) as record:186 WSGIDispatch(None)187188 assert len(record) == 1189 assert (190 record[0].message.args[0]191 == "WSGIDispatch is deprecated, please use WSGITransport"192 )
modified:tests/client/test_proxies.py+3 added-2 removed
103 unmodified lines
104 None,105 ),106 ],107)108@pytest.mark.parametrize("client_class", [httpx.Client, httpx.AsyncClient])108def test_proxies_environ(monkeypatch, url, env, expected):109def test_proxies_environ(monkeypatch, client_class, url, env, expected):110 for name, value in env.items():111 monkeypatch.setenv(name, value)112112 client = httpx.AsyncClient()113 client = client_class()114 transport = client.transport_for_url(httpx.URL(url))115116 if expected is None:117 assert transport == client.transport
2 unmodified lines
modified:tests/models/test_headers.py+8 added-1 removed
44 unmodified lines
45 assert dict(h) == {"b": "4"}46 assert h.raw == [(b"b", b"4")]474849def test_copy_headers():49def test_copy_headers_method():50 headers = httpx.Headers({"custom": "example"})51 headers_copy = headers.copy()52 assert headers == headers_copy53 assert headers is not headers_copy545556def test_copy_headers_init():57 headers = httpx.Headers({"custom": "example"})58 headers_copy = httpx.Headers(headers)59 assert headers == headers_copy6061
111 unmodified lines
modified:tests/models/test_url.py+5 added
190 unmodified lines
191 assert origin.host == "example.com"192 assert origin.port == 443193194195def test_origin_repr():196 origin = Origin("https://example.com:8080")197 assert str(origin) == "Origin(scheme='https' host='example.com' port=8080)"198199200def test_url_copywith_for_authority():201 copy_with_kwargs = {202 "username": "username",203 "password": "password",
8 unmodified lines
modified:tests/conftest.py-12 removed
74 unmodified lines
75async def app(scope, receive, send):76 assert scope["type"] == "http"77 if scope["path"].startswith("/slow_response"):78 await slow_response(scope, receive, send)79 elif scope["path"].startswith("/premature_close"):80 await premature_close(scope, receive, send)79 elif scope["path"].startswith("/status"):80 await status_code(scope, receive, send)81 elif scope["path"].startswith("/echo_body"):82 await echo_body(scope, receive, send)
27 unmodified lines
110 )111 await send({"type": "http.response.body", "body": b"Hello, world!"})112113116async def premature_close(scope, receive, send):117 await send(118 {119 "type": "http.response.start",120 "status": 200,121 "headers": [[b"content-type", b"text/plain"]],122 }123 )124125114async def status_code(scope, receive, send):115 status_code = int(scope["path"].replace("/status/", ""))116 await send(117 {
176 unmodified lines