Skip to content
Myr

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

4 files+182 added−68 removed

modified:httpcore/models.py+4 added
820 unmodified lines
821 break822823 def close(self) -> None:824 return self._loop.run_until_complete(self._response.close())825826 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
1import pytest23import httpcore456@pytest.mark.asyncio7async 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!"131415@pytest.mark.asyncio16async 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 == 200212223@pytest.mark.asyncio24async 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!"313233@pytest.mark.asyncio34async 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.content404142@pytest.mark.asyncio43async def test_stream_request(server):44 async def hello_world():45 yield b"Hello, "46 yield b"world!"4748 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 == 200535455@pytest.mark.asyncio56async 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 )6263 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 None
added:tests/client/test_async_client.py+113 added
1import pytest23import httpcore456@pytest.mark.asyncio7async 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')>"161718@pytest.mark.asyncio19async 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 == 200242526@pytest.mark.asyncio27async 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!"343536@pytest.mark.asyncio37async 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.content434445@pytest.mark.asyncio46async def test_stream_request(server):47 async def hello_world():48 yield b"Hello, "49 yield b"world!"5051 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 == 200565758@pytest.mark.asyncio59async 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 )6566 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 None717273@pytest.mark.asyncio74async 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!"808182@pytest.mark.asyncio83async 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 == ""899091@pytest.mark.asyncio92async 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 == 200979899@pytest.mark.asyncio100async 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 == 200105106107@pytest.mark.asyncio108async 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 lines
242526@threadpool27def 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')>"414243@threadpool44def test_post(server):
31 unmodified lines
76 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?818283@threadpool84def 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 )9091 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 None969798@threadpool99def 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"104105106@threadpool107def 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"112113114@threadpool115def 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"120121122@threadpool123def 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"128129130@threadpool131def 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"