|
6 | 6 | import pytest |
7 | 7 |
|
8 | 8 | import httpx |
9 | | -from httpx._utils import build_url_pattern, get_environment_proxies |
| 9 | +from httpx._utils import ( |
| 10 | + IPNetPattern, |
| 11 | + WildcardURLPattern, |
| 12 | + build_url_pattern, |
| 13 | + get_environment_proxies, |
| 14 | +) |
10 | 15 |
|
11 | 16 |
|
12 | 17 | @pytest.mark.parametrize( |
@@ -139,17 +144,45 @@ def test_url_matches(pattern, url, expected): |
139 | 144 | assert pattern.matches(httpx.URL(url)) == expected |
140 | 145 |
|
141 | 146 |
|
| 147 | +@pytest.mark.parametrize( |
| 148 | + ["pattern", "url", "expected"], |
| 149 | + [ |
| 150 | + ("all://192.168.0.0/24", "http://192.168.0.1", True), |
| 151 | + ("all://192.168.0.1", "http://192.168.0.1", True), |
| 152 | + ("all://192.168.0.0/24", "foobar", False), |
| 153 | + ], |
| 154 | +) |
| 155 | +def test_IPNetPattern(pattern, url, expected): |
| 156 | + proto, rest = pattern.split("://", 1) |
| 157 | + pattern = IPNetPattern(rest) |
| 158 | + assert pattern.matches(httpx.URL(url)) == expected |
| 159 | + |
| 160 | + |
| 161 | +def test_build_url_pattern(): |
| 162 | + pattern1 = build_url_pattern("all://192.168.0.0/16") |
| 163 | + pattern2 = build_url_pattern("all://192.168.0.0/16") |
| 164 | + pattern3 = build_url_pattern("all://192.168.0.1") |
| 165 | + assert isinstance(pattern1, IPNetPattern) |
| 166 | + assert isinstance(pattern2, IPNetPattern) |
| 167 | + assert isinstance(pattern3, WildcardURLPattern) |
| 168 | + assert pattern1 == pattern2 |
| 169 | + assert pattern2 != pattern3 |
| 170 | + assert pattern1 < pattern3 |
| 171 | + assert hash(pattern1) == hash(pattern2) |
| 172 | + assert hash(pattern2) != hash(pattern3) |
| 173 | + |
| 174 | + |
142 | 175 | def test_pattern_priority(): |
143 | 176 | matchers = [ |
144 | 177 | build_url_pattern("all://"), |
145 | 178 | build_url_pattern("http://"), |
146 | 179 | build_url_pattern("http://example.com"), |
147 | 180 | build_url_pattern("http://example.com:123"), |
148 | | - build_url_pattern("192.168.0.1/16"), |
| 181 | + build_url_pattern("all://192.168.0.0/16"), |
149 | 182 | ] |
150 | 183 | random.shuffle(matchers) |
151 | 184 | assert sorted(matchers) == [ |
152 | | - build_url_pattern("192.168.0.1/16"), |
| 185 | + build_url_pattern("all://192.168.0.0/16"), |
153 | 186 | build_url_pattern("http://example.com:123"), |
154 | 187 | build_url_pattern("http://example.com"), |
155 | 188 | build_url_pattern("http://"), |
|
0 commit comments