Skip to content

Commit

Permalink
Increased test coverage & cleanup (#1003)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
jcugat and florimondmanca authored May 30, 2020
1 parent 89a8100 commit 8c84210
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 17 deletions.
3 changes: 2 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ True
'application/json'
```

* `def __init__(self, headers)`
* `def __init__(self, headers, encoding=None)`
* `def copy()` - **Headers**

## `Cookies`

Expand Down
34 changes: 34 additions & 0 deletions tests/client/test_async_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from datetime import timedelta

import httpcore
import pytest

import httpx
from httpx import ASGIDispatch


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


@pytest.mark.usefixtures("async_environment")
async def test_get_invalid_url(server):
async with httpx.AsyncClient() as client:
with pytest.raises(httpx.InvalidURL):
await client.get("invalid://example.org")


@pytest.mark.usefixtures("async_environment")
async def test_build_request(server):
url = server.url.copy_with(path="/echo_headers")
Expand Down Expand Up @@ -148,3 +157,28 @@ async def test_100_continue(server):

assert response.status_code == 200
assert response.content == data


def test_dispatch_deprecated():
dispatch = httpcore.AsyncHTTPTransport()

with pytest.warns(DeprecationWarning) as record:
client = httpx.AsyncClient(dispatch=dispatch)

assert client.transport is dispatch
assert len(record) == 1
assert record[0].message.args[0] == (
"The dispatch argument is deprecated since v0.13 and will be "
"removed in a future release, please use 'transport'"
)


def test_asgi_dispatch_deprecated():
with pytest.warns(DeprecationWarning) as record:
ASGIDispatch(None)

assert len(record) == 1
assert (
record[0].message.args[0]
== "ASGIDispatch is deprecated, please use ASGITransport"
)
30 changes: 29 additions & 1 deletion tests/client/test_client.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from datetime import timedelta

import httpcore
import pytest

import httpx
from httpx import WSGIDispatch


def test_get(server):
Expand Down Expand Up @@ -103,12 +105,13 @@ def test_raise_for_status(server):
with httpx.Client() as client:
for status_code in (200, 400, 404, 500, 505):
response = client.request(
"GET", server.url.copy_with(path="/status/{}".format(status_code))
"GET", server.url.copy_with(path=f"/status/{status_code}")
)
if 400 <= status_code < 600:
with pytest.raises(httpx.HTTPError) as exc_info:
response.raise_for_status()
assert exc_info.value.response == response
assert exc_info.value.request.url.path == f"/status/{status_code}"
else:
assert response.raise_for_status() is None # type: ignore

Expand Down Expand Up @@ -162,3 +165,28 @@ def test_merge_url():

assert url.scheme == "https"
assert url.is_ssl


def test_dispatch_deprecated():
dispatch = httpcore.SyncHTTPTransport()

with pytest.warns(DeprecationWarning) as record:
client = httpx.Client(dispatch=dispatch)

assert client.transport is dispatch
assert len(record) == 1
assert record[0].message.args[0] == (
"The dispatch argument is deprecated since v0.13 and will be "
"removed in a future release, please use 'transport'"
)


def test_wsgi_dispatch_deprecated():
with pytest.warns(DeprecationWarning) as record:
WSGIDispatch(None)

assert len(record) == 1
assert (
record[0].message.args[0]
== "WSGIDispatch is deprecated, please use WSGITransport"
)
5 changes: 3 additions & 2 deletions tests/client/test_proxies.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,12 @@ def test_unsupported_proxy_scheme():
),
],
)
def test_proxies_environ(monkeypatch, url, env, expected):
@pytest.mark.parametrize("client_class", [httpx.Client, httpx.AsyncClient])
def test_proxies_environ(monkeypatch, client_class, url, env, expected):
for name, value in env.items():
monkeypatch.setenv(name, value)

client = httpx.AsyncClient()
client = client_class()
transport = client.transport_for_url(httpx.URL(url))

if expected is None:
Expand Down
12 changes: 0 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ async def app(scope, receive, send):
assert scope["type"] == "http"
if scope["path"].startswith("/slow_response"):
await slow_response(scope, receive, send)
elif scope["path"].startswith("/premature_close"):
await premature_close(scope, receive, send)
elif scope["path"].startswith("/status"):
await status_code(scope, receive, send)
elif scope["path"].startswith("/echo_body"):
Expand Down Expand Up @@ -113,16 +111,6 @@ async def slow_response(scope, receive, send):
await send({"type": "http.response.body", "body": b"Hello, world!"})


async def premature_close(scope, receive, send):
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [[b"content-type", b"text/plain"]],
}
)


async def status_code(scope, receive, send):
status_code = int(scope["path"].replace("/status/", ""))
await send(
Expand Down
9 changes: 8 additions & 1 deletion tests/models/test_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ def test_header_mutations():
assert h.raw == [(b"b", b"4")]


def test_copy_headers():
def test_copy_headers_method():
headers = httpx.Headers({"custom": "example"})
headers_copy = headers.copy()
assert headers == headers_copy
assert headers is not headers_copy


def test_copy_headers_init():
headers = httpx.Headers({"custom": "example"})
headers_copy = httpx.Headers(headers)
assert headers == headers_copy
Expand Down
5 changes: 5 additions & 0 deletions tests/models/test_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ def test_origin_from_url_string():
assert origin.port == 443


def test_origin_repr():
origin = Origin("https://example.com:8080")
assert str(origin) == "Origin(scheme='https' host='example.com' port=8080)"


def test_url_copywith_for_authority():
copy_with_kwargs = {
"username": "username",
Expand Down

0 comments on commit 8c84210

Please sign in to comment.