Skip to content
Myr

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 file

Take it

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 1–5why, one step back
1import pytest
23import httpcore45

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 6–8why, one step back
6async 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 back
9
10

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 11–12why, one step back
11def 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 back
13 assert response.status_code == 200

Add response.reasonTom Christiebefore adoptionfc7cf9d

line 14why, one step back
14 assert response.reason == "OK"

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 15–20why, one step back
15 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 back
21 response = httpcore.Response(200, body=b"Hello, world!")

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 22–34why, one step back
22
23 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.asyncio

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

lines 35–36why, one step back
35async 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 back
37
38 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 back
51 response = httpcore.Response(200, body=streaming_body())

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 52–62why, one step back
52
53 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 back
63 response = httpcore.Response(200, body=streaming_body())

Merge pull request #1 from encode/add-connectionsTom Christiebefore adoption7626fe7

lines 64–68why, one step back
64
65 await response.close()6667 with pytest.raises(httpcore.ResponseClosed):68 await response.read()

Add response.reasonTom Christiebefore adoptionfc7cf9d

lines 69–74why, one step back
69
7071def test_unknown_status_code():72 response = httpcore.Response(600)73 assert response.status_code == 60074 assert response.reason == ""