test_responses.py
This file
Path
tests/models/test_responses.py
104 lines · 2.8 KiB · Python
Last changed by
Add response.text and response.encodingTom Christiebefore adoptiond8df61b
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
lines 18–47why, one step back18def test_response_content_type_encoding():19 headers = {"Content-Type": "text-plain; charset=latin-1"}20 response = httpcore.Response(21 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers22 )23 assert response.text == "Latin 1: ÿ"24 assert response.encoding == "latin-1"252627def test_response_autodetect_encoding():28 response = httpcore.Response(200, content="Snowmen: ☃☃☃".encode("utf-8"))29 assert response.text == "Snowmen: ☃☃☃"30 assert response.encoding == "utf-8"313233def test_response_default_encoding():34 response = httpcore.Response(200, content=b"")35 assert response.text == ""36 assert response.encoding == "utf-8"373839def test_response_force_encoding():40 response = httpcore.Response(200, content="Snowman: ☃".encode("utf-8"))41 response.encoding = "iso-8859-1"42 assert response.status_code == 20043 assert response.reason_phrase == "OK"44 assert response.text == "Snowman: â\x98\x83"45 assert response.encoding == "iso-8859-1"4647Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 48–49why, one step back48@pytest.mark.asyncio49async def test_read_response():Response.contentTom Christiebefore adoption4c55113
line 50why, one step back50 response = httpcore.Response(200, content=b"Hello, world!")Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 51–52why, one step back5152 assert response.status_code == 200Add response.text and response.encodingTom Christiebefore adoptiond8df61b
lines 53–54why, one step back53 assert response.text == "Hello, world!"54 assert response.encoding == "ascii"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 55–56why, one step back55 assert response.is_closed56Response.contentTom Christiebefore adoption4c55113
line 57why, one step back57 content = await response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 58why, one step back58Response.contentTom Christiebefore adoption4c55113
lines 59–60why, one step back59 assert content == b"Hello, world!"60 assert response.content == b"Hello, world!"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 61–65why, one step back61 assert response.is_closed626364@pytest.mark.asyncio65async def test_streaming_response():Response.contentTom Christiebefore adoption4c55113
line 66why, one step back66 response = httpcore.Response(200, content=streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 67–70why, one step back6768 assert response.status_code == 20069 assert not response.is_closed70Response.contentTom Christiebefore adoption4c55113
line 71why, one step back71 content = await response.read()Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 72why, one step back72Response.contentTom Christiebefore adoption4c55113
lines 73–74why, one step back73 assert content == b"Hello, world!"74 assert response.content == b"Hello, world!"Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 75–79why, one step back75 assert response.is_closed767778@pytest.mark.asyncio79async def test_cannot_read_after_stream_consumed():Response.contentTom Christiebefore adoption4c55113
line 80why, one step back80 response = httpcore.Response(200, content=streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 81why, one step back81Response.contentTom Christiebefore adoption4c55113
line 82why, one step back82 content = b""Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
line 83why, one step back83 async for part in response.stream():Response.contentTom Christiebefore adoption4c55113
line 84why, one step back84 content += partRejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 85–91why, one step back8586 with pytest.raises(httpcore.StreamConsumed):87 await response.read()888990@pytest.mark.asyncio91async def test_cannot_read_after_response_closed():Response.contentTom Christiebefore adoption4c55113
line 92why, one step back92 response = httpcore.Response(200, content=streaming_body())Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 93–103why, one step back9394 await response.close()9596 with pytest.raises(httpcore.ResponseClosed):97 await response.read()9899100def test_unknown_status_code():101 response = httpcore.Response(600)102 assert response.status_code == 600103 assert response.reason_phrase == ""Add response.text and response.encodingTom Christiebefore adoptiond8df61b
line 104why, one step back104 assert response.text == ""