Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

104 lines · 2.8 KiB · Python

Last changed by

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

History of this file

Take it

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 1–11why, one step back
1import pytest
23import httpcore456async def streaming_body():7 yield b"Hello, "8 yield b"world!"91011def test_response():

Response.contentTom Christiebefore adoption4c55113

line 12why, one step back
12 response = httpcore.Response(200, content=b"Hello, world!")

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 13–14why, one step back
13 assert response.status_code == 200
14 assert response.reason_phrase == "OK"

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 16–17why, one step back
16
17

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 18–47why, one step back
18def test_response_content_type_encoding():
19 headers = {"Content-Type": "text-plain; charset=latin-1"}20 response = httpcore.Response(21 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers22 )23 assert response.text == "Latin 1: ÿ"24 assert response.encoding == "latin-1"252627def test_response_autodetect_encoding():28 response = httpcore.Response(200, content="Snowmen: ☃☃☃".encode("utf-8"))29 assert response.text == "Snowmen: ☃☃☃"30 assert response.encoding == "utf-8"313233def test_response_default_encoding():34 response = httpcore.Response(200, content=b"")35 assert response.text == ""36 assert response.encoding == "utf-8"373839def test_response_force_encoding():40 response = httpcore.Response(200, content="Snowman: ☃".encode("utf-8"))41 response.encoding = "iso-8859-1"42 assert response.status_code == 20043 assert response.reason_phrase == "OK"44 assert response.text == "Snowman: â\x98\x83"45 assert response.encoding == "iso-8859-1"4647

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 48–49why, one step back
48@pytest.mark.asyncio
49async def test_read_response():

Response.contentTom Christiebefore adoption4c55113

line 50why, one step back
50 response = httpcore.Response(200, content=b"Hello, world!")

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 53–54why, one step back
53 assert response.text == "Hello, world!"
54 assert response.encoding == "ascii"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 55–56why, one step back
55 assert response.is_closed
56

Response.contentTom Christiebefore adoption4c55113

line 57why, one step back
57 content = await response.read()

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 58why, one step back
58

Response.contentTom Christiebefore adoption4c55113

lines 59–60why, one step back
59 assert content == b"Hello, world!"
60 assert response.content == b"Hello, world!"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 61–65why, one step back
61 assert response.is_closed
626364@pytest.mark.asyncio65async def test_streaming_response():

Response.contentTom Christiebefore adoption4c55113

line 66why, one step back
66 response = httpcore.Response(200, content=streaming_body())

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 67–70why, one step back
67
68 assert response.status_code == 20069 assert not response.is_closed70

Response.contentTom Christiebefore adoption4c55113

line 71why, one step back
71 content = await response.read()

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 72why, one step back
72

Response.contentTom Christiebefore adoption4c55113

lines 73–74why, one step back
73 assert content == b"Hello, world!"
74 assert response.content == b"Hello, world!"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 75–79why, one step back
75 assert response.is_closed
767778@pytest.mark.asyncio79async def test_cannot_read_after_stream_consumed():

Response.contentTom Christiebefore adoption4c55113

line 80why, one step back
80 response = httpcore.Response(200, content=streaming_body())

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 81why, one step back
81

Response.contentTom Christiebefore adoption4c55113

line 82why, one step back
82 content = b""

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 83why, one step back
83 async for part in response.stream():

Response.contentTom Christiebefore adoption4c55113

line 84why, one step back
84 content += part

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 85–91why, one step back
85
86 with pytest.raises(httpcore.StreamConsumed):87 await response.read()888990@pytest.mark.asyncio91async def test_cannot_read_after_response_closed():

Response.contentTom Christiebefore adoption4c55113

line 92why, one step back
92 response = httpcore.Response(200, content=streaming_body())

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 93–103why, one step back
93
94 await response.close()9596 with pytest.raises(httpcore.ResponseClosed):97 await response.read()9899100def test_unknown_status_code():101 response = httpcore.Response(600)102 assert response.status_code == 600103 assert response.reason_phrase == ""

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

line 104why, one step back
104 assert response.text == ""