test_responses.py
This file
Path
tests/models/test_responses.py
181 lines · 5.1 KiB · Python
Last changed by
Media types with 'text/' should default to iso-8859-1Tom Christiebefore adoptiond7cf8bb
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_content_type_encoding():19 """20 Use the charset encoding in the Content-Type header if possible.21 """22 headers = {"Content-Type": "text-plain; charset=latin-1"}23 content = "Latin 1: ÿ".encode("latin-1")24 response = httpcore.Response(200, content=content, headers=headers)25 assert response.text == "Latin 1: ÿ"26 assert response.encoding == "latin-1"272829def test_response_autodetect_encoding():30 """31 Autodetect encoding if there is no charset info in a Content-Type header.32 """33 content = "おはようございます。".encode("EUC-JP")34 response = httpcore.Response(200, content=content)35 assert response.text == "おはようございます。"36 assert response.encoding == "EUC-JP"373839def test_response_fallback_to_autodetect():40 """41 Fallback to autodetection if we get an invalid charset in the Content-Type header.42 """43 headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}44 content = "おはようございます。".encode("EUC-JP")45 response = httpcore.Response(200, content=content, headers=headers)46 assert response.text == "おはようございます。"47 assert response.encoding == "EUC-JP"484950def test_response():51 """52 A media type of 'text/*' with no charset should default to ISO-8859-1.53 See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.154 """55 content = b"Hello, world!"56 headers = {"Content-Type": "text/plain"}57 response = httpcore.Response(200, content=content, headers=headers)58 assert response.status_code == 20059 assert response.encoding == "iso-8859-1"60 assert response.text == "Hello, world!"616263def test_response_default_encoding():64 """65 Default to utf-8 if all else fails.66 """67 response = httpcore.Response(200, content=b"")68 assert response.text == ""69 assert response.encoding == "utf-8"707172def test_response_set_explicit_encoding():73 headers = {74 "Content-Type": "text-plain; charset=utf-8"75 } # Deliberately incorrect charset76 response = httpcore.Response(77 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers78 )79 response.encoding = "latin-1"80 assert response.text == "Latin 1: ÿ"81 assert response.encoding == "latin-1"828384def test_response_force_encoding():85 response = httpcore.Response(200, content="Snowman: ☃".encode("utf-8"))86 response.encoding = "iso-8859-1"87 assert response.status_code == 20088 assert response.reason_phrase == "OK"89 assert response.text == "Snowman: â\x98\x83"90 assert response.encoding == "iso-8859-1"919293@pytest.mark.asyncio94async def test_read_response():95 response = httpcore.Response(200, content=b"Hello, world!")9697 assert response.status_code == 20098 assert response.text == "Hello, world!"99 assert response.encoding == "ascii"100 assert response.is_closed101102 content = await response.read()103104 assert content == b"Hello, world!"105 assert response.content == b"Hello, world!"106 assert response.is_closed107108109@pytest.mark.asyncio110async def test_raw_interface():111 response = httpcore.Response(200, content=b"Hello, world!")112113 raw = b""114 async for part in response.raw():115 raw += part116 assert raw == b"Hello, world!"117118119@pytest.mark.asyncio120async def test_stream_interface():121 response = httpcore.Response(200, content=b"Hello, world!")122123 content = b""124 async for part in response.stream():125 content += part126 assert content == b"Hello, world!"127128129@pytest.mark.asyncio130async def test_stream_interface_after_read():131 response = httpcore.Response(200, content=b"Hello, world!")132133 await response.read()134135 content = b""136 async for part in response.stream():137 content += part138 assert content == b"Hello, world!"139140141@pytest.mark.asyncio142async def test_streaming_response():143 response = httpcore.Response(200, content=streaming_body())144145 assert response.status_code == 200146 assert not response.is_closed147148 content = await response.read()149150 assert content == b"Hello, world!"151 assert response.content == b"Hello, world!"152 assert response.is_closed153154155@pytest.mark.asyncio156async def test_cannot_read_after_stream_consumed():157 response = httpcore.Response(200, content=streaming_body())158159 content = b""160 async for part in response.stream():161 content += part162163 with pytest.raises(httpcore.StreamConsumed):164 await response.read()165166167@pytest.mark.asyncio168async def test_cannot_read_after_response_closed():169 response = httpcore.Response(200, content=streaming_body())170171 await response.close()172173 with pytest.raises(httpcore.ResponseClosed):174 await response.read()175176177def test_unknown_status_code():178 response = httpcore.Response(600)179 assert response.status_code == 600180 assert response.reason_phrase == ""181 assert response.text == ""