Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

252 lines · 6.7 KiB · Python

Last changed by

Int status codes (#92)Tom Christiebefore adoption56a7943

History of this file

Take it

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 1–2why, one step back
1import pytest
2

http3 (#86)Tom Christiebefore adoptionc9747aa

line 3why, one step back
3import http3

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 4–5why, one step back
4
5

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 6why, one step back
6def streaming_body():

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 7–10why, one step back
7 yield b"Hello, "
8 yield b"world!"910

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 11–15why, one step back
11async def async_streaming_body():
12 yield b"Hello, "13 yield b"world!"1415

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 16why, one step back
16def test_response():

http3 (#86)Tom Christiebefore adoptionc9747aa

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 21–22why, one step back
21
22

Increase test coverageTom Christiebefore adoptione99ffbd

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

http3 (#86)Tom Christiebefore adoptionc9747aa

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

Int status codes (#92)Tom Christiebefore adoption56a7943

line 25why, one step back
25 assert repr(response) == "<Response [200 OK])>"

Increase test coverageTom Christiebefore adoptione99ffbd

lines 26–27why, one step back
26
27

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 29–31why, one step back
29 """
30 Use the charset encoding in the Content-Type header if possible.31 """

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

line 33why, one step back
33 content = "Latin 1: ÿ".encode("latin-1")

http3 (#86)Tom Christiebefore adoptionc9747aa

line 34why, one step back
34 response = http3.Response(200, content=content, headers=headers)

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 35–39why, one step back
35 assert response.text == "Latin 1: ÿ"
36 assert response.encoding == "latin-1"373839def test_response_autodetect_encoding():

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 40–43why, one step back
40 """
41 Autodetect encoding if there is no charset info in a Content-Type header.42 """43 content = "おはようございます。".encode("EUC-JP")

http3 (#86)Tom Christiebefore adoptionc9747aa

line 44why, one step back
44 response = http3.Response(200, content=content)

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 45–46why, one step back
45 assert response.text == "おはようございます。"
46 assert response.encoding == "EUC-JP"

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 47–48why, one step back
47
48

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

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

http3 (#86)Tom Christiebefore adoptionc9747aa

line 55why, one step back
55 response = http3.Response(200, content=content, headers=headers)

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 56–59why, one step back
56 assert response.text == "おはようございます。"
57 assert response.encoding == "EUC-JP"5859

Increase test coverageTom Christiebefore adoptione99ffbd

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

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

lines 61–66why, one step back
61 """
62 A media type of 'text/*' with no charset should default to ISO-8859-1.63 See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.164 """65 content = b"Hello, world!"66 headers = {"Content-Type": "text/plain"}

http3 (#86)Tom Christiebefore adoptionc9747aa

line 67why, one step back
67 response = http3.Response(200, content=content, headers=headers)

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

lines 68–72why, one step back
68 assert response.status_code == 200
69 assert response.encoding == "iso-8859-1"70 assert response.text == "Hello, world!"7172

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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

Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36

lines 74–76why, one step back
74 """
75 Default to utf-8 if all else fails.76 """

http3 (#86)Tom Christiebefore adoptionc9747aa

line 77why, one step back
77 response = http3.Response(200, content=b"")

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 78–79why, one step back
78 assert response.text == ""
79 assert response.encoding == "utf-8"

Increase test coverageTom Christiebefore adoptione99ffbd

lines 80–86why, one step back
80
8182def test_response_non_text_encoding():83 """84 Default to apparent encoding for non-text content-type headers.85 """86 headers = {"Content-Type": "image/png"}

http3 (#86)Tom Christiebefore adoptionc9747aa

line 87why, one step back
87 response = http3.Response(200, content=b"xyz", headers=headers)

Increase test coverageTom Christiebefore adoptione99ffbd

lines 88–89why, one step back
88 assert response.text == "xyz"
89 assert response.encoding == "ascii"

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 90–91why, one step back
90
91

Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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

http3 (#86)Tom Christiebefore adoptionc9747aa

line 96why, one step back
96 response = http3.Response(

Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e

lines 97–98why, one step back
97 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers
98 )

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

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

Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e

lines 100–103why, one step back
100 assert response.text == "Latin 1: ÿ"
101 assert response.encoding == "latin-1"102103

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

line 104why, one step back
104def test_response_force_encoding():

http3 (#86)Tom Christiebefore adoptionc9747aa

line 105why, one step back
105 response = http3.Response(200, content="Snowman: ☃".encode("utf-8"))

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

lines 106–112why, one step back
106 response.encoding = "iso-8859-1"
107 assert response.status_code == 200108 assert response.reason_phrase == "OK"109 assert response.text == "Snowman: â\x98\x83"110 assert response.encoding == "iso-8859-1"111112

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 113why, one step back
113def test_read_response():

http3 (#86)Tom Christiebefore adoptionc9747aa

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 115–116why, one step back
115
116 assert response.status_code == 200

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 119–120why, one step back
119 assert response.is_closed
120

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 122why, one step back
122

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

lines 126–127why, one step back
126
127

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 128why, one step back
128def test_raw_interface():

http3 (#86)Tom Christiebefore adoptionc9747aa

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

lines 130–131why, one step back
130
131 raw = b""

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 132why, one step back
132 for part in response.raw():

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

lines 133–136why, one step back
133 raw += part
134 assert raw == b"Hello, world!"135136

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 137why, one step back
137def test_stream_interface():

http3 (#86)Tom Christiebefore adoptionc9747aa

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

lines 139–140why, one step back
139
140 content = b""

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

lines 142–146why, one step back
142 content += part
143 assert content == b"Hello, world!"144145146@pytest.mark.asyncio

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 147why, one step back
147async def test_async_stream_interface():

http3 (#86)Tom Christiebefore adoptionc9747aa

line 148why, one step back
148 response = http3.AsyncResponse(200, content=b"Hello, world!")

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 149–156why, one step back
149
150 content = b""151 async for part in response.stream():152 content += part153 assert content == b"Hello, world!"154155156def test_stream_interface_after_read():

http3 (#86)Tom Christiebefore adoptionc9747aa

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

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

line 158why, one step back
158

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 159–168why, one step back
159 response.read()
160161 content = b""162 for part in response.stream():163 content += part164 assert content == b"Hello, world!"165166167@pytest.mark.asyncio168async def test_async_stream_interface_after_read():

http3 (#86)Tom Christiebefore adoptionc9747aa

line 169why, one step back
169 response = http3.AsyncResponse(200, content=b"Hello, world!")

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 170why, one step back
170

Use rfc3986 for URL handlingTom Christiebefore adoption302fe93

lines 171–176why, one step back
171 await response.read()
172173 content = b""174 async for part in response.stream():175 content += part176 assert content == b"Hello, world!"

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 177–178why, one step back
177
178

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 179why, one step back
179def test_streaming_response():

http3 (#86)Tom Christiebefore adoptionc9747aa

line 180why, one step back
180 response = http3.Response(200, content=streaming_body())

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 181–184why, one step back
181
182 assert response.status_code == 200183 assert not response.is_closed184

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 186why, one step back
186

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 189–192why, one step back
189 assert response.is_closed
190191192@pytest.mark.asyncio

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 193why, one step back
193async def test_async_streaming_response():

http3 (#86)Tom Christiebefore adoptionc9747aa

line 194why, one step back
194 response = http3.AsyncResponse(200, content=async_streaming_body())

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 195–206why, one step back
195
196 assert response.status_code == 200197 assert not response.is_closed198199 content = await response.read()200201 assert content == b"Hello, world!"202 assert response.content == b"Hello, world!"203 assert response.is_closed204205206def test_cannot_read_after_stream_consumed():

http3 (#86)Tom Christiebefore adoptionc9747aa

line 207why, one step back
207 response = http3.Response(200, content=streaming_body())

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 208why, one step back
208

Response.contentTom Christiebefore adoption4c55113

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

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 210–212why, one step back
210 for part in response.stream():
211 content += part212

http3 (#86)Tom Christiebefore adoptionc9747aa

line 213why, one step back
213 with pytest.raises(http3.StreamConsumed):

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 214–218why, one step back
214 response.read()
215216217@pytest.mark.asyncio218async def test_async_cannot_read_after_stream_consumed():

http3 (#86)Tom Christiebefore adoptionc9747aa

line 219why, one step back
219 response = http3.AsyncResponse(200, content=async_streaming_body())

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 220–221why, one step back
220
221 content = b""

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

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

Response.contentTom Christiebefore adoption4c55113

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

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

line 224why, one step back
224

http3 (#86)Tom Christiebefore adoptionc9747aa

line 225why, one step back
225 with pytest.raises(http3.StreamConsumed):

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 226–228why, one step back
226 await response.read()
227228

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 229why, one step back
229def test_cannot_read_after_response_closed():

http3 (#86)Tom Christiebefore adoptionc9747aa

line 230why, one step back
230 response = http3.Response(200, content=streaming_body())

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 231–233why, one step back
231
232 response.close()233

http3 (#86)Tom Christiebefore adoptionc9747aa

line 234why, one step back
234 with pytest.raises(http3.ResponseClosed):

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 235–239why, one step back
235 response.read()
236237238@pytest.mark.asyncio239async def test_async_cannot_read_after_response_closed():

http3 (#86)Tom Christiebefore adoptionc9747aa

line 240why, one step back
240 response = http3.AsyncResponse(200, content=async_streaming_body())

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 241–243why, one step back
241
242 await response.close()243

http3 (#86)Tom Christiebefore adoptionc9747aa

line 244why, one step back
244 with pytest.raises(http3.ResponseClosed):

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 245–248why, one step back
245 await response.read()
246247248def test_unknown_status_code():

http3 (#86)Tom Christiebefore adoptionc9747aa

line 249why, one step back
249 response = http3.Response(600)

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 250–251why, one step back
250 assert response.status_code == 600
251 assert response.reason_phrase == ""

Add response.text and response.encodingTom Christiebefore adoptiond8df61b

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