test_api.py
This file
Path
tests/test_api.py
96 lines · 2.4 KiB · Python
Last changed by
StuffTom Christiebefore adoption30530d4
History of this fileTake it
StuffTom Christiebefore adoption30530d4
lines 1–96why, one step back1import pytest23import httpcore456@pytest.mark.asyncio7async def test_request():8 response = await httpcore.request("GET", "http://example.com")9 assert response.status_code == 20010 assert response.body == b"Hello, world!"11 assert response.is_closed121314@pytest.mark.asyncio15async def test_read_response():16 response = await httpcore.request("GET", "http://example.com")1718 assert response.status_code == 20019 assert response.body == b"Hello, world!"20 assert response.is_closed2122 body = await response.read()2324 assert body == b"Hello, world!"25 assert response.body == b"Hello, world!"26 assert response.is_closed272829@pytest.mark.asyncio30async def test_stream_response():31 response = await httpcore.request("GET", "http://example.com")3233 assert response.status_code == 20034 assert response.body == b"Hello, world!"35 assert response.is_closed3637 body = b''38 async for part in response.stream():39 body += part4041 assert body == b"Hello, world!"42 assert response.body == b"Hello, world!"43 assert response.is_closed444546@pytest.mark.asyncio47async def test_read_streaming_response():48 response = await httpcore.request("GET", "http://example.com", stream=True)4950 assert response.status_code == 20051 assert not hasattr(response, 'body')52 assert not response.is_closed5354 body = await response.read()5556 assert body == b"Hello, world!"57 assert response.body == b"Hello, world!"58 assert response.is_closed596061@pytest.mark.asyncio62async def test_stream_streaming_response():63 response = await httpcore.request("GET", "http://example.com", stream=True)6465 assert response.status_code == 20066 assert not hasattr(response, 'body')67 assert not response.is_closed6869 body = b''70 async for part in response.stream():71 body += part7273 assert body == b"Hello, world!"74 assert not hasattr(response, 'body')75 assert response.is_closed767778@pytest.mark.asyncio79async def test_cannot_read_after_stream_consumed():80 response = await httpcore.request("GET", "http://example.com", stream=True)8182 body = b''83 async for part in response.stream():84 body += part8586 with pytest.raises(httpcore.StreamConsumed):87 await response.read()8889@pytest.mark.asyncio90async def test_cannot_read_after_response_closed():91 response = await httpcore.request("GET", "http://example.com", stream=True)9293 await response.close()9495 with pytest.raises(httpcore.ResponseClosed):96 await response.read()