File: test_static_files.py

package info (click to toggle)
litestar 2.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,568 kB
  • sloc: python: 70,588; makefile: 254; javascript: 104; sh: 60
file content (69 lines) | stat: -rw-r--r-- 2,183 bytes parent folder | download
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import secrets
from pathlib import Path

import pytest
from _pytest.monkeypatch import MonkeyPatch

from litestar.testing import TestClient


@pytest.fixture(autouse=True)
def _chdir(tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
    monkeypatch.chdir(tmp_path)


@pytest.fixture()
def assets_file(tmp_path: Path) -> str:
    content = secrets.token_hex()
    assets_path = tmp_path / "assets"
    assets_path.mkdir()
    assets_path.joinpath("test.txt").write_text(content)
    return content


def test_custom_router() -> None:
    from docs.examples.static_files import custom_router  # noqa: F401


def test_full_example() -> None:
    from docs.examples.static_files import full_example

    with TestClient(full_example.app) as client:
        assert client.get("/static/hello.txt").text == "Hello, world!"


def test_html_mode() -> None:
    from docs.examples.static_files import html_mode

    with TestClient(html_mode.app) as client:
        assert client.get("/").text == "<strong>Hello, world!</strong>"
        assert client.get("/index.html").text == "<strong>Hello, world!</strong>"
        assert client.get("/something").text == "<h1>Not found</h1>"


def test_passing_options() -> None:
    from docs.examples.static_files import passing_options  # noqa: F401


def test_route_reverse(capsys) -> None:
    from docs.examples.static_files import route_reverse  # noqa: F401

    assert capsys.readouterr().out.strip() == "/static/some_file.txt"


def test_send_as_attachment(tmp_path: Path, assets_file: str) -> None:
    from docs.examples.static_files import send_as_attachment

    with TestClient(send_as_attachment.app) as client:
        res = client.get("/static/test.txt")
        assert res.text == assets_file
        assert res.headers["content-disposition"].startswith("attachment")


def test_upgrade_from_static(tmp_path: Path, assets_file: str) -> None:
    from docs.examples.static_files import upgrade_from_static_1, upgrade_from_static_2

    for app in [upgrade_from_static_1.app, upgrade_from_static_2.app]:
        with TestClient(app) as client:
            res = client.get("/static/test.txt")
            assert res.text == assets_file