Skip to content
Myr

test_responses.py

This file

Path

tests/models/test_responses.py

252 lines · 6.7 KiB · Python

Last changed by

Int status codes (#92)Tom Christiebefore adoption56a7943

History of this file

Take it

1import pytest23import http3456def streaming_body():7    yield b"Hello, "8    yield b"world!"91011async def async_streaming_body():12    yield b"Hello, "13    yield b"world!"141516def test_response():17    response = http3.Response(200, content=b"Hello, world!")18    assert response.status_code == 20019    assert response.reason_phrase == "OK"20    assert response.text == "Hello, world!"212223def test_response_repr():24    response = http3.Response(200, content=b"Hello, world!")25    assert repr(response) == "<Response [200 OK])>"262728def test_response_content_type_encoding():29    """30    Use the charset encoding in the Content-Type header if possible.31    """32    headers = {"Content-Type": "text-plain; charset=latin-1"}33    content = "Latin 1: ÿ".encode("latin-1")34    response = http3.Response(200, content=content, headers=headers)35    assert response.text == "Latin 1: ÿ"36    assert response.encoding == "latin-1"373839def test_response_autodetect_encoding():40    """41    Autodetect encoding if there is no charset info in a Content-Type header.42    """43    content = "おはようございます。".encode("EUC-JP")44    response = http3.Response(200, content=content)45    assert response.text == "おはようございます。"46    assert response.encoding == "EUC-JP"474849def test_response_fallback_to_autodetect():50    """51    Fallback to autodetection if we get an invalid charset in the Content-Type header.52    """53    headers = {"Content-Type": "text-plain; charset=invalid-codec-name"}54    content = "おはようございます。".encode("EUC-JP")55    response = http3.Response(200, content=content, headers=headers)56    assert response.text == "おはようございます。"57    assert response.encoding == "EUC-JP"585960def test_response_default_text_encoding():61    """62    A media type of 'text/*' with no charset should default to ISO-8859-1.63    See: https://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.164    """65    content = b"Hello, world!"66    headers = {"Content-Type": "text/plain"}67    response = http3.Response(200, content=content, headers=headers)68    assert response.status_code == 20069    assert response.encoding == "iso-8859-1"70    assert response.text == "Hello, world!"717273def test_response_default_encoding():74    """75    Default to utf-8 if all else fails.76    """77    response = http3.Response(200, content=b"")78    assert response.text == ""79    assert response.encoding == "utf-8"808182def test_response_non_text_encoding():83    """84    Default to apparent encoding for non-text content-type headers.85    """86    headers = {"Content-Type": "image/png"}87    response = http3.Response(200, content=b"xyz", headers=headers)88    assert response.text == "xyz"89    assert response.encoding == "ascii"909192def test_response_set_explicit_encoding():93    headers = {94        "Content-Type": "text-plain; charset=utf-8"95    }  # Deliberately incorrect charset96    response = http3.Response(97        200, content="Latin 1: ÿ".encode("latin-1"), headers=headers98    )99    response.encoding = "latin-1"100    assert response.text == "Latin 1: ÿ"101    assert response.encoding == "latin-1"102103104def test_response_force_encoding():105    response = http3.Response(200, content="Snowman: ☃".encode("utf-8"))106    response.encoding = "iso-8859-1"107    assert response.status_code == 200108    assert response.reason_phrase == "OK"109    assert response.text == "Snowman: â\x98\x83"110    assert response.encoding == "iso-8859-1"111112113def test_read_response():114    response = http3.Response(200, content=b"Hello, world!")115116    assert response.status_code == 200117    assert response.text == "Hello, world!"118    assert response.encoding == "ascii"119    assert response.is_closed120121    content = response.read()122123    assert content == b"Hello, world!"124    assert response.content == b"Hello, world!"125    assert response.is_closed126127128def test_raw_interface():129    response = http3.Response(200, content=b"Hello, world!")130131    raw = b""132    for part in response.raw():133        raw += part134    assert raw == b"Hello, world!"135136137def test_stream_interface():138    response = http3.Response(200, content=b"Hello, world!")139140    content = b""141    for part in response.stream():142        content += part143    assert content == b"Hello, world!"144145146@pytest.mark.asyncio147async def test_async_stream_interface():148    response = http3.AsyncResponse(200, content=b"Hello, world!")149150    content = b""151    async for part in response.stream():152        content += part153    assert content == b"Hello, world!"154155156def test_stream_interface_after_read():157    response = http3.Response(200, content=b"Hello, world!")158159    response.read()160161    content = b""162    for part in response.stream():163        content += part164    assert content == b"Hello, world!"165166167@pytest.mark.asyncio168async def test_async_stream_interface_after_read():169    response = http3.AsyncResponse(200, content=b"Hello, world!")170171    await response.read()172173    content = b""174    async for part in response.stream():175        content += part176    assert content == b"Hello, world!"177178179def test_streaming_response():180    response = http3.Response(200, content=streaming_body())181182    assert response.status_code == 200183    assert not response.is_closed184185    content = response.read()186187    assert content == b"Hello, world!"188    assert response.content == b"Hello, world!"189    assert response.is_closed190191192@pytest.mark.asyncio193async def test_async_streaming_response():194    response = http3.AsyncResponse(200, content=async_streaming_body())195196    assert response.status_code == 200197    assert not response.is_closed198199    content = await response.read()200201    assert content == b"Hello, world!"202    assert response.content == b"Hello, world!"203    assert response.is_closed204205206def test_cannot_read_after_stream_consumed():207    response = http3.Response(200, content=streaming_body())208209    content = b""210    for part in response.stream():211        content += part212213    with pytest.raises(http3.StreamConsumed):214        response.read()215216217@pytest.mark.asyncio218async def test_async_cannot_read_after_stream_consumed():219    response = http3.AsyncResponse(200, content=async_streaming_body())220221    content = b""222    async for part in response.stream():223        content += part224225    with pytest.raises(http3.StreamConsumed):226        await response.read()227228229def test_cannot_read_after_response_closed():230    response = http3.Response(200, content=streaming_body())231232    response.close()233234    with pytest.raises(http3.ResponseClosed):235        response.read()236237238@pytest.mark.asyncio239async def test_async_cannot_read_after_response_closed():240    response = http3.AsyncResponse(200, content=async_streaming_body())241242    await response.close()243244    with pytest.raises(http3.ResponseClosed):245        await response.read()246247248def test_unknown_status_code():249    response = http3.Response(600)250    assert response.status_code == 600251    assert response.reason_phrase == ""252    assert response.text == ""