Skip to content
Myr

test_responses.py

This file

Path

tests/test_responses.py

121 lines · 3.0 KiB · Python

Last changed by

Add response.reasonTom Christiebefore adoptionfc7cf9d

History of this file

Take it

1import pytest23import httpcore456class MockHTTP(httpcore.ConnectionPool):7    async def request(8        self, method, url, *, headers=(), body=b"", stream=False9    ) -> httpcore.Response:10        if stream:1112            async def streaming_body():13                yield b"Hello, "14                yield b"world!"1516            return httpcore.Response(200, body=streaming_body())17        return httpcore.Response(200, body=b"Hello, world!")181920http = MockHTTP()212223@pytest.mark.asyncio24async def test_request():25    response = await http.request("GET", "http://example.com")26    assert response.status_code == 20027    assert response.reason == "OK"28    assert response.body == b"Hello, world!"29    assert response.is_closed303132@pytest.mark.asyncio33async def test_read_response():34    response = await http.request("GET", "http://example.com")3536    assert response.status_code == 20037    assert response.body == b"Hello, world!"38    assert response.is_closed3940    body = await response.read()4142    assert body == b"Hello, world!"43    assert response.body == b"Hello, world!"44    assert response.is_closed454647@pytest.mark.asyncio48async def test_stream_response():49    response = await http.request("GET", "http://example.com")5051    assert response.status_code == 20052    assert response.body == b"Hello, world!"53    assert response.is_closed5455    body = b""56    async for part in response.stream():57        body += part5859    assert body == b"Hello, world!"60    assert response.body == b"Hello, world!"61    assert response.is_closed626364@pytest.mark.asyncio65async def test_read_streaming_response():66    response = await http.request("GET", "http://example.com", stream=True)6768    assert response.status_code == 20069    assert not hasattr(response, "body")70    assert not response.is_closed7172    body = await response.read()7374    assert body == b"Hello, world!"75    assert response.body == b"Hello, world!"76    assert response.is_closed777879@pytest.mark.asyncio80async def test_stream_streaming_response():81    response = await http.request("GET", "http://example.com", stream=True)8283    assert response.status_code == 20084    assert not hasattr(response, "body")85    assert not response.is_closed8687    body = b""88    async for part in response.stream():89        body += part9091    assert body == b"Hello, world!"92    assert not hasattr(response, "body")93    assert response.is_closed949596@pytest.mark.asyncio97async def test_cannot_read_after_stream_consumed():98    response = await http.request("GET", "http://example.com", stream=True)99100    body = b""101    async for part in response.stream():102        body += part103104    with pytest.raises(httpcore.StreamConsumed):105        await response.read()106107108@pytest.mark.asyncio109async def test_cannot_read_after_response_closed():110    response = await http.request("GET", "http://example.com", stream=True)111112    await response.close()113114    with pytest.raises(httpcore.ResponseClosed):115        await response.read()116117118def test_unknown_status_code():119    response = httpcore.Response(600)120    assert response.status_code == 600121    assert response.reason == ""