Skip to content
Myr

test_client.py

This file

Path

tests/test_client.py

67 lines · 2.0 KiB · Python

Last changed by

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

History of this file

Take it

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 1–11why, one step back
1import pytest
23import httpcore456@pytest.mark.asyncio7async def test_get(server):8 url = "http://127.0.0.1:8000/"9 async with httpcore.Client() as client:10 response = await client.get(url)11 assert response.status_code == 200

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

line 12why, one step back
12 assert response.text == "Hello, world!"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 13–18why, one step back
13
1415@pytest.mark.asyncio16async def test_post(server):17 url = "http://127.0.0.1:8000/"18 async with httpcore.Client() as client:

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

line 19why, one step back
19 response = await client.post(url, data=b"Hello, world!")

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 20–29why, one step back
20 assert response.status_code == 200
212223@pytest.mark.asyncio24async def test_stream_response(server):25 async with httpcore.Client() 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!"

Response.contentTom Christiebefore adoption4c55113

lines 30–39why, one step back
30 assert response.content == b"Hello, world!"
313233@pytest.mark.asyncio34async def test_access_content_stream_response(server):35 async with httpcore.Client() 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.content

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 40–49why, one step back
40
4142@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.Client() as client:49 response = await client.request(

Request(content=...) -> Request(data=...)Tom Christiebefore adoption19542f2

line 50why, one step back
50 "POST", "http://127.0.0.1:8000/", data=hello_world()

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 51–52why, one step back
51 )
52 assert response.status_code == 200

Merge pull request #39 from matusf/29-add-raise_for_statusTom Christiebefore adoption5c1cb30

lines 53–67why, one step back
53
5455@pytest.mark.asyncio56async def test_raise_for_status(server):57 async with httpcore.Client() 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