|
6 | 6 | import pytest
|
7 | 7 |
|
8 | 8 | import pook
|
| 9 | +from pook.exceptions import PookNoMatches |
9 | 10 |
|
10 | 11 |
|
11 | 12 | class StandardTests:
|
@@ -59,6 +60,10 @@ def url_404(self, httpbin):
|
59 | 60 | def url_500(self, httpbin):
|
60 | 61 | return f"{httpbin.url}/status/500"
|
61 | 62 |
|
| 63 | + @pytest.fixture |
| 64 | + def url_401(self, httpbin): |
| 65 | + return httpbin + "/status/401" |
| 66 | + |
62 | 67 | @pytest.mark.pook
|
63 | 68 | def test_activate_deactivate(self, url_404):
|
64 | 69 | """Deactivating pook allows requests to go through."""
|
@@ -86,6 +91,69 @@ def test_network_mode(self, url_404, url_500):
|
86 | 91 |
|
87 | 92 | assert status == 500
|
88 | 93 |
|
| 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 | + |
89 | 157 | @pytest.mark.pook
|
90 | 158 | def test_json_request(self, url_404):
|
91 | 159 | """JSON request bodies are correctly matched."""
|
|
0 commit comments