Skip to content

Commit b1d4a10

Browse files
Match any network filter instead of all (#155)
* Match any network filter instead of all * Add test_network_mode_multiple_hosts * Fix networking mode tests --------- Co-authored-by: sarayourfriend <[email protected]>
1 parent f5f8a3f commit b1d4a10

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

src/pook/engine.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,16 @@ def should_use_network(self, request):
382382
Returns:
383383
bool
384384
"""
385-
return self.networking and all(fn(request) for fn in self.network_filters)
385+
if not self.networking:
386+
return False
387+
388+
if not self.network_filters:
389+
# networking is enabled, and there are no filters, so
390+
# all unmatching requests should be allowed
391+
return True
392+
393+
# Otherwise, only allow if at least one of the network filters matches
394+
return any(fn(request) for fn in self.network_filters)
386395

387396
def match(self, request):
388397
"""

tests/unit/interceptors/base.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77

88
import pook
9+
from pook.exceptions import PookNoMatches
910

1011

1112
class StandardTests:
@@ -59,6 +60,10 @@ def url_404(self, httpbin):
5960
def url_500(self, httpbin):
6061
return f"{httpbin.url}/status/500"
6162

63+
@pytest.fixture
64+
def url_401(self, httpbin):
65+
return httpbin + "/status/401"
66+
6267
@pytest.mark.pook
6368
def test_activate_deactivate(self, url_404):
6469
"""Deactivating pook allows requests to go through."""
@@ -86,6 +91,69 @@ def test_network_mode(self, url_404, url_500):
8691

8792
assert status == 500
8893

94+
mocked_status, mocked_body, *_ = self.make_request("GET", url_404)
95+
96+
assert mocked_status == 200
97+
assert mocked_body == b"hello from pook"
98+
99+
@pytest.mark.pook(allow_pending_mocks=True)
100+
def test_network_mode_hostname(self, url_401):
101+
example_com = "http://example.com"
102+
pook.get(example_com).header("x-pook", "1").reply(200).body("hello from pook")
103+
# httpbin runs on loopback
104+
pook.enable_network("127.0.0.1")
105+
106+
httpbin_status, *_ = self.make_request("GET", url_401)
107+
108+
# network is enabled for httpbin hostname so it goes through
109+
assert httpbin_status == 401
110+
111+
with pytest.raises(PookNoMatches):
112+
# Make the request without query params to avoid matching the mock
113+
# which should raise a no match exception, as network mode is not enabled
114+
# for example.com hostname
115+
self.make_request("GET", example_com)
116+
117+
# this matches the mock on the header, so gets 200 with the hello from pook body
118+
example_status, body, *_ = self.make_request(
119+
"GET", example_com, headers=[("x-pook", "1")]
120+
)
121+
122+
assert example_status == 200
123+
assert body == b"hello from pook"
124+
125+
@pytest.mark.pook(allow_pending_mocks=True)
126+
def test_multiple_network_filters(self, url_401):
127+
"""When multiple network filters are added, only one is required to match for the
128+
request to be allowed through the network."""
129+
130+
def has_x_header(request: pook.Request):
131+
return request.headers.get("x-pook") == "x"
132+
133+
def has_y_header(request: pook.Request):
134+
return request.headers.get("x-pook") == "y"
135+
136+
pook.enable_network()
137+
138+
pook.use_network_filter(has_x_header, has_y_header)
139+
140+
pook.get(url_401).header("x-pook", "z").reply(200).body("hello from pook")
141+
142+
# Network filter matches, so request is allowed despite not matching a mock
143+
x_status, *_ = self.make_request("GET", url_401, headers=[("x-pook", "x")])
144+
assert x_status == 401
145+
146+
# Network filter matches, so request is allowed despite not matching a mock
147+
y_status, *_ = self.make_request("GET", url_401, headers=[("x-pook", "y")])
148+
assert y_status == 401
149+
150+
# Mock matches, so the response is mocked
151+
z_status, z_body, *_ = self.make_request(
152+
"GET", url_401, headers=[("x-pook", "z")]
153+
)
154+
assert z_status == 200
155+
assert z_body == b"hello from pook"
156+
89157
@pytest.mark.pook
90158
def test_json_request(self, url_404):
91159
"""JSON request bodies are correctly matched."""

0 commit comments

Comments
 (0)