Skip to content

Commit 8c84210

Browse files
Increased test coverage & cleanup (#1003)
* 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>
1 parent 89a8100 commit 8c84210

File tree

7 files changed

+81
-17
lines changed

7 files changed

+81
-17
lines changed

docs/api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ True
151151
'application/json'
152152
```
153153

154-
* `def __init__(self, headers)`
154+
* `def __init__(self, headers, encoding=None)`
155+
* `def copy()` - **Headers**
155156

156157
## `Cookies`
157158

tests/client/test_async_client.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from datetime import timedelta
22

3+
import httpcore
34
import pytest
45

56
import httpx
7+
from httpx import ASGIDispatch
68

79

810
@pytest.mark.usefixtures("async_environment")
@@ -18,6 +20,13 @@ async def test_get(server):
1820
assert response.elapsed > timedelta(seconds=0)
1921

2022

23+
@pytest.mark.usefixtures("async_environment")
24+
async 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")
28+
29+
2130
@pytest.mark.usefixtures("async_environment")
2231
async def test_build_request(server):
2332
url = server.url.copy_with(path="/echo_headers")
@@ -148,3 +157,28 @@ async def test_100_continue(server):
148157

149158
assert response.status_code == 200
150159
assert response.content == data
160+
161+
162+
def test_dispatch_deprecated():
163+
dispatch = httpcore.AsyncHTTPTransport()
164+
165+
with pytest.warns(DeprecationWarning) as record:
166+
client = httpx.AsyncClient(dispatch=dispatch)
167+
168+
assert client.transport is dispatch
169+
assert len(record) == 1
170+
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+
)
174+
175+
176+
def test_asgi_dispatch_deprecated():
177+
with pytest.warns(DeprecationWarning) as record:
178+
ASGIDispatch(None)
179+
180+
assert len(record) == 1
181+
assert (
182+
record[0].message.args[0]
183+
== "ASGIDispatch is deprecated, please use ASGITransport"
184+
)

tests/client/test_client.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
from datetime import timedelta
22

3+
import httpcore
34
import pytest
45

56
import httpx
7+
from httpx import WSGIDispatch
68

79

810
def test_get(server):
@@ -103,12 +105,13 @@ def test_raise_for_status(server):
103105
with httpx.Client() as client:
104106
for status_code in (200, 400, 404, 500, 505):
105107
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}")
107109
)
108110
if 400 <= status_code < 600:
109111
with pytest.raises(httpx.HTTPError) as exc_info:
110112
response.raise_for_status()
111113
assert exc_info.value.response == response
114+
assert exc_info.value.request.url.path == f"/status/{status_code}"
112115
else:
113116
assert response.raise_for_status() is None # type: ignore
114117

@@ -162,3 +165,28 @@ def test_merge_url():
162165

163166
assert url.scheme == "https"
164167
assert url.is_ssl
168+
169+
170+
def test_dispatch_deprecated():
171+
dispatch = httpcore.SyncHTTPTransport()
172+
173+
with pytest.warns(DeprecationWarning) as record:
174+
client = httpx.Client(dispatch=dispatch)
175+
176+
assert client.transport is dispatch
177+
assert len(record) == 1
178+
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+
)
182+
183+
184+
def test_wsgi_dispatch_deprecated():
185+
with pytest.warns(DeprecationWarning) as record:
186+
WSGIDispatch(None)
187+
188+
assert len(record) == 1
189+
assert (
190+
record[0].message.args[0]
191+
== "WSGIDispatch is deprecated, please use WSGITransport"
192+
)

tests/client/test_proxies.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,12 @@ def test_unsupported_proxy_scheme():
105105
),
106106
],
107107
)
108-
def test_proxies_environ(monkeypatch, url, env, expected):
108+
@pytest.mark.parametrize("client_class", [httpx.Client, httpx.AsyncClient])
109+
def test_proxies_environ(monkeypatch, client_class, url, env, expected):
109110
for name, value in env.items():
110111
monkeypatch.setenv(name, value)
111112

112-
client = httpx.AsyncClient()
113+
client = client_class()
113114
transport = client.transport_for_url(httpx.URL(url))
114115

115116
if expected is None:

tests/conftest.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ async def app(scope, receive, send):
7676
assert scope["type"] == "http"
7777
if scope["path"].startswith("/slow_response"):
7878
await slow_response(scope, receive, send)
79-
elif scope["path"].startswith("/premature_close"):
80-
await premature_close(scope, receive, send)
8179
elif scope["path"].startswith("/status"):
8280
await status_code(scope, receive, send)
8381
elif scope["path"].startswith("/echo_body"):
@@ -113,16 +111,6 @@ async def slow_response(scope, receive, send):
113111
await send({"type": "http.response.body", "body": b"Hello, world!"})
114112

115113

116-
async 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-
)
124-
125-
126114
async def status_code(scope, receive, send):
127115
status_code = int(scope["path"].replace("/status/", ""))
128116
await send(

tests/models/test_headers.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ def test_header_mutations():
4646
assert h.raw == [(b"b", b"4")]
4747

4848

49-
def test_copy_headers():
49+
def test_copy_headers_method():
50+
headers = httpx.Headers({"custom": "example"})
51+
headers_copy = headers.copy()
52+
assert headers == headers_copy
53+
assert headers is not headers_copy
54+
55+
56+
def test_copy_headers_init():
5057
headers = httpx.Headers({"custom": "example"})
5158
headers_copy = httpx.Headers(headers)
5259
assert headers == headers_copy

tests/models/test_url.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,11 @@ def test_origin_from_url_string():
192192
assert origin.port == 443
193193

194194

195+
def test_origin_repr():
196+
origin = Origin("https://example.com:8080")
197+
assert str(origin) == "Origin(scheme='https' host='example.com' port=8080)"
198+
199+
195200
def test_url_copywith_for_authority():
196201
copy_with_kwargs = {
197202
"username": "username",

0 commit comments

Comments
 (0)