Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

196 lines · 5.5 KiB · Python

Last changed by

Increase test coverageTom Christiebefore adoptione99ffbd

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

Increase test coverageTom Christiebefore adoptione99ffbd

lines 18–22why, one step back
18def test_response_repr():
19 response = httpcore.Response(200, content=b"Hello, world!")20 assert repr(response) == "<Response(200, 'OK')>"2122

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 24–26why, one step back
24 """
25 Use the charset encoding in the Content-Type header if possible.26 """

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

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

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 30–34why, one step back
30 assert response.text == "Latin 1: ÿ"
31 assert response.encoding == "latin-1"323334def test_response_autodetect_encoding():

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

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

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 42–43why, one step back
42
43

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

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

Increase test coverageTom Christiebefore adoptione99ffbd

line 55why, one step back
55def test_response_default_text_encoding():

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

lines 56–67why, one step back
56 """
57 A media type of 'text/*' with no charset should default to ISO-8859-1.58 See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.159 """60 content = b"Hello, world!"61 headers = {"Content-Type": "text/plain"}62 response = httpcore.Response(200, content=content, headers=headers)63 assert response.status_code == 20064 assert response.encoding == "iso-8859-1"65 assert response.text == "Hello, world!"6667

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 69–71why, one step back
69 """
70 Default to utf-8 if all else fails.71 """

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 72–74why, one step back
72 response = httpcore.Response(200, content=b"")
73 assert response.text == ""74 assert response.encoding == "utf-8"

Increase test coverageTom Christiebefore adoptione99ffbd

lines 75–84why, one step back
75
7677def test_response_non_text_encoding():78 """79 Default to apparent encoding for non-text content-type headers.80 """81 headers = {"Content-Type": "image/png"}82 response = httpcore.Response(200, content=b"xyz", headers=headers)83 assert response.text == "xyz"84 assert response.encoding == "ascii"

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 85–86why, one step back
85
86

Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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

Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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

Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e

lines 95–98why, one step back
95 assert response.text == "Latin 1: ÿ"
96 assert response.encoding == "latin-1"9798

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 99–107why, one step back
99def test_response_force_encoding():
100 response = httpcore.Response(200, content="Snowman: ☃".encode("utf-8"))101 response.encoding = "iso-8859-1"102 assert response.status_code == 200103 assert response.reason_phrase == "OK"104 assert response.text == "Snowman: â\x98\x83"105 assert response.encoding == "iso-8859-1"106107

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 111–112why, one step back
111
112 assert response.status_code == 200

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 115–116why, one step back
115 assert response.is_closed
116

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 118why, one step back
118

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

lines 122–153why, one step back
122
123124@pytest.mark.asyncio125async def test_raw_interface():126 response = httpcore.Response(200, content=b"Hello, world!")127128 raw = b""129 async for part in response.raw():130 raw += part131 assert raw == b"Hello, world!"132133134@pytest.mark.asyncio135async def test_stream_interface():136 response = httpcore.Response(200, content=b"Hello, world!")137138 content = b""139 async for part in response.stream():140 content += part141 assert content == b"Hello, world!"142143144@pytest.mark.asyncio145async def test_stream_interface_after_read():146 response = httpcore.Response(200, content=b"Hello, world!")147148 await response.read()149150 content = b""151 async for part in response.stream():152 content += part153 assert content == b"Hello, world!"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 154–157why, one step back
154
155156@pytest.mark.asyncio157async def test_streaming_response():

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 159–162why, one step back
159
160 assert response.status_code == 200161 assert not response.is_closed162

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 164why, one step back
164

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 167–171why, one step back
167 assert response.is_closed
168169170@pytest.mark.asyncio171async def test_cannot_read_after_stream_consumed():

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 173why, one step back
173

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 177–183why, one step back
177
178 with pytest.raises(httpcore.StreamConsumed):179 await response.read()180181182@pytest.mark.asyncio183async def test_cannot_read_after_response_closed():

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 185–195why, one step back
185
186 await response.close()187188 with pytest.raises(httpcore.ResponseClosed):189 await response.read()190191192def test_unknown_status_code():193 response = httpcore.Response(600)194 assert response.status_code == 600195 assert response.reason_phrase == ""

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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