| 1 |
from datasette.utils.asgi import Response |
| 2 |
import pytest |
| 3 |
|
| 4 |
|
| 5 |
def test_response_html(): |
| 6 |
response = Response.html("Hello from HTML") |
| 7 |
assert 200 == response.status |
| 8 |
assert "Hello from HTML" == response.body |
| 9 |
assert "text/html; charset=utf-8" == response.content_type |
| 10 |
|
| 11 |
|
| 12 |
def test_response_text(): |
| 13 |
response = Response.text("Hello from text") |
| 14 |
assert 200 == response.status |
| 15 |
assert "Hello from text" == response.body |
| 16 |
assert "text/plain; charset=utf-8" == response.content_type |
| 17 |
|
| 18 |
|
| 19 |
def test_response_json(): |
| 20 |
response = Response.json({"this_is": "json"}) |
| 21 |
assert 200 == response.status |
| 22 |
assert '{"this_is": "json"}' == response.body |
| 23 |
assert "application/json; charset=utf-8" == response.content_type |
| 24 |
|
| 25 |
|
| 26 |
def test_response_redirect(): |
| 27 |
response = Response.redirect("/foo") |
| 28 |
assert 302 == response.status |
| 29 |
assert "/foo" == response.headers["Location"] |
| 30 |
|
| 31 |
|
| 32 |
@pytest.mark.asyncio |
| 33 |
async def test_response_set_cookie(): |
| 34 |
events = [] |
| 35 |
|
| 36 |
async def send(event): |
| 37 |
events.append(event) |
| 38 |
|
| 39 |
response = Response.redirect("/foo") |
| 40 |
response.set_cookie("foo", "bar", max_age=10, httponly=True) |
| 41 |
await response.asgi_send(send) |
| 42 |
|
| 43 |
assert [ |
| 44 |
{ |
| 45 |
"type": "http.response.start", |
| 46 |
"status": 302, |
| 47 |
"headers": [ |
| 48 |
[b"Location", b"/foo"], |
| 49 |
[b"content-type", b"text/plain"], |
| 50 |
[b"set-cookie", b"foo=bar; HttpOnly; Max-Age=10; Path=/; SameSite=lax"], |
| 51 |
], |
| 52 |
}, |
| 53 |
{"type": "http.response.body", "body": b""}, |
| 54 |
] == events |