File: test_plugins.py

package info (click to toggle)
litestar 2.19.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,500 kB
  • sloc: python: 70,169; makefile: 254; javascript: 105; sh: 60
file content (118 lines) | stat: -rw-r--r-- 4,267 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import pytest

from litestar.openapi.config import OpenAPIConfig
from litestar.testing import TestClient, create_test_client


def test_scalar_simple() -> None:
    from docs.examples.openapi.plugins.scalar_simple import app

    with TestClient(app=app) as client:
        resp = client.get("/schema/scalar")
        assert resp.status_code == 200
        assert resp.headers["content-type"] == "text/html; charset=utf-8"
        assert "Litestar Example" in resp.text


def test_rapidoc_simple() -> None:
    from docs.examples.openapi.plugins.rapidoc_simple import app

    with TestClient(app=app) as client:
        resp = client.get("/schema/rapidoc")
        assert resp.status_code == 200
        assert resp.headers["content-type"] == "text/html; charset=utf-8"
        assert "Litestar Example" in resp.text


def test_redoc_simple() -> None:
    from docs.examples.openapi.plugins.redoc_simple import app

    with TestClient(app=app) as client:
        resp = client.get("/schema/redoc")
        assert resp.status_code == 200
        assert resp.headers["content-type"] == "text/html; charset=utf-8"
        assert "Litestar Example" in resp.text


def test_stoplights_simple() -> None:
    from docs.examples.openapi.plugins.stoplight_simple import app

    with TestClient(app=app) as client:
        resp = client.get("/schema/elements")
        assert resp.status_code == 200
        assert resp.headers["content-type"] == "text/html; charset=utf-8"
        assert "Litestar Example" in resp.text


def test_swagger_ui_simple() -> None:
    from docs.examples.openapi.plugins.swagger_ui_simple import app

    with TestClient(app=app) as client:
        resp = client.get("/schema/swagger")
        assert resp.status_code == 200
        assert resp.headers["content-type"] == "text/html; charset=utf-8"
        assert "Litestar Example" in resp.text


@pytest.mark.parametrize("path", ["/schema/openapi.yml", "/schema/openapi.yaml"])
def test_yaml_simple(path: str) -> None:
    from docs.examples.openapi.plugins.yaml_simple import app

    with TestClient(app=app) as client:
        resp = client.get(path)
        assert resp.status_code == 200
        assert resp.headers["content-type"] == "application/vnd.oai.openapi"
        assert "Litestar Example" in resp.text


def test_serving_multiple_uis() -> None:
    from docs.examples.openapi.plugins.serving_multiple_uis import app

    with TestClient(app=app) as client:
        resp = client.get("/schema/rapidoc")
        assert resp.status_code == 200
        assert resp.headers["content-type"] == "text/html; charset=utf-8"
        assert "Litestar Example" in resp.text

        resp = client.get("/schema/swagger")
        assert resp.status_code == 200
        assert resp.headers["content-type"] == "text/html; charset=utf-8"
        assert "Litestar Example" in resp.text


def test_custom_plugin() -> None:
    from docs.examples.openapi.plugins.custom_plugin import ScalarRenderPlugin

    openapi_config = OpenAPIConfig(
        title="My API",
        description="This is the description of my API",
        version="0.1.0",
        render_plugins=[ScalarRenderPlugin()],
    )

    with create_test_client(route_handlers=[], openapi_config=openapi_config) as client:
        resp = client.get("/schema/scalar")
        assert resp.status_code == 200
        assert resp.headers["content-type"] == "text/html; charset=utf-8"
        assert "My API" in resp.text


def test_receive_router() -> None:
    from docs.examples.openapi.plugins.receive_router import MyOpenAPIPlugin

    openapi_config = OpenAPIConfig(
        title="My API",
        description="This is the description of my API",
        version="0.1.0",
        render_plugins=[MyOpenAPIPlugin(path="/custom")],
    )

    with create_test_client(route_handlers=[], openapi_config=openapi_config) as client:
        resp = client.get("/schema/custom")
        assert resp.status_code == 200
        assert resp.headers["content-type"] == "text/html; charset=utf-8"
        assert "My UI of Choice" in resp.text
        resp = client.get("/schema/something")
        assert resp.status_code == 200
        assert resp.headers["content-type"] == "text/plain; charset=utf-8"
        assert "Something" in resp.text