Skip to content
Myr

test_responses.py

This file

Path

tests/test_responses.py

74 lines · 1.7 KiB · Python

Last changed by

Merge pull request #12 from encode/refactorTom Christiebefore adoption39eb55b

History of this file

Take it

1import pytest23import httpcore456async def streaming_body():7    yield b"Hello, "8    yield b"world!"91011def test_response():12    response = httpcore.Response(200, body=b"Hello, world!")13    assert response.status_code == 20014    assert response.reason == "OK"15    assert response.body == b"Hello, world!"16    assert response.is_closed171819@pytest.mark.asyncio20async def test_read_response():21    response = httpcore.Response(200, body=b"Hello, world!")2223    assert response.status_code == 20024    assert response.body == b"Hello, world!"25    assert response.is_closed2627    body = await response.read()2829    assert body == b"Hello, world!"30    assert response.body == b"Hello, world!"31    assert response.is_closed323334@pytest.mark.asyncio35async def test_streaming_response():36    response = httpcore.Response(200, body=streaming_body())3738    assert response.status_code == 20039    assert not hasattr(response, "body")40    assert not response.is_closed4142    body = await response.read()4344    assert body == b"Hello, world!"45    assert response.body == b"Hello, world!"46    assert response.is_closed474849@pytest.mark.asyncio50async def test_cannot_read_after_stream_consumed():51    response = httpcore.Response(200, body=streaming_body())5253    body = b""54    async for part in response.stream():55        body += part5657    with pytest.raises(httpcore.StreamConsumed):58        await response.read()596061@pytest.mark.asyncio62async def test_cannot_read_after_response_closed():63    response = httpcore.Response(200, body=streaming_body())6465    await response.close()6667    with pytest.raises(httpcore.ResponseClosed):68        await response.read()697071def test_unknown_status_code():72    response = httpcore.Response(600)73    assert response.status_code == 60074    assert response.reason == ""