Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

181 lines · 5.1 KiB · Python

Last changed by

Media types with 'text/' should default to iso-8859-1Tom Christiebefore adoptiond7cf8bb

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

line 18why, one step back
18def test_response_content_type_encoding():

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 19–21why, one step back
19 """
20 Use the charset encoding in the Content-Type header if possible.21 """

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

line 22why, one step back
22 headers = {"Content-Type": "text-plain; charset=latin-1"}

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 23–24why, one step back
23 content = "Latin 1: ÿ".encode("latin-1")
24 response = httpcore.Response(200, content=content, headers=headers)

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 25–29why, one step back
25 assert response.text == "Latin 1: ÿ"
26 assert response.encoding == "latin-1"272829def test_response_autodetect_encoding():

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 30–36why, one step back
30 """
31 Autodetect encoding if there is no charset info in a Content-Type header.32 """33 content = "おはようございます。".encode("EUC-JP")34 response = httpcore.Response(200, content=content)35 assert response.text == "おはようございます。"36 assert response.encoding == "EUC-JP"

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 37–38why, one step back
37
38

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 39–49why, one step back
39def test_response_fallback_to_autodetect():
40 """41 Fallback to autodetection if we get an invalid charset in the Content-Type header.42 """43 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}44 content = "おはようございます。".encode("EUC-JP")45 response = httpcore.Response(200, content=content, headers=headers)46 assert response.text == "おはようございます。"47 assert response.encoding == "EUC-JP"4849

Media types with 'text/' should default to iso-8859-1Tom Christiebefore adoptiond7cf8bb

lines 50–62why, one step back
50def test_response():
51 """52 A media type of 'text/*' with no charset should default to ISO-8859-1.53 See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.154 """55 content = b"Hello, world!"56 headers = {"Content-Type": "text/plain"}57 response = httpcore.Response(200, content=content, headers=headers)58 assert response.status_code == 20059 assert response.encoding == "iso-8859-1"60 assert response.text == "Hello, world!"6162

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

line 63why, one step back
63def test_response_default_encoding():

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 64–66why, one step back
64 """
65 Default to utf-8 if all else fails.66 """

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 67–71why, one step back
67 response = httpcore.Response(200, content=b"")
68 assert response.text == ""69 assert response.encoding == "utf-8"7071

Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e

line 72why, one step back
72def test_response_set_explicit_encoding():

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

lines 73–75why, one step back
73 headers = {
74 "Content-Type": "text-plain; charset=utf-8"75 } # Deliberately incorrect charset

Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e

lines 76–78why, one step back
76 response = httpcore.Response(
77 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers78 )

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

line 79why, one step back
79 response.encoding = "latin-1"

Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e

lines 80–83why, one step back
80 assert response.text == "Latin 1: ÿ"
81 assert response.encoding == "latin-1"8283

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 84–92why, one step back
84def test_response_force_encoding():
85 response = httpcore.Response(200, content="Snowman: ☃".encode("utf-8"))86 response.encoding = "iso-8859-1"87 assert response.status_code == 20088 assert response.reason_phrase == "OK"89 assert response.text == "Snowman: â\x98\x83"90 assert response.encoding == "iso-8859-1"9192

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 96–97why, one step back
96
97 assert response.status_code == 200

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 100–101why, one step back
100 assert response.is_closed
101

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 103why, one step back
103

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 106why, one step back
106 assert response.is_closed

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

lines 107–138why, one step back
107
108109@pytest.mark.asyncio110async def test_raw_interface():111 response = httpcore.Response(200, content=b"Hello, world!")112113 raw = b""114 async for part in response.raw():115 raw += part116 assert raw == b"Hello, world!"117118119@pytest.mark.asyncio120async def test_stream_interface():121 response = httpcore.Response(200, content=b"Hello, world!")122123 content = b""124 async for part in response.stream():125 content += part126 assert content == b"Hello, world!"127128129@pytest.mark.asyncio130async def test_stream_interface_after_read():131 response = httpcore.Response(200, content=b"Hello, world!")132133 await response.read()134135 content = b""136 async for part in response.stream():137 content += part138 assert content == b"Hello, world!"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 139–142why, one step back
139
140141@pytest.mark.asyncio142async def test_streaming_response():

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 144–147why, one step back
144
145 assert response.status_code == 200146 assert not response.is_closed147

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 149why, one step back
149

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 152–156why, one step back
152 assert response.is_closed
153154155@pytest.mark.asyncio156async def test_cannot_read_after_stream_consumed():

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 158why, one step back
158

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 162–168why, one step back
162
163 with pytest.raises(httpcore.StreamConsumed):164 await response.read()165166167@pytest.mark.asyncio168async def test_cannot_read_after_response_closed():

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 170–180why, one step back
170
171 await response.close()172173 with pytest.raises(httpcore.ResponseClosed):174 await response.read()175176177def test_unknown_status_code():178 response = httpcore.Response(600)179 assert response.status_code == 600180 assert response.reason_phrase == ""

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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