Beispiel #1
0
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")
Beispiel #2
0
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
Beispiel #3
0
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"),
    }
Beispiel #4
0
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"})
Beispiel #5
0
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
Beispiel #6
0
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
Beispiel #7
0
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"
Beispiel #8
0
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)