Ejemplo n.º 1
0
async def test_mg_fork_raises(rq):
    with pytest.raises(HttpException) as e:
        await MgFork(
            FkRegex(pattern="/foo-bar", resp=RsText(text="resp1")),
            FkRegex(pattern="/hello-world", resp=RsText(text="resp2")),
        ).act(rq)
    assert e.value.code() == 404
Ejemplo n.º 2
0
async def test_fk_chain_has_match():
    rs = await FkChain(
        FkRegex(r"/foo", resp=RsText("Foo!")),
        FkRegex(r"/bar", resp=RsText("Bar!")),
        FkRegex(r"/baz", resp=RsText("Baz!")),
    ).route(RqFake(url="/bar"))
    assert await whole_body_of(rs) == b"Bar!"
Ejemplo n.º 3
0
async def test_rs_text():
    rs = RsText(b"12345", RsWithHeaders(RsWithStatus(204), {"foo": "bar"}))
    assert await rs.status() == "204 No Content"
    assert await rs.headers() == {"Content-Type": "text/plain", "foo": "bar"}
    body = rs.body()
    assert await body.__anext__() == b"12345"
    with pytest.raises(StopAsyncIteration):
        await body.__anext__()
Ejemplo n.º 4
0
async def test_rs_text_str(text, expected_status, expected_headers, expected_text):
    rs = RsText(text)
    assert await rs.status() == "200 OK"
    assert await rs.headers() == {"Content-Type": "text/plain"}
    body = rs.body()
    assert await body.__anext__() == expected_text
    with pytest.raises(StopAsyncIteration):
        await body.__anext__()
Ejemplo n.º 5
0
async def test_mg_fork_with_fk_regex(rq, expected_response):
    resp: Optional[Response] = await MgFork(
        FkRegex(pattern="/foo-bar", resp=RsText(text="resp1")),
        FkRegex(pattern="/hello-world", resp=RsText(text="resp2")),
        FkRegex(pattern="/hello.*", resp=RsText(text="resp3")),
        FkRegex(pattern=re.compile("/bang[0-9]"), resp=RsText(text="resp4")),
        FkRegex(pattern="/foo-baz", resp=RsText(text="resp5")),
    ).act(rq)
    if expected_response is None:
        assert resp is None
    else:
        assert resp is not None
        assert (await resp.body().__anext__() == await
                expected_response.body().__anext__())
        assert await resp.status() == await expected_response.status()
        assert await resp.headers() == await expected_response.headers()
Ejemplo n.º 6
0
async def test_fk_encoding_no_match(fk, rq):
    assert (
        await FkEncoding(fk, resp=RsText("Foo!")).route(
            RqFake(headers={"accept-encoding": rq})
        )
        is None
    )
Ejemplo n.º 7
0
async def test_app_timeable_raises():
    async with TestClient(
            AppTimeable(timeout=0.5,
                        app=AppBasic(
                            MgDelayed(1.0, MgFixed(
                                RsText("Hello there!")))))) as client:
        with pytest.raises(asyncio.TimeoutError):
            await client.get("/")
Ejemplo n.º 8
0
async def test_app_timeable_works():
    async with TestClient(
            AppTimeable(timeout=3.0,
                        app=AppBasic(
                            MgDelayed(1.0, MgFixed(
                                RsText("Hello there!")))))) as client:
        resp = await client.get("/")
        assert resp.status_code == 200
        assert resp.content == b"Hello there!"
Ejemplo n.º 9
0
async def test_mg_auth_logs_in_with_cookie():
    rs = await MgAuth(MgFixed(RsText()), PsCookie(CcPlain())).act(
        RqFake(headers={"cookie": "PsCookie=urn%3Atest%3A99"}))
    assert await rs.headers() == {
        "Content-Type":
        "text/plain",
        "Set-Cookie":
        "PsCookie=urn%3Atest%3A99;Path=/;HttpOnly;Expires=Sat, 13 Feb 2021 03:21:34 GMT;",
    }
Ejemplo n.º 10
0
 def __init__(self, types: Collection[str], resp: Union[Muggle, Response, str]):
     self._types: Collection[str] = types
     self._mg: Muggle
     if isinstance(resp, str):
         self._mg = MgFixed(RsText(resp))
     elif isinstance(resp, Response):
         self._mg = MgFixed(resp)
     elif isinstance(resp, Muggle):
         self._mg = resp
     else:
         raise TypeError("Expected Response, Muggle or str. Got: %r" % type(resp))
