test_responses.py
This file
Path
tests/test_responses.py
74 lines · 1.7 KiB · Python
Last changed by
Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
History of this fileTake it
Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 1–5why, one step back1import pytest23import httpcore45Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
lines 6–8why, one step back6async def streaming_body():7 yield b"Hello, "8 yield b"world!"Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 9–10why, one step back910Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
lines 11–12why, one step back11def test_response():12 response = httpcore.Response(200, body=b"Hello, world!")Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
line 13why, one step back13 assert response.status_code == 200Add response.reasonTom Christiebefore adoptionfc7cf9d
line 14why, one step back14 assert response.reason == "OK"Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 15–20why, one step back15 assert response.body == b"Hello, world!"16 assert response.is_closed171819@pytest.mark.asyncio20async def test_read_response():Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 21why, one step back21 response = httpcore.Response(200, body=b"Hello, world!")Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 22–34why, one step back2223 assert response.status_code == 20024 assert response.body == b"Hello, world!"25 assert response.is_closed2627 body = await response.read()2829 assert body == b"Hello, world!"30 assert response.body == b"Hello, world!"31 assert response.is_closed323334@pytest.mark.asyncioMerge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
lines 35–36why, one step back35async def test_streaming_response():36 response = httpcore.Response(200, body=streaming_body())Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 37–50why, one step back3738 assert response.status_code == 20039 assert not hasattr(response, "body")40 assert not response.is_closed4142 body = await response.read()4344 assert body == b"Hello, world!"45 assert response.body == b"Hello, world!"46 assert response.is_closed474849@pytest.mark.asyncio50async def test_cannot_read_after_stream_consumed():Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 51why, one step back51 response = httpcore.Response(200, body=streaming_body())Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 52–62why, one step back5253 body = b""54 async for part in response.stream():55 body += part5657 with pytest.raises(httpcore.StreamConsumed):58 await response.read()596061@pytest.mark.asyncio62async def test_cannot_read_after_response_closed():Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b
line 63why, one step back63 response = httpcore.Response(200, body=streaming_body())Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7
lines 64–68why, one step back6465 await response.close()6667 with pytest.raises(httpcore.ResponseClosed):68 await response.read()Add response.reasonTom Christiebefore adoptionfc7cf9d
lines 69–74why, one step back697071def test_unknown_status_code():72 response = httpcore.Response(600)73 assert response.status_code == 60074 assert response.reason == ""