From: Yeray Diaz Diaz Date: Mon, 13 May 2019 20:01:13 +0000 (+0100) Subject: Add tests for additional HTTP methods on async client X-Git-Tag: 0.3.0~17^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fe5a3b875b6f10a2f7769c791c8a85ba1ed18b8b;p=thirdparty%2Fhttpx.git Add tests for additional HTTP methods on async client Add tests for raise_for_status and SyncResponse properties --- diff --git a/httpcore/models.py b/httpcore/models.py index b05dd814..421f19e8 100644 --- a/httpcore/models.py +++ b/httpcore/models.py @@ -822,3 +822,7 @@ class SyncResponse: def close(self) -> None: return self._loop.run_until_complete(self._response.close()) + + def __repr__(self) -> str: + class_name = self.__class__.__name__ + return f"<{class_name}({self.status_code}, {self.reason_phrase!r})>" diff --git a/tests/client/test_async_client.py b/tests/client/test_async_client.py index 21368ad6..27602d12 100644 --- a/tests/client/test_async_client.py +++ b/tests/client/test_async_client.py @@ -10,6 +10,9 @@ async def test_get(server): response = await client.get(url) assert response.status_code == 200 assert response.text == "Hello, world!" + assert response.protocol == "HTTP/1.1" + assert response.headers + assert repr(response) == "" @pytest.mark.asyncio @@ -65,3 +68,46 @@ async def test_raise_for_status(server): response.raise_for_status() else: assert response.raise_for_status() is None + + +@pytest.mark.asyncio +async def test_options(server): + url = "http://127.0.0.1:8000/" + async with httpcore.AsyncClient() as client: + response = await client.options(url) + assert response.status_code == 200 + assert response.text == "Hello, world!" + + +@pytest.mark.asyncio +async def test_head(server): + url = "http://127.0.0.1:8000/" + async with httpcore.AsyncClient() as client: + response = await client.head(url) + assert response.status_code == 200 + assert response.text == "" + + +@pytest.mark.asyncio +async def test_put(server): + url = "http://127.0.0.1:8000/" + async with httpcore.AsyncClient() as client: + response = await client.put(url, data=b"Hello, world!") + assert response.status_code == 200 + + +@pytest.mark.asyncio +async def test_patch(server): + url = "http://127.0.0.1:8000/" + async with httpcore.AsyncClient() as client: + response = await client.patch(url, data=b"Hello, world!") + assert response.status_code == 200 + + +@pytest.mark.asyncio +async def test_delete(server): + url = "http://127.0.0.1:8000/" + async with httpcore.AsyncClient() as client: + response = await client.delete(url) + assert response.status_code == 200 + assert response.text == "Hello, world!" diff --git a/tests/client/test_client.py b/tests/client/test_client.py index 29587b4b..76f2a5bd 100644 --- a/tests/client/test_client.py +++ b/tests/client/test_client.py @@ -25,14 +25,19 @@ def threadpool(func): @threadpool def test_get(server): + url = "http://127.0.0.1:8000/" with httpcore.Client() as http: - response = http.get("http://127.0.0.1:8000/") + response = http.get(url) assert response.status_code == 200 + assert response.url == httpcore.URL(url) assert response.content == b"Hello, world!" assert response.text == "Hello, world!" assert response.protocol == "HTTP/1.1" + assert response.encoding == "iso-8859-1" + assert response.request.url == httpcore.URL(url) assert response.headers - assert repr(response) == "" + assert response.is_redirect is False + assert repr(response) == "" @threadpool @@ -75,27 +80,40 @@ def test_raw_iterator(server): response.close() # TODO: should Response be available as context managers? +@threadpool +def test_raise_for_status(server): + with httpcore.Client() as client: + for status_code in (200, 400, 404, 500, 505): + response = client.request( + "GET", "http://127.0.0.1:8000/status/{}".format(status_code) + ) + + if 400 <= status_code < 600: + with pytest.raises(httpcore.exceptions.HttpError): + response.raise_for_status() + else: + assert response.raise_for_status() is None + + @threadpool def test_options(server): - with httpcore.SyncClient() as http: + with httpcore.Client() as http: response = http.options("http://127.0.0.1:8000/") - assert response.status_code == 200 assert response.reason_phrase == "OK" @threadpool def test_head(server): - with httpcore.SyncClient() as http: + with httpcore.Client() as http: response = http.head("http://127.0.0.1:8000/") - assert response.status_code == 200 assert response.reason_phrase == "OK" @threadpool def test_put(server): - with httpcore.SyncClient() as http: + with httpcore.Client() as http: response = http.put("http://127.0.0.1:8000/", data=b"Hello, world!") assert response.status_code == 200 assert response.reason_phrase == "OK" @@ -103,7 +121,7 @@ def test_put(server): @threadpool def test_patch(server): - with httpcore.SyncClient() as http: + with httpcore.Client() as http: response = http.patch("http://127.0.0.1:8000/", data=b"Hello, world!") assert response.status_code == 200 assert response.reason_phrase == "OK" @@ -111,7 +129,7 @@ def test_patch(server): @threadpool def test_delete(server): - with httpcore.SyncClient() as http: + with httpcore.Client() as http: response = http.delete("http://127.0.0.1:8000/") assert response.status_code == 200 assert response.reason_phrase == "OK"