Merge pull request #56 from yeraydiazdiaz/test-additional-http-methods
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Additional tests for SyncClient
authored by Tom Christie · committed by GitHub · parents c27c635, fe5a3b8 · Browse files · 4 files·+182 added−68 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+182 added−68 removed
- tests:+178 added−68 removed
- other:+4 added−0 removed
Provenance
- Commit
87cab4c- Tree
4a98f71- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent 1
c27c635- Parent 2
fe5a3b8
4 files changed+182 added−68 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})>"deleted:tests/client/test_asnyc_client.py-67 removed
1-import pytest2-3-import httpcore4-5-6-@pytest.mark.asyncio7-async def test_get(server):8- url = "http://127.0.0.1:8000/"9- async with httpcore.AsyncClient() as client:10- response = await client.get(url)11- assert response.status_code == 20012- assert response.text == "Hello, world!"13-14-15-@pytest.mark.asyncio16-async def test_post(server):17- url = "http://127.0.0.1:8000/"18- async with httpcore.AsyncClient() as client:19- response = await client.post(url, data=b"Hello, world!")20- assert response.status_code == 20021-22-23-@pytest.mark.asyncio24-async def test_stream_response(server):25- async with httpcore.AsyncClient() as client:26- response = await client.request("GET", "http://127.0.0.1:8000/", stream=True)27- assert response.status_code == 20028- body = await response.read()29- assert body == b"Hello, world!"30- assert response.content == b"Hello, world!"31-32-33-@pytest.mark.asyncio34-async def test_access_content_stream_response(server):35- async with httpcore.AsyncClient() as client:36- response = await client.request("GET", "http://127.0.0.1:8000/", stream=True)37- assert response.status_code == 20038- with pytest.raises(httpcore.ResponseNotRead):39- response.content40-41-42-@pytest.mark.asyncio43-async def test_stream_request(server):44- async def hello_world():45- yield b"Hello, "46- yield b"world!"47-48- async with httpcore.AsyncClient() as client:49- response = await client.request(50- "POST", "http://127.0.0.1:8000/", data=hello_world()51- )52- assert response.status_code == 20053-54-55-@pytest.mark.asyncio56-async def test_raise_for_status(server):57- async with httpcore.AsyncClient() as client:58- for status_code in (200, 400, 404, 500, 505):59- response = await client.request(60- "GET", f"http://127.0.0.1:8000/status/{status_code}"61- )62-63- if 400 <= status_code < 600:64- with pytest.raises(httpcore.exceptions.HttpError):65- response.raise_for_status()66- else:67- assert response.raise_for_status() is Noneadded:tests/client/test_async_client.py+113 added
1+import pytest2+3+import httpcore4+5+6+@pytest.mark.asyncio7+async def test_get(server):8+ url = "http://127.0.0.1:8000/"9+ 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):20+ url = "http://127.0.0.1:8000/"21+ async with httpcore.AsyncClient() as client:22+ response = await client.post(url, data=b"Hello, world!")23+ assert response.status_code == 20024+25+26+@pytest.mark.asyncio27+async def test_stream_response(server):28+ async with httpcore.AsyncClient() as client:29+ response = await client.request("GET", "http://127.0.0.1:8000/", stream=True)30+ assert response.status_code == 20031+ body = await response.read()32+ assert body == b"Hello, world!"33+ assert response.content == b"Hello, world!"34+35+36+@pytest.mark.asyncio37+async def test_access_content_stream_response(server):38+ async with httpcore.AsyncClient() as client:39+ response = await client.request("GET", "http://127.0.0.1:8000/", stream=True)40+ assert response.status_code == 20041+ with pytest.raises(httpcore.ResponseNotRead):42+ response.content43+44+45+@pytest.mark.asyncio46+async def test_stream_request(server):47+ async def hello_world():48+ yield b"Hello, "49+ yield b"world!"50+51+ async with httpcore.AsyncClient() as client:52+ response = await client.request(53+ "POST", "http://127.0.0.1:8000/", data=hello_world()54+ )55+ assert response.status_code == 20056+57+58+@pytest.mark.asyncio59+async def test_raise_for_status(server):60+ async with httpcore.AsyncClient() as client:61+ for status_code in (200, 400, 404, 500, 505):62+ response = await client.request(63+ "GET", f"http://127.0.0.1:8000/status/{status_code}"64+ )65+66+ if 400 <= status_code < 600:67+ 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+65 added-1 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 False40+ assert repr(response) == "<SyncResponse(200, 'OK')>"41 42 43 @threadpool44 def test_post(server):31 unmodified lines76 body = b""77 for chunk in response.raw():78 body += chunk79 assert body == b"Hello, world!"80+ 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):100+ with httpcore.Client() as http:101+ response = http.options("http://127.0.0.1:8000/")102+ assert response.status_code == 200103+ assert response.reason_phrase == "OK"104+105+106+@threadpool107+def test_head(server):108+ with httpcore.Client() as http:109+ response = http.head("http://127.0.0.1:8000/")110+ assert response.status_code == 200111+ assert response.reason_phrase == "OK"112+113+114+@threadpool115+def test_put(server):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):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):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"