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 fileTake it
Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 1–11why, one step back1import pytest23import httpcore456async def streaming_body():7 yield b"Hello, "8 yield b"world!"91011def test_response():Response.contentTom Christiebefore adoption4c55113
line 12why, one step back12 response = httpcore.Response(200, content=b"Hello, world!")Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 13–14why, one step back13 assert response.status_code == 20014 assert response.reason_phrase == "OK"Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 15why, one step back15 assert response.text == "Hello, world!"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 16–17why, one step back1617Increase test coverageTom Christiebefore adoptione99ffbd
lines 18–22why, one step back18def test_response_repr():19 response = httpcore.Response(200, content=b"Hello, world!")20 assert repr(response) == "<Response(200, 'OK')>"2122Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 23why, one step back23def test_response_content_type_encoding():Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 24–26why, one step back24 """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 back27 headers = {"Content-Type": "text-plain; charset=latin-1"}Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 28–29why, one step back28 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 back30 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 back35 """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 back4243Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 44–54why, one step back44def 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"5354Increase test coverageTom Christiebefore adoptione99ffbd
line 55why, one step back55def test_response_default_text_encoding():Media types with 'text/' should default to iso-8859-1Tom Christiebefore adoptiond7cf8bb
lines 56–67why, one step back56 """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!"6667Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 68why, one step back68def test_response_default_encoding():Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 69–71why, one step back69 """70 Default to utf-8 if all else fails.71 """Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 72–74why, one step back72 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 back757677def 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 back8586Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e
line 87why, one step back87def test_response_set_explicit_encoding():Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 88–90why, one step back88 headers = {89 "Content-Type": "text-plain; charset=utf-8"90 } # Deliberately incorrect charsetAdd test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e
lines 91–93why, one step back91 response = httpcore.Response(92 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers93 )Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
line 94why, one step back94 response.encoding = "latin-1"Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e
lines 95–98why, one step back95 assert response.text == "Latin 1: ÿ"96 assert response.encoding == "latin-1"9798Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 99–107why, one step back99def 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"106107Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 108–109why, one step back108@pytest.mark.asyncio109async def test_read_response():Response.contentTom Christiebefore adoption4c55113
line 110why, one step back110 response = httpcore.Response(200, content=b"Hello, world!")Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 111–112why, one step back111112 assert response.status_code == 200Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 113–114why, one step back113 assert response.text == "Hello, world!"114 assert response.encoding == "ascii"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 115–116why, one step back115 assert response.is_closed116Response.contentTom Christiebefore adoption4c55113
line 117why, one step back117 content = await response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 118why, one step back118Response.contentTom Christiebefore adoption4c55113
lines 119–120why, one step back119 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 back121 assert response.is_closedUse rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 122–153why, one step back122123124@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 back154155156@pytest.mark.asyncio157async def test_streaming_response():Response.contentTom Christiebefore adoption4c55113
line 158why, one step back158 response = httpcore.Response(200, content=streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 159–162why, one step back159160 assert response.status_code == 200161 assert not response.is_closed162Response.contentTom Christiebefore adoption4c55113
line 163why, one step back163 content = await response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 164why, one step back164Response.contentTom Christiebefore adoption4c55113
lines 165–166why, one step back165 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 back167 assert response.is_closed168169170@pytest.mark.asyncio171async def test_cannot_read_after_stream_consumed():Response.contentTom Christiebefore adoption4c55113
line 172why, one step back172 response = httpcore.Response(200, content=streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 173why, one step back173Response.contentTom Christiebefore adoption4c55113
line 174why, one step back174 content = b""Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 175why, one step back175 async for part in response.stream():Response.contentTom Christiebefore adoption4c55113
line 176why, one step back176 content += partRejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 177–183why, one step back177178 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 back184 response = httpcore.Response(200, content=streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 185–195why, one step back185186 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 back196 assert response.text == ""