File: test_exceptions.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 (48 lines) | stat: -rw-r--r-- 1,457 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
from docs.examples.exceptions import (
    layered_handlers,
    override_default_handler,
    per_exception_handlers,
)

from litestar.testing import TestClient


def test_override_default_handler() -> None:
    with TestClient(app=override_default_handler.app) as client:
        res = client.get("/")
        assert res.status_code == 400
        assert res.text == "an error occurred"


def test_per_exception_handlers() -> None:
    with TestClient(app=per_exception_handlers.app) as client:
        res = client.get("/validation-error")
        assert res.status_code == 400
        assert res.text.startswith("validation error:")

        res = client.get("/server-error")
        assert res.status_code == 500
        assert res.text == "server error: 500: Internal Server Error"

        res = client.get("/value-error")
        assert res.status_code == 400
        assert res.text == "value error: this is wrong"


def test_layered_handlers() -> None:
    with TestClient(app=layered_handlers.app) as client:
        res = client.get("/")
        assert res.status_code == 500
        assert res.json() == {
            "error": "server error",
            "path": "/",
            "detail": "something's gone wrong",
            "status_code": 500,
        }

        res = client.get("/greet")
        assert res.status_code == 400
        assert res.json() == {
            "error": "validation error",
            "path": "/greet",
        }