File: test_handler_errors.py

package info (click to toggle)
pytest-httpserver 1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 908 kB
  • sloc: python: 2,382; makefile: 77; sh: 21
file content (92 lines) | stat: -rw-r--r-- 2,645 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
import pytest
import requests
import werkzeug

from pytest_httpserver import HTTPServer


def test_check_assertions_raises_handler_assertions(httpserver: HTTPServer):
    def handler(_):
        assert False  # noqa: PT015

    httpserver.expect_request("/foobar").respond_with_handler(handler)

    requests.get(httpserver.url_for("/foobar"))

    with pytest.raises(AssertionError):
        httpserver.check_assertions()

    httpserver.check_handler_errors()


def test_check_handler_errors_raises_handler_error(httpserver: HTTPServer):
    def handler(_) -> werkzeug.Response:
        raise ValueError("should be propagated")

    httpserver.expect_request("/foobar").respond_with_handler(handler)

    requests.get(httpserver.url_for("/foobar"))

    httpserver.check_assertions()

    with pytest.raises(ValueError):  # noqa: PT011
        httpserver.check_handler_errors()


def test_check_handler_errors_correct_order(httpserver: HTTPServer):
    def handler1(_) -> werkzeug.Response:
        raise ValueError("should be propagated")

    def handler2(_) -> werkzeug.Response:
        raise OSError("should be propagated")

    httpserver.expect_request("/foobar1").respond_with_handler(handler1)
    httpserver.expect_request("/foobar2").respond_with_handler(handler2)

    requests.get(httpserver.url_for("/foobar1"))
    requests.get(httpserver.url_for("/foobar2"))

    httpserver.check_assertions()

    with pytest.raises(ValueError):  # noqa: PT011
        httpserver.check_handler_errors()

    with pytest.raises(OSError):  # noqa: PT011
        httpserver.check_handler_errors()

    httpserver.check_handler_errors()


def test_missing_matcher_raises_exception(httpserver):
    requests.get(httpserver.url_for("/foobar"))

    # missing handlers should not raise handler exception here
    httpserver.check_handler_errors()

    with pytest.raises(AssertionError):
        httpserver.check_assertions()


def test_check_raises_errors_in_order(httpserver):
    def handler1(_):
        assert False  # noqa: PT015

    def handler2(_):
        pass  # does nothing

    def handler3(_):
        raise ValueError

    httpserver.expect_request("/foobar1").respond_with_handler(handler1)
    httpserver.expect_request("/foobar2").respond_with_handler(handler2)
    httpserver.expect_request("/foobar3").respond_with_handler(handler3)

    requests.get(httpserver.url_for("/foobar1"))
    requests.get(httpserver.url_for("/foobar2"))
    requests.get(httpserver.url_for("/foobar3"))

    with pytest.raises(AssertionError):
        httpserver.check()

    with pytest.raises(ValueError):  # noqa: PT011
        httpserver.check()