test_responses.py
This file
Path
tests/models/test_responses.py
252 lines · 6.7 KiB · Python
Last changed by
http3 (#86)Tom Christiebefore adoptionc9747aa
History of this fileTake it
Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 1–2why, one step back1import pytest2http3 (#86)Tom Christiebefore adoptionc9747aa
line 3why, one step back3import http3Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 4–5why, one step back45Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 6why, one step back6def streaming_body():Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 7–10why, one step back7 yield b"Hello, "8 yield b"world!"910Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 11–15why, one step back11async def async_streaming_body():12 yield b"Hello, "13 yield b"world!"1415Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 16why, one step back16def test_response():http3 (#86)Tom Christiebefore adoptionc9747aa
line 17why, one step back17 response = http3.Response(200, content=b"Hello, world!")Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 18–19why, one step back18 assert response.status_code == 20019 assert response.reason_phrase == "OK"Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 20why, one step back20 assert response.text == "Hello, world!"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 21–22why, one step back2122Increase test coverageTom Christiebefore adoptione99ffbd
line 23why, one step back23def test_response_repr():http3 (#86)Tom Christiebefore adoptionc9747aa
line 24why, one step back24 response = http3.Response(200, content=b"Hello, world!")Increase test coverageTom Christiebefore adoptione99ffbd
lines 25–27why, one step back25 assert repr(response) == "<Response(200, 'OK')>"2627Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 28why, one step back28def test_response_content_type_encoding():Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 29–31why, one step back29 """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 back32 headers = {"Content-Type": "text-plain; charset=latin-1"}Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
line 33why, one step back33 content = "Latin 1: ÿ".encode("latin-1")http3 (#86)Tom Christiebefore adoptionc9747aa
line 34why, one step back34 response = http3.Response(200, content=content, headers=headers)Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 35–39why, one step back35 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 back40 """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 back44 response = http3.Response(200, content=content)Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 45–46why, one step back45 assert response.text == "おはようございます。"46 assert response.encoding == "EUC-JP"Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 47–48why, one step back4748Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 49–54why, one step back49def 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 back55 response = http3.Response(200, content=content, headers=headers)Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 56–59why, one step back56 assert response.text == "おはようございます。"57 assert response.encoding == "EUC-JP"5859Increase test coverageTom Christiebefore adoptione99ffbd
line 60why, one step back60def test_response_default_text_encoding():Media types with 'text/' should default to iso-8859-1Tom Christiebefore adoptiond7cf8bb
lines 61–66why, one step back61 """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 back67 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 back68 assert response.status_code == 20069 assert response.encoding == "iso-8859-1"70 assert response.text == "Hello, world!"7172Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 73why, one step back73def test_response_default_encoding():Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 74–76why, one step back74 """75 Default to utf-8 if all else fails.76 """http3 (#86)Tom Christiebefore adoptionc9747aa
line 77why, one step back77 response = http3.Response(200, content=b"")Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 78–79why, one step back78 assert response.text == ""79 assert response.encoding == "utf-8"Increase test coverageTom Christiebefore adoptione99ffbd
lines 80–86why, one step back808182def 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 back87 response = http3.Response(200, content=b"xyz", headers=headers)Increase test coverageTom Christiebefore adoptione99ffbd
lines 88–89why, one step back88 assert response.text == "xyz"89 assert response.encoding == "ascii"Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 90–91why, one step back9091Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e
line 92why, one step back92def test_response_set_explicit_encoding():Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 93–95why, one step back93 headers = {94 "Content-Type": "text-plain; charset=utf-8"95 } # Deliberately incorrect charsethttp3 (#86)Tom Christiebefore adoptionc9747aa
line 96why, one step back96 response = http3.Response(Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e
lines 97–98why, one step back97 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers98 )Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
line 99why, one step back99 response.encoding = "latin-1"Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e
lines 100–103why, one step back100 assert response.text == "Latin 1: ÿ"101 assert response.encoding == "latin-1"102103Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 104why, one step back104def test_response_force_encoding():http3 (#86)Tom Christiebefore adoptionc9747aa
line 105why, one step back105 response = http3.Response(200, content="Snowman: ☃".encode("utf-8"))Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 106–112why, one step back106 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"111112Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 113why, one step back113def test_read_response():http3 (#86)Tom Christiebefore adoptionc9747aa
line 114why, one step back114 response = http3.Response(200, content=b"Hello, world!")Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 115–116why, one step back115116 assert response.status_code == 200Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 117–118why, one step back117 assert response.text == "Hello, world!"118 assert response.encoding == "ascii"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 119–120why, one step back119 assert response.is_closed120Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 121why, one step back121 content = response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 122why, one step back122Response.contentTom Christiebefore adoption4c55113
lines 123–124why, one step back123 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 back125 assert response.is_closedUse rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 126–127why, one step back126127Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 128why, one step back128def test_raw_interface():http3 (#86)Tom Christiebefore adoptionc9747aa
line 129why, one step back129 response = http3.Response(200, content=b"Hello, world!")Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 130–131why, one step back130131 raw = b""Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 132why, one step back132 for part in response.raw():Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 133–136why, one step back133 raw += part134 assert raw == b"Hello, world!"135136Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 137why, one step back137def test_stream_interface():http3 (#86)Tom Christiebefore adoptionc9747aa
line 138why, one step back138 response = http3.Response(200, content=b"Hello, world!")Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 139–140why, one step back139140 content = b""Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 141why, one step back141 for part in response.stream():Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 142–146why, one step back142 content += part143 assert content == b"Hello, world!"144145146@pytest.mark.asyncioSync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 147why, one step back147async def test_async_stream_interface():http3 (#86)Tom Christiebefore adoptionc9747aa
line 148why, one step back148 response = http3.AsyncResponse(200, content=b"Hello, world!")Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 149–156why, one step back149150 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 back157 response = http3.Response(200, content=b"Hello, world!")Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
line 158why, one step back158Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 159–168why, one step back159 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 back169 response = http3.AsyncResponse(200, content=b"Hello, world!")Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 170why, one step back170Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 171–176why, one step back171 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 back177178Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 179why, one step back179def test_streaming_response():http3 (#86)Tom Christiebefore adoptionc9747aa
line 180why, one step back180 response = http3.Response(200, content=streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 181–184why, one step back181182 assert response.status_code == 200183 assert not response.is_closed184Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 185why, one step back185 content = response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 186why, one step back186Response.contentTom Christiebefore adoption4c55113
lines 187–188why, one step back187 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 back189 assert response.is_closed190191192@pytest.mark.asyncioSync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 193why, one step back193async def test_async_streaming_response():http3 (#86)Tom Christiebefore adoptionc9747aa
line 194why, one step back194 response = http3.AsyncResponse(200, content=async_streaming_body())Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 195–206why, one step back195196 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 back207 response = http3.Response(200, content=streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 208why, one step back208Response.contentTom Christiebefore adoption4c55113
line 209why, one step back209 content = b""Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 210–212why, one step back210 for part in response.stream():211 content += part212http3 (#86)Tom Christiebefore adoptionc9747aa
line 213why, one step back213 with pytest.raises(http3.StreamConsumed):Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 214–218why, one step back214 response.read()215216217@pytest.mark.asyncio218async def test_async_cannot_read_after_stream_consumed():http3 (#86)Tom Christiebefore adoptionc9747aa
line 219why, one step back219 response = http3.AsyncResponse(200, content=async_streaming_body())Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 220–221why, one step back220221 content = b""Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 222why, one step back222 async for part in response.stream():Response.contentTom Christiebefore adoption4c55113
line 223why, one step back223 content += partRejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 224why, one step back224http3 (#86)Tom Christiebefore adoptionc9747aa
line 225why, one step back225 with pytest.raises(http3.StreamConsumed):Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 226–228why, one step back226 await response.read()227228Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 229why, one step back229def test_cannot_read_after_response_closed():http3 (#86)Tom Christiebefore adoptionc9747aa
line 230why, one step back230 response = http3.Response(200, content=streaming_body())Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 231–233why, one step back231232 response.close()233http3 (#86)Tom Christiebefore adoptionc9747aa
line 234why, one step back234 with pytest.raises(http3.ResponseClosed):Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 235–239why, one step back235 response.read()236237238@pytest.mark.asyncio239async def test_async_cannot_read_after_response_closed():http3 (#86)Tom Christiebefore adoptionc9747aa
line 240why, one step back240 response = http3.AsyncResponse(200, content=async_streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 241–243why, one step back241242 await response.close()243http3 (#86)Tom Christiebefore adoptionc9747aa
line 244why, one step back244 with pytest.raises(http3.ResponseClosed):Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 245–248why, one step back245 await response.read()246247248def test_unknown_status_code():http3 (#86)Tom Christiebefore adoptionc9747aa
line 249why, one step back249 response = http3.Response(600)Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 250–251why, one step back250 assert response.status_code == 600251 assert response.reason_phrase == ""Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 252why, one step back252 assert response.text == ""