aboutsummaryrefslogtreecommitdiffstats
path: root/tests/middleware/test_catch_exceptions.py
blob: 5042f7f0a8ca20d3453c81c295489c109e7203fe (plain)
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
from wsgiref.validate import validator

from paste import catch_exceptions

from ..common_wsgi import call_app


def test_no_exception():
    @validator
    @catch_exceptions
    @validator
    def app(_, start_response):
        start_response("200 OK", [("Content-Type", "text/plain")])
        return [b"Hello, world!"]

    response = call_app(app)
    assert response.data == b"Hello, world!"
    assert response.status == "200 OK"
    assert ("Content-Type", "text/plain") in response.headers


def test_exception(capfd):
    @validator
    @catch_exceptions
    @validator
    def app(_, start_response):
        start_response("200 OK", [("Content-Type", "text/plain")])
        raise Exception("Something went wrong")

    response = call_app(app)
    assert response.data == b"500 Internal Server Error\n"
    assert response.status == "500 Internal Server Error"
    assert ("Content-Type", "text/plain") in response.headers
    out, _ = capfd.readouterr()
    assert "Exception: Something went wrong\n" in out
    assert response.exc_info is not None
    assert isinstance(response.exc_info[1], Exception)