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
1import pytest23import httpcore456async def streaming_body():7 yield b"Hello, "8 yield b"world!"91011def test_response():12 response = httpcore.Response(200, content=b"Hello, world!")13 assert response.status_code == 20014 assert response.reason_phrase == "OK"15 assert response.text == "Hello, world!"161718def test_response_repr():19 response = httpcore.Response(200, content=b"Hello, world!")20 assert repr(response) == "<Response(200, 'OK')>"212223def test_response_content_type_encoding():24 """25 Use the charset encoding in the Content-Type header if possible.26 """27 headers = {"Content-Type": "text-plain; charset=latin-1"}28 content = "Latin 1: ÿ".encode("latin-1")29 response = httpcore.Response(200, content=content, headers=headers)30 assert response.text == "Latin 1: ÿ"31 assert response.encoding == "latin-1"323334def test_response_autodetect_encoding():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"424344def 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"535455def test_response_default_text_encoding():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!"666768def test_response_default_encoding():69 """70 Default to utf-8 if all else fails.71 """72 response = httpcore.Response(200, content=b"")73 assert response.text == ""74 assert response.encoding == "utf-8"757677def 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"858687def test_response_set_explicit_encoding():88 headers = {89 "Content-Type": "text-plain; charset=utf-8"90 } # Deliberately incorrect charset91 response = httpcore.Response(92 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers93 )94 response.encoding = "latin-1"95 assert response.text == "Latin 1: ÿ"96 assert response.encoding == "latin-1"979899def 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"106107108@pytest.mark.asyncio109async def test_read_response():110 response = httpcore.Response(200, content=b"Hello, world!")111112 assert response.status_code == 200113 assert response.text == "Hello, world!"114 assert response.encoding == "ascii"115 assert response.is_closed116117 content = await response.read()118119 assert content == b"Hello, world!"120 assert response.content == b"Hello, world!"121 assert response.is_closed122123124@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!"154155156@pytest.mark.asyncio157async def test_streaming_response():158 response = httpcore.Response(200, content=streaming_body())159160 assert response.status_code == 200161 assert not response.is_closed162163 content = await response.read()164165 assert content == b"Hello, world!"166 assert response.content == b"Hello, world!"167 assert response.is_closed168169170@pytest.mark.asyncio171async def test_cannot_read_after_stream_consumed():172 response = httpcore.Response(200, content=streaming_body())173174 content = b""175 async for part in response.stream():176 content += part177178 with pytest.raises(httpcore.StreamConsumed):179 await response.read()180181182@pytest.mark.asyncio183async def test_cannot_read_after_response_closed():184 response = httpcore.Response(200, content=streaming_body())185186 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 == ""196 assert response.text == ""