def test_bitwise_operators(method, url, expected): pattern = ((Method("GET") | Method("post") | Method("Patch")) & URL("https://foo.bar/")) | (Method("POST") & ~URL("https://foo.bar/")) request = httpx.Request(method, url) assert bool(pattern.match(request)) is expected assert bool(~pattern.match(request)) is not expected
def test_bitwise_and(): pattern = Method("GET") & Host("foo.bar") request = httpx.Request("GET", "https://foo.bar/") match = pattern.match(request) assert match assert bool(match) is True assert not ~match
def test_iter_pattern(): pattern = M(Method("GET") & Path("/baz/") | ~Params("x=y"), url="https://foo.bar:88/") patterns = list(iter(pattern)) assert len(patterns) == 6 assert set(patterns) == { Method("GET"), Scheme("https"), Host("foo.bar"), Port(88), Path("/baz/"), Params("x=y"), }
def test_method_pattern(lookup, value, expected): request = httpx.Request("GET", "https://foo.bar/") assert bool(Method(value, lookup=lookup).match(request)) is expected
def test_merge_patterns(): pattern = Method("GET") & Path("/spam/") base = Path("/ham/", Lookup.STARTS_WITH) merged_pattern = merge_patterns(pattern, path=base) assert any([p.base == base for p in iter(merged_pattern)])
import httpcore import httpx import pytest from respx import MockResponse, Route, Router from respx.patterns import Host, M, Method @pytest.mark.parametrize( "args,kwargs,expected", [ ((Method("GET"), Host("foo.bar")), dict(), True), (tuple(), dict(method="GET", host="foo.bar"), True), ((Method("GET"), ), dict(port=443, url__regex=r"/baz/$"), True), ((Method("POST"), ), dict(host="foo.bar"), False), ((~Method("GET"), ), dict(), False), ((~M(url__regex=r"/baz/$"), ), dict(), False), (tuple(), dict(headers={"host": "foo.bar"}), True), (tuple(), dict(headers={"Content-Type": "text/plain"}), False), (tuple(), dict(headers={"cookie": "foo=bar"}), False), (tuple(), dict(cookies={"ham": "spam"}), True), ], ) def test_match_and_resolve(args, kwargs, expected): router = Router(assert_all_mocked=False) route = router.route(*args, **kwargs).respond(status_code=201) request = httpx.Request("GET", "https://foo.bar/baz/", cookies={ "foo": "bar",