From aa918e2495105cef59af261b8ee6477b708cb95b Mon Sep 17 00:00:00 2001 From: Bill Peck Date: Tue, 24 Feb 2026 21:09:16 -0500 Subject: [PATCH] Fix unittests --- httpx/_utils.py | 10 ++++++++-- tests/test_utils.py | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/httpx/_utils.py b/httpx/_utils.py index 7d1653c8..b492e5b0 100644 --- a/httpx/_utils.py +++ b/httpx/_utils.py @@ -127,12 +127,18 @@ def peek_filelike_length(stream: typing.Any) -> int | None: class Pattern(typing.Protocol): @abstractmethod def matches(self, other: URL) -> bool: - pass + """this method should never be accessed""" @property @abstractmethod def priority(self) -> tuple[int, int, int]: - pass + """this property should never be accessed""" + + def __lt__(self, other: Pattern) -> bool: + """this method should never be accessed""" + + def __eq__(self, other: typing.Any) -> bool: + """this method should never be accessed""" class WildcardURLPattern(Pattern): diff --git a/tests/test_utils.py b/tests/test_utils.py index d42570ed..01e3566b 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -6,7 +6,12 @@ import random import pytest import httpx -from httpx._utils import build_url_pattern, get_environment_proxies +from httpx._utils import ( + IPNetPattern, + WildcardURLPattern, + build_url_pattern, + get_environment_proxies, +) @pytest.mark.parametrize( @@ -139,17 +144,45 @@ def test_url_matches(pattern, url, expected): assert pattern.matches(httpx.URL(url)) == expected +@pytest.mark.parametrize( + ["pattern", "url", "expected"], + [ + ("all://192.168.0.0/24", "http://192.168.0.1", True), + ("all://192.168.0.1", "http://192.168.0.1", True), + ("all://192.168.0.0/24", "foobar", False), + ], +) +def test_IPNetPattern(pattern, url, expected): + proto, rest = pattern.split("://", 1) + pattern = IPNetPattern(rest) + assert pattern.matches(httpx.URL(url)) == expected + + +def test_build_url_pattern(): + pattern1 = build_url_pattern("all://192.168.0.0/16") + pattern2 = build_url_pattern("all://192.168.0.0/16") + pattern3 = build_url_pattern("all://192.168.0.1") + assert isinstance(pattern1, IPNetPattern) + assert isinstance(pattern2, IPNetPattern) + assert isinstance(pattern3, WildcardURLPattern) + assert pattern1 == pattern2 + assert pattern2 != pattern3 + assert pattern1 < pattern3 + assert hash(pattern1) == hash(pattern2) + assert hash(pattern2) != hash(pattern3) + + def test_pattern_priority(): matchers = [ build_url_pattern("all://"), build_url_pattern("http://"), build_url_pattern("http://example.com"), build_url_pattern("http://example.com:123"), - build_url_pattern("192.168.0.1/16"), + build_url_pattern("all://192.168.0.0/16"), ] random.shuffle(matchers) assert sorted(matchers) == [ - build_url_pattern("192.168.0.1/16"), + build_url_pattern("all://192.168.0.0/16"), build_url_pattern("http://example.com:123"), build_url_pattern("http://example.com"), build_url_pattern("http://"),