Ejemplo n.º 11
0
 def __init__(
     self, pattern: Union[str, Pattern], *, resp: Union[Muggle, Response, str]
 ):
     self._pattern: Pattern = (
         pattern if isinstance(pattern, re.Pattern) else re.compile(pattern)
     )
     self._mg: Muggle
     if isinstance(resp, str):
         self._mg = MgFixed(RsText(resp))
     elif isinstance(resp, Response):
         self._mg = MgFixed(resp)
     elif isinstance(resp, Muggle):
         self._mg = resp
     else:
         raise TypeError("Expected Response, Muggle or str. Got: %r" % type(resp))
Ejemplo n.º 12
0
async def test_app_with_lifespan_uses_callbacks():
    startup = AsyncMock(return_value=None)
    shutdown = AsyncMock(return_value=None)
    seal(startup), seal(shutdown)
    async with TestClient(
            AppWithLifespan(
                app=AppBasic(MgFixed(RsText("Hello there!"))),
                startup=startup,
                shutdown=shutdown,
            ), ) as client:
        startup.assert_awaited_once()
        shutdown.assert_not_awaited()
        resp = await client.get("/")
        assert resp.status_code == 200
        assert resp.content == b"Hello there!"
    shutdown.assert_awaited_once()
Ejemplo n.º 13
0
async def test_fk_regex_has_match(arg):
    rs = await FkRegex(arg, resp=RsText("Foo!")).route(RqFake(url="/foo"))
    assert await whole_body_of(rs) == b"Foo!"
Ejemplo n.º 14
0
async def test_fk_regex_no_match(arg):
    assert (await FkRegex(arg, resp=RsText("Foo!")).route(
        RqFake(url="/idontmatch")) is None)
Ejemplo n.º 15
0
async def test_fk_encoding_matches(fk, rq):
    rs = await FkEncoding(fk, resp=RsText("Foo!")).route(
        RqFake(headers={"accept-encoding": rq})
    )
    assert await whole_body_of(rs) == b"Foo!"
Ejemplo n.º 16
0
async def test_fk_content_type_no_match(fk, rq):
    assert (await FkContentType([fk], resp=RsText("Foo!")).route(
        RqFake(headers={"content-type": rq})) is None)
Ejemplo n.º 17
0
async def test_fk_chain_no_match():
    assert (await FkChain(
        FkRegex(r"/foo", resp=RsText("Foo!")),
        FkRegex(r"/bar", resp=RsText("Bar!")),
        FkRegex(r"/baz", resp=RsText("Baz!")),
    ).route(RqFake(url="/idontmatch")) is None)
Ejemplo n.º 18
0
async def test_fk_types_no_match(fk, rq):
    assert (await FkTypes([fk], resp=RsText("Foo!")).route(
        RqFake(headers={"accept": rq})) is None)
Ejemplo n.º 19
0
async def test_fk_types_matches(fk, rq):
    rs = await FkTypes([fk], resp=RsText("Foo!")).route(
        RqFake(headers={"accept": rq}))
    assert await whole_body_of(rs) == b"Foo!"
Ejemplo n.º 20
0
async def test_fk_content_type_matches(fk, rq):
    rs = await FkContentType([fk], resp=RsText("Foo!")).route(
        RqFake(headers={"content-type": rq}))
    assert await whole_body_of(rs) == b"Foo!"
Ejemplo n.º 21
0
import pytest

from muggle.facets.fork.fk_regex import FkRegex
from muggle.facets.fork.mg_fork import MgFork
from muggle.http_exception import HttpException
from muggle.response import Response
from muggle.rq.rq_fake import RqFake
from muggle.rs.rs_text import RsText


@pytest.mark.asyncio
@pytest.mark.parametrize(
    "rq, expected_response",
    [
        [RqFake("/foo-bar"), RsText(text="resp1")],
        [RqFake("/hello-world"), RsText(text="resp2")],
        [RqFake("/hello-friend"),
         RsText(text="resp3")],
        [RqFake("/bang7"), RsText(text="resp4")],
    ],
)
async def test_mg_fork_with_fk_regex(rq, expected_response):
    resp: Optional[Response] = await MgFork(
        FkRegex(pattern="/foo-bar", resp=RsText(text="resp1")),
        FkRegex(pattern="/hello-world", resp=RsText(text="resp2")),
        FkRegex(pattern="/hello.*", resp=RsText(text="resp3")),
        FkRegex(pattern=re.compile("/bang[0-9]"), resp=RsText(text="resp4")),
        FkRegex(pattern="/foo-baz", resp=RsText(text="resp5")),
    ).act(rq)
    if expected_response is None: