コード例 #1
0
def test_parse_url_patterns():
    patterns = parse_url_patterns("https://foo.bar:443/ham/spam/?egg=yolk")
    assert patterns == {
        "scheme": Scheme("https"),
        "host": Host("foo.bar"),
        "path": Path("/ham/spam/"),
        "params": Params({"egg": "yolk"}, Lookup.EQUAL),
    }

    patterns = parse_url_patterns("https://foo.bar:1337/ham/spam/?egg=yolk")
    assert patterns == {
        "scheme": Scheme("https"),
        "host": Host("foo.bar"),
        "port": Port(1337),
        "path": Path("/ham/spam/"),
        "params": Params({"egg": "yolk"}, Lookup.EQUAL),
    }

    patterns = parse_url_patterns("https://foo.bar/ham/spam/?egg=yolk",
                                  exact=False)
    assert patterns == {
        "scheme": Scheme("https"),
        "host": Host("foo.bar"),
        "path": Path("/ham/spam/", Lookup.STARTS_WITH),
        "params": Params({"egg": "yolk"}, Lookup.CONTAINS),
    }

    patterns = parse_url_patterns("all://*.foo.bar")
    assert len(patterns) == 1
    assert "host" in patterns
    assert patterns["host"].lookup is Lookup.REGEX

    patterns = parse_url_patterns("all")
    assert len(patterns) == 0
コード例 #2
0
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
コード例 #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"),
    }
コード例 #4
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
コード例 #5
0
def test_host_pattern(lookup, host, expected):
    request = httpx.Request("GET", "https://foo.bar/")
    assert bool(Host(host, lookup=lookup).match(request)) is expected
コード例 #6
0
ファイル: test_patterns.py プロジェクト: SlavaSkvortsov/respx
def test_host_pattern(host, expected):
    _request = httpx.Request("GET", "https://foo.bar/")
    for request in (_request, encode(_request)):
        assert bool(Host(host).match(request)) is expected
コード例 #7
0
ファイル: test_router.py プロジェクト: SlavaSkvortsov/respx
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",