File: test_internals_response.py

package info (click to toggle)
datasette 0.65.2%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,540 kB
  • sloc: python: 19,371; javascript: 10,089; sh: 71; makefile: 47; ansic: 26
file content (54 lines) | stat: -rw-r--r-- 1,553 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from datasette.utils.asgi import Response
import pytest


def test_response_html():
    response = Response.html("Hello from HTML")
    assert 200 == response.status
    assert "Hello from HTML" == response.body
    assert "text/html; charset=utf-8" == response.content_type


def test_response_text():
    response = Response.text("Hello from text")
    assert 200 == response.status
    assert "Hello from text" == response.body
    assert "text/plain; charset=utf-8" == response.content_type


def test_response_json():
    response = Response.json({"this_is": "json"})
    assert 200 == response.status
    assert '{"this_is": "json"}' == response.body
    assert "application/json; charset=utf-8" == response.content_type


def test_response_redirect():
    response = Response.redirect("/foo")
    assert 302 == response.status
    assert "/foo" == response.headers["Location"]


@pytest.mark.asyncio
async def test_response_set_cookie():
    events = []

    async def send(event):
        events.append(event)

    response = Response.redirect("/foo")
    response.set_cookie("foo", "bar", max_age=10, httponly=True)
    await response.asgi_send(send)

    assert [
        {
            "type": "http.response.start",
            "status": 302,
            "headers": [
                [b"Location", b"/foo"],
                [b"content-type", b"text/plain"],
                [b"set-cookie", b"foo=bar; HttpOnly; Max-Age=10; Path=/; SameSite=lax"],
            ],
        },
        {"type": "http.response.body", "body": b""},
    ] == events