starlette/tests/test_response.py

85 lines
2.3 KiB
Python
Raw Normal View History

2018-07-11 15:30:40 +00:00
from starlette import FileResponse, Response, StreamingResponse, TestClient
2018-06-25 13:15:32 +00:00
import asyncio
def test_text_response():
def app(scope):
async def asgi(receive, send):
response = Response("hello, world", media_type="text/plain")
await response(receive, send)
return asgi
client = TestClient(app)
response = client.get("/")
assert response.text == "hello, world"
def test_bytes_response():
def app(scope):
async def asgi(receive, send):
response = Response(b"xxxxx", media_type="image/png")
await response(receive, send)
return asgi
client = TestClient(app)
response = client.get("/")
assert response.content == b"xxxxx"
def test_streaming_response():
def app(scope):
async def numbers(minimum, maximum):
for i in range(minimum, maximum + 1):
yield str(i)
if i != maximum:
yield ", "
await asyncio.sleep(0)
async def asgi(receive, send):
generator = numbers(1, 5)
response = StreamingResponse(generator, media_type="text/plain")
await response(receive, send)
return asgi
client = TestClient(app)
response = client.get("/")
assert response.text == "1, 2, 3, 4, 5"
def test_response_headers():
def app(scope):
async def asgi(receive, send):
headers = {"x-header-1": "123", "x-header-2": "456"}
response = Response(
"hello, world", media_type="text/plain", headers=headers
)
response.headers["x-header-2"] = "789"
await response(receive, send)
return asgi
client = TestClient(app)
response = client.get("/")
assert response.headers["x-header-1"] == "123"
assert response.headers["x-header-2"] == "789"
2018-07-10 14:19:51 +00:00
2018-07-11 15:30:40 +00:00
def test_file_response(tmpdir):
with open("xyz", "wb") as file:
file.write(b"<file content>")
2018-07-10 14:19:51 +00:00
2018-07-11 15:30:40 +00:00
def app(scope):
return FileResponse(path="xyz", filename="example.png")
2018-07-10 14:19:51 +00:00
client = TestClient(app)
response = client.get("/")
2018-07-11 15:30:40 +00:00
assert response.status_code == 200
assert response.content == b"<file content>"
assert response.headers["content-type"] == "image/png"
assert (
response.headers["content-disposition"] == 'attachment; filename="example.png"'
)