Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

148 lines · 3.9 KiB · Python

Last changed by

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

History of this file

Take it

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

lines 1–148why, one step back
1import pytest
23import 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 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_set_explicit_encoding():40 headers = {41 "Content-Type": "text-plain; charset=utf-8"42 } # Deliberately incorrect charset43 response = httpcore.Response(44 200, content="Latin 1: ÿ".encode("latin-1"), headers=headers45 )46 response.encoding = "latin-1"47 assert response.text == "Latin 1: ÿ"48 assert response.encoding == "latin-1"495051def test_response_force_encoding():52 response = httpcore.Response(200, content="Snowman: ☃".encode("utf-8"))53 response.encoding = "iso-8859-1"54 assert response.status_code == 20055 assert response.reason_phrase == "OK"56 assert response.text == "Snowman: â\x98\x83"57 assert response.encoding == "iso-8859-1"585960@pytest.mark.asyncio61async def test_read_response():62 response = httpcore.Response(200, content=b"Hello, world!")6364 assert response.status_code == 20065 assert response.text == "Hello, world!"66 assert response.encoding == "ascii"67 assert response.is_closed6869 content = await response.read()7071 assert content == b"Hello, world!"72 assert response.content == b"Hello, world!"73 assert response.is_closed747576@pytest.mark.asyncio77async def test_raw_interface():78 response = httpcore.Response(200, content=b"Hello, world!")7980 raw = b""81 async for part in response.raw():82 raw += part83 assert raw == b"Hello, world!"848586@pytest.mark.asyncio87async def test_stream_interface():88 response = httpcore.Response(200, content=b"Hello, world!")8990 content = b""91 async for part in response.stream():92 content += part93 assert content == b"Hello, world!"949596@pytest.mark.asyncio97async def test_stream_interface_after_read():98 response = httpcore.Response(200, content=b"Hello, world!")99100 await response.read()101102 content = b""103 async for part in response.stream():104 content += part105 assert content == b"Hello, world!"106107108@pytest.mark.asyncio109async def test_streaming_response():110 response = httpcore.Response(200, content=streaming_body())111112 assert response.status_code == 200113 assert not response.is_closed114115 content = await response.read()116117 assert content == b"Hello, world!"118 assert response.content == b"Hello, world!"119 assert response.is_closed120121122@pytest.mark.asyncio123async def test_cannot_read_after_stream_consumed():124 response = httpcore.Response(200, content=streaming_body())125126 content = b""127 async for part in response.stream():128 content += part129130 with pytest.raises(httpcore.StreamConsumed):131 await response.read()132133134@pytest.mark.asyncio135async def test_cannot_read_after_response_closed():136 response = httpcore.Response(200, content=streaming_body())137138 await response.close()139140 with pytest.raises(httpcore.ResponseClosed):141 await response.read()142143144def test_unknown_status_code():145 response = httpcore.Response(600)146 assert response.status_code == 600147 assert response.reason_phrase == ""148 assert response.text == ""