Skip to content

Commit 5195d42

Browse files
committed
tests/test_mixed.py: time to test all types of expectations together
1 parent 7b2fb1f commit 5195d42

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

tests/test_mixed.py

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import pytest
2+
import requests
3+
from pytest_httpserver.httpserver import HTTPServer
4+
5+
6+
def _setup_oneshot(server: HTTPServer):
7+
server.expect_request("/generic").respond_with_data("OK generic")
8+
server.expect_oneshot_request("/oneshot1").respond_with_data("OK oneshot1")
9+
server.expect_oneshot_request("/oneshot2").respond_with_data("OK oneshot2")
10+
11+
12+
def _setup_ordered(server: HTTPServer):
13+
server.expect_oneshot_request("/ordered1", ordered=True).respond_with_data("OK ordered1")
14+
server.expect_oneshot_request("/ordered2", ordered=True).respond_with_data("OK ordered2")
15+
16+
17+
def _setup_all(server: HTTPServer):
18+
_setup_oneshot(server)
19+
_setup_ordered(server)
20+
21+
22+
def test_oneshot_and_generic_happy_path1(httpserver: HTTPServer):
23+
_setup_oneshot(httpserver)
24+
assert requests.get(httpserver.url_for("/generic")).text == "OK generic"
25+
assert requests.get(httpserver.url_for("/oneshot1")).text == "OK oneshot1"
26+
assert requests.get(httpserver.url_for("/oneshot2")).text == "OK oneshot2"
27+
assert requests.get(httpserver.url_for("/generic")).text == "OK generic"
28+
29+
assert len(httpserver.oneshot_handlers) == 0
30+
31+
32+
def test_oneshot_and_generic_happy_path2(httpserver: HTTPServer):
33+
_setup_oneshot(httpserver)
34+
assert requests.get(httpserver.url_for("/generic")).text == "OK generic"
35+
assert requests.get(httpserver.url_for("/oneshot2")).text == "OK oneshot2"
36+
assert requests.get(httpserver.url_for("/oneshot1")).text == "OK oneshot1"
37+
assert requests.get(httpserver.url_for("/generic")).text == "OK generic"
38+
39+
assert len(httpserver.oneshot_handlers) == 0
40+
41+
42+
def test_all_happy_path1(httpserver: HTTPServer):
43+
_setup_all(httpserver)
44+
45+
# ordered must go first
46+
assert requests.get(httpserver.url_for("/ordered1")).text == "OK ordered1"
47+
assert requests.get(httpserver.url_for("/ordered2")).text == "OK ordered2"
48+
assert requests.get(httpserver.url_for("/generic")).text == "OK generic"
49+
assert requests.get(httpserver.url_for("/oneshot2")).text == "OK oneshot2"
50+
assert requests.get(httpserver.url_for("/oneshot1")).text == "OK oneshot1"
51+
assert requests.get(httpserver.url_for("/generic")).text == "OK generic"
52+
53+
assert len(httpserver.oneshot_handlers) == 0
54+
assert len(httpserver.ordered_handlers) == 0
55+
56+
57+
def test_all_ordered_missing(httpserver: HTTPServer):
58+
_setup_all(httpserver)
59+
60+
# ordered is missing so everything must fail
61+
# a.k.a. permanently fail
62+
63+
requests.get(httpserver.url_for("/generic"))
64+
with pytest.raises(AssertionError):
65+
httpserver.check_assertions()
66+
67+
requests.get(httpserver.url_for("/oneshot2"))
68+
with pytest.raises(AssertionError):
69+
httpserver.check_assertions()
70+
71+
requests.get(httpserver.url_for("/oneshot1"))
72+
with pytest.raises(AssertionError):
73+
httpserver.check_assertions()
74+
75+
requests.get(httpserver.url_for("/generic"))
76+
with pytest.raises(AssertionError):
77+
httpserver.check_assertions()
78+
79+
# handlers must be still intact but as the ordered are failed
80+
# everything will fail
81+
assert len(httpserver.ordered_handlers) == 2
82+
assert len(httpserver.oneshot_handlers) == 2
83+
assert len(httpserver.handlers) == 1

0 commit comments

Comments
 (0)