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
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 back1617Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 18why, one step back18def test_response_content_type_encoding():Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 19–21why, one step back19 """20 Use the charset encoding in the Content-Type header if possible.21 """Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 22why, one step back22 headers = {"Content-Type": "text-plain; charset=latin-1"}Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 23–24why, one step back23 content = "Latin 1: ÿ".encode("latin-1")24 response = httpcore.Response(200, content=content, headers=headers)Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 25–29why, one step back25 assert response.text == "Latin 1: ÿ"26 assert response.encoding == "latin-1"272829def test_response_autodetect_encoding():Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 30–36why, one step back30 """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"Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 37–38why, one step back3738Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 39–49why, one step back39def 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"4849Media types with 'text/' should default to iso-8859-1Tom Christiebefore adoptiond7cf8bb
lines 50–62why, one step back50def 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!"6162Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 63why, one step back63def test_response_default_encoding():Carefulness with encoding everywhereTom Christiebefore adoptione1ecb36
lines 64–66why, one step back64 """65 Default to utf-8 if all else fails.66 """Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 67–71why, one step back67 response = httpcore.Response(200, content=b"")68 assert response.text == ""69 assert response.encoding == "utf-8"7071Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e
line 72why, one step back72def test_response_set_explicit_encoding():Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 73–75why, one step back73 headers = {74 "Content-Type": "text-plain; charset=utf-8"75 } # Deliberately incorrect charsetAdd test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e
lines 76–78why, one step back76 response = httpcore.Response(77 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers78 )Use rfc3986 for URL handlingTom Christiebefore adoption302fe93
line 79why, one step back79 response.encoding = "latin-1"Add test for explicitly setting the encoding to useTom Christiebefore adoptionfd9823e
lines 80–83why, one step back80 assert response.text == "Latin 1: ÿ"81 assert response.encoding == "latin-1"8283Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 84–92why, one step back84def 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"9192Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 93–94why, one step back93@pytest.mark.asyncio94async def test_read_response():Response.contentTom Christiebefore adoption4c55113
line 95why, one step back95 response = httpcore.Response(200, content=b"Hello, world!")Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 96–97why, one step back9697 assert response.status_code == 200Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 98–99why, one step back98 assert response.text == "Hello, world!"99 assert response.encoding == "ascii"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 100–101why, one step back100 assert response.is_closed101Response.contentTom Christiebefore adoption4c55113
line 102why, one step back102 content = await response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 103why, one step back103Response.contentTom Christiebefore adoption4c55113
lines 104–105why, one step back104 assert content == b"Hello, world!"105 assert response.content == b"Hello, world!"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 106why, one step back106 assert response.is_closedUse rfc3986 for URL handlingTom Christiebefore adoption302fe93
lines 107–138why, one step back107108109@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!"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 139–142why, one step back139140141@pytest.mark.asyncio142async def test_streaming_response():Response.contentTom Christiebefore adoption4c55113
line 143why, one step back143 response = httpcore.Response(200, content=streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 144–147why, one step back144145 assert response.status_code == 200146 assert not response.is_closed147Response.contentTom Christiebefore adoption4c55113
line 148why, one step back148 content = await response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 149why, one step back149Response.contentTom Christiebefore adoption4c55113
lines 150–151why, one step back150 assert content == b"Hello, world!"151 assert response.content == b"Hello, world!"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 152–156why, one step back152 assert response.is_closed153154155@pytest.mark.asyncio156async def test_cannot_read_after_stream_consumed():Response.contentTom Christiebefore adoption4c55113
line 157why, one step back157 response = httpcore.Response(200, content=streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 158why, one step back158Response.contentTom Christiebefore adoption4c55113
line 159why, one step back159 content = b""Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 160why, one step back160 async for part in response.stream():Response.contentTom Christiebefore adoption4c55113
line 161why, one step back161 content += partRejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 162–168why, one step back162163 with pytest.raises(httpcore.StreamConsumed):164 await response.read()165166167@pytest.mark.asyncio168async def test_cannot_read_after_response_closed():Response.contentTom Christiebefore adoption4c55113
line 169why, one step back169 response = httpcore.Response(200, content=streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 170–180why, one step back170171 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 == ""Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 181why, one step back181 assert response.text == ""