forked from csernazs/pytest-httpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_permanent.py
More file actions
92 lines (63 loc) · 3.48 KB
/
test_permanent.py
File metadata and controls
92 lines (63 loc) · 3.48 KB
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 requests
import pytest
from werkzeug.wrappers 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 == []