def test_invalid_pattern(): with pytest.raises(KeyError, match="is not a valid Pattern"): M(foo="baz") with pytest.raises(NotImplementedError, match="pattern does not support"): Scheme("http", Lookup.REGEX) with pytest.raises(ValueError, match="is not a valid Lookup"): M(scheme__baz="zoo")
def test_json_pattern_path(json, path, value, expected): request = httpx.Request("POST", "https://foo.bar/", json=json) pattern = M(**{f"json__{path}": value}) if type(expected) is bool: match = pattern.match(request) assert bool(match) is expected elif issubclass(expected, Exception): with pytest.raises(expected): pattern.match(request) else: raise AssertionError() # pragma: nocover
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_add(): with respx.mock: route = Route(method="GET", url="https://foo.bar/") respx.add(route, name="foobar") response = httpx.get("https://foo.bar/") assert response.status_code == 200 assert respx.routes["foobar"].called with pytest.raises(TypeError): respx.add(route, status_code=418) # pragma: nocover with pytest.raises(ValueError): respx.add("GET") # pragma: nocover with pytest.raises(NotImplementedError): route.name = "spam" with pytest.raises(NotImplementedError): route.pattern &= M(params={"foo": "bar"})
def test_json_pattern_path(json, path, value, expected): request = httpx.Request("POST", "https://foo.bar/", json=json) pattern = M(**{f"json__{path}": value}) match = pattern.match(request) assert bool(match) is expected
def test_url_pattern_hash(): p = Host("foo.bar") & Path("/baz/") assert M(url="//foo.bar/baz/") == p p = Scheme("https") & Host("foo.bar") & Path("/baz/") assert M(url="https://foo.bar/baz/") == p
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", "ham": "spam"
import unittest import httpx from respx.patterns import M from iaqualink.client import AqualinkClient dotstar = M(host__regex=".*") resp_200 = httpx.Response(status_code=200, json={}) class TestBase(unittest.IsolatedAsyncioTestCase): __test__ = False def __init_subclass__(cls) -> None: if cls.__name__.startswith("TestBase"): setattr(cls, "__test__", False) else: setattr(cls, "__test__", True) return super().__init_subclass__() def setUp(self) -> None: super().setUp() self.client = AqualinkClient("foo", "bar") self.addAsyncCleanup(self.client.close)