Skip to content

Commit 71c6448

Browse files
committed
Fix unittests
1 parent 1e52631 commit 71c6448

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

httpx/_utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,18 @@ def peek_filelike_length(stream: typing.Any) -> int | None:
127127
class Pattern(typing.Protocol):
128128
@abstractmethod
129129
def matches(self, other: URL) -> bool:
130-
pass
130+
"""this method should never be accessed"""
131131

132132
@property
133133
@abstractmethod
134134
def priority(self) -> tuple[int, int, int]:
135-
pass
135+
"""this property should never be accessed"""
136+
137+
def __lt__(self, other: Pattern) -> bool:
138+
"""this method should never be accessed"""
139+
140+
def __eq__(self, other: typing.Any) -> bool:
141+
"""this method should never be accessed"""
136142

137143

138144
class WildcardURLPattern(Pattern):

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def serve_in_thread(server: TestServer) -> typing.Iterator[TestServer]:
273273
thread.start()
274274
try:
275275
while not server.started:
276-
time.sleep(1e-3)
276+
time.sleep(1)
277277
yield server
278278
finally:
279279
server.should_exit = True

tests/test_utils.py

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

88
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+
)
1015

1116

1217
@pytest.mark.parametrize(
@@ -139,17 +144,45 @@ def test_url_matches(pattern, url, expected):
139144
assert pattern.matches(httpx.URL(url)) == expected
140145

141146

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+
142175
def test_pattern_priority():
143176
matchers = [
144177
build_url_pattern("all://"),
145178
build_url_pattern("http://"),
146179
build_url_pattern("http://example.com"),
147180
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"),
149182
]
150183
random.shuffle(matchers)
151184
assert sorted(matchers) == [
152-
build_url_pattern("192.168.0.1/16"),
185+
build_url_pattern("all://192.168.0.0/16"),
153186
build_url_pattern("http://example.com:123"),
154187
build_url_pattern("http://example.com"),
155188
build_url_pattern("http://"),

0 commit comments

Comments
 (0)