Add tests for additional HTTP methods on async client
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Add tests for raise_for_status and SyncResponse properties
authored by Yeray Diaz Diaz · committed by Yeray Diaz Diaz · parent e46ee49 · Browse files · 3 files·+77 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+77 added−9 removed
- tests:+73 added−9 removed
- other:+4 added−0 removed
Provenance
- Commit
fe5a3b8- Tree
2232fb7- Author
Yeray Diaz Diaz <yeraydiazdiaz@gmail.com>- Committer
Yeray Diaz Diaz <yeraydiazdiaz@gmail.com>- Parent
e46ee49
3 files changed+77 added−9 removed
modified:httpcore/models.py+4 added
820 unmodified lines821 break822 823 def close(self) -> None:824 return self._loop.run_until_complete(self._response.close())825+826+ def __repr__(self) -> str:827+ class_name = self.__class__.__name__828+ return f"<{class_name}({self.status_code}, {self.reason_phrase!r})>"modified:tests/client/test_async_client.py+46 added
8 unmodified lines9 async with httpcore.AsyncClient() as client:10 response = await client.get(url)11 assert response.status_code == 20012 assert response.text == "Hello, world!"13+ assert response.protocol == "HTTP/1.1"14+ assert response.headers15+ assert repr(response) == "<Response(200, 'OK')>"16 17 18 @pytest.mark.asyncio19 async def test_post(server):47 unmodified lines67 with pytest.raises(httpcore.exceptions.HttpError):68 response.raise_for_status()69 else:70 assert response.raise_for_status() is None71+72+73+@pytest.mark.asyncio74+async def test_options(server):75+ url = "http://127.0.0.1:8000/"76+ async with httpcore.AsyncClient() as client:77+ response = await client.options(url)78+ assert response.status_code == 20079+ assert response.text == "Hello, world!"80+81+82+@pytest.mark.asyncio83+async def test_head(server):84+ url = "http://127.0.0.1:8000/"85+ async with httpcore.AsyncClient() as client:86+ response = await client.head(url)87+ assert response.status_code == 20088+ assert response.text == ""89+90+91+@pytest.mark.asyncio92+async def test_put(server):93+ url = "http://127.0.0.1:8000/"94+ async with httpcore.AsyncClient() as client:95+ response = await client.put(url, data=b"Hello, world!")96+ assert response.status_code == 20097+98+99+@pytest.mark.asyncio100+async def test_patch(server):101+ url = "http://127.0.0.1:8000/"102+ async with httpcore.AsyncClient() as client:103+ response = await client.patch(url, data=b"Hello, world!")104+ assert response.status_code == 200105+106+107+@pytest.mark.asyncio108+async def test_delete(server):109+ url = "http://127.0.0.1:8000/"110+ async with httpcore.AsyncClient() as client:111+ response = await client.delete(url)112+ assert response.status_code == 200113+ assert response.text == "Hello, world!"modified:tests/client/test_client.py+27 added-9 removed
23 unmodified lines24 25 26 @threadpool27 def test_get(server):28+ url = "http://127.0.0.1:8000/"29 with httpcore.Client() as http:29- response = http.get("http://127.0.0.1:8000/")30+ response = http.get(url)31 assert response.status_code == 20032+ assert response.url == httpcore.URL(url)33 assert response.content == b"Hello, world!"34 assert response.text == "Hello, world!"35 assert response.protocol == "HTTP/1.1"36+ assert response.encoding == "iso-8859-1"37+ assert response.request.url == httpcore.URL(url)38 assert response.headers39+ assert response.is_redirect is False35- assert repr(response) == "<SyncResponse(status_code=200)>"40+ assert repr(response) == "<SyncResponse(200, 'OK')>"41 42 43 @threadpool44 def test_post(server):35 unmodified lines80 response.close() # TODO: should Response be available as context managers?81 82 83 @threadpool84+def test_raise_for_status(server):85+ with httpcore.Client() as client:86+ for status_code in (200, 400, 404, 500, 505):87+ response = client.request(88+ "GET", "http://127.0.0.1:8000/status/{}".format(status_code)89+ )90+91+ if 400 <= status_code < 600:92+ with pytest.raises(httpcore.exceptions.HttpError):93+ response.raise_for_status()94+ else:95+ assert response.raise_for_status() is None96+97+98+@threadpool99 def test_options(server):80- with httpcore.SyncClient() as http:100+ with httpcore.Client() as http:101 response = http.options("http://127.0.0.1:8000/")82-102 assert response.status_code == 200103 assert response.reason_phrase == "OK"104 105 106 @threadpool107 def test_head(server):89- with httpcore.SyncClient() as http:108+ with httpcore.Client() as http:109 response = http.head("http://127.0.0.1:8000/")91-110 assert response.status_code == 200111 assert response.reason_phrase == "OK"112 113 114 @threadpool115 def test_put(server):98- with httpcore.SyncClient() as http:116+ with httpcore.Client() as http:117 response = http.put("http://127.0.0.1:8000/", data=b"Hello, world!")118 assert response.status_code == 200119 assert response.reason_phrase == "OK"120 121 122 @threadpool123 def test_patch(server):106- with httpcore.SyncClient() as http:124+ with httpcore.Client() as http:125 response = http.patch("http://127.0.0.1:8000/", data=b"Hello, world!")126 assert response.status_code == 200127 assert response.reason_phrase == "OK"128 129 130 @threadpool131 def test_delete(server):114- with httpcore.SyncClient() as http:132+ with httpcore.Client() as http:133 response = http.delete("http://127.0.0.1:8000/")134 assert response.status_code == 200135 assert response.reason_phrase == "OK"