File: test_permanent.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 (122 lines) | stat: -rw-r--r-- 4,629 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
119
120
121
122
import pytest
import requests
from werkzeug import Response

from pytest_httpserver import HTTPServer

JSON_STRING = '{"foo": "bar"}'


def test_expected_request_json(httpserver: HTTPServer):
    httpserver.expect_request("/foobar").respond_with_json({"foo": "bar"})
    assert requests.get(httpserver.url_for("/foobar")).json() == {"foo": "bar"}


def test_expected_request_data(httpserver: HTTPServer):
    httpserver.expect_request("/foobar").respond_with_data(JSON_STRING)
    assert requests.get(httpserver.url_for("/foobar")).json() == {"foo": "bar"}


def test_expected_request_handler(httpserver: HTTPServer):
    httpserver.expect_request("/foobar").respond_with_handler(lambda request: JSON_STRING)  # type: ignore
    assert requests.get(httpserver.url_for("/foobar")).json() == {"foo": "bar"}


def test_expected_request_response(httpserver: HTTPServer):
    httpserver.expect_request("/foobar").respond_with_response(Response(JSON_STRING))
    assert requests.get(httpserver.url_for("/foobar")).json() == {"foo": "bar"}


def test_expected_request_response_as_string(httpserver: HTTPServer):
    httpserver.expect_request("/foobar").respond_with_response(JSON_STRING)  # type: ignore
    assert requests.get(httpserver.url_for("/foobar")).json() == {"foo": "bar"}


def test_request_post(httpserver: HTTPServer):
    httpserver.expect_request("/foobar", data='{"request": "example"}', method="POST").respond_with_data(
        "example_response"
    )
    response = requests.post(httpserver.url_for("/foobar"), json={"request": "example"})
    httpserver.check_assertions()
    assert response.text == "example_response"
    assert response.status_code == 200


def test_request_post_case_insensitive_method(httpserver: HTTPServer):
    httpserver.expect_request("/foobar", data='{"request": "example"}', method="post").respond_with_data(
        "example_response"
    )
    response = requests.post(httpserver.url_for("/foobar"), json={"request": "example"})
    httpserver.check_assertions()
    assert response.text == "example_response"
    assert response.status_code == 200


def test_request_any_method(httpserver: HTTPServer):
    httpserver.expect_request("/foobar").respond_with_data("OK")
    response = requests.post(httpserver.url_for("/foobar"))
    assert response.text == "OK"
    assert response.status_code == 200

    response = requests.delete(httpserver.url_for("/foobar"))
    assert response.text == "OK"
    assert response.status_code == 200

    response = requests.put(httpserver.url_for("/foobar"))
    assert response.text == "OK"
    assert response.status_code == 200

    response = requests.patch(httpserver.url_for("/foobar"))
    assert response.text == "OK"
    assert response.status_code == 200

    response = requests.get(httpserver.url_for("/foobar"))
    assert response.text == "OK"
    assert response.status_code == 200


def test_unexpected_request(httpserver: HTTPServer):
    httpserver.expect_request("/foobar").respond_with_json({"foo": "bar"})
    requests.get(httpserver.url_for("/nonexists"))
    with pytest.raises(AssertionError):
        httpserver.check_assertions()


def test_no_handler_status_code(httpserver: HTTPServer):
    httpserver.no_handler_status_code = 404
    assert requests.get(httpserver.url_for("/foobar")).status_code == 404


def test_server_cleared_for_each_test(httpserver: HTTPServer):
    assert httpserver.log == []
    assert httpserver.assertions == []
    assert httpserver.ordered_handlers == []
    assert httpserver.oneshot_handlers == []
    assert httpserver.handlers == []


def test_response_handler_replaced(httpserver: HTTPServer):
    # https://github.com/csernazs/pytest-httpserver/issues/229
    handler = httpserver.expect_request("/foobar")
    handler.respond_with_data("FOO")
    response = requests.get(httpserver.url_for("/foobar"))
    assert response.text == "FOO"
    assert response.status_code == 200
    handler.respond_with_json({"foo": "bar"})
    response = requests.get(httpserver.url_for("/foobar"))
    assert response.json() == {"foo": "bar"}
    assert response.status_code == 200


def test_request_handler_repr(httpserver: HTTPServer):
    handler = httpserver.expect_request("/foo", method="POST")
    assert (
        repr(handler)
        == "<RequestHandler uri='/foo' method='POST' query_string=None headers={} data=None json=<UNDEFINED>>"
    )

    handler = httpserver.expect_request("/query", query_string={"a": "123"})
    assert (
        repr(handler) == "<RequestHandler uri='/query' method='__ALL' query_string={'a': '123'} "
        "headers={} data=None json=<UNDEFINED>>"
    )