Ejemplo n.º 1
0
async def _(api: Api):
    @api.route("/")
    class Index:
        pass

    @api.route("/foo")
    class Foo:
        pass

    @api.route("/foo/bar")
    class FooBar:
        pass

    @api.route("/foo/{var}")
    class FooVar:
        pass

    index = api.url_for(Index)
    api._router.get(index) == (Index, {})
    foo = api.url_for(Foo)
    api._router.get(foo) == (Foo, {})
    foobar = api.url_for(FooBar)
    api._router.get(foobar) == (FooBar, {})
    foovar = api.url_for(FooVar, {"var": "baz"})
    api._router.get(foovar) == (FooVar, {"var": "baz"})
Ejemplo n.º 2
0
async def _(api: Api, view: type[RequestHandlerProtocol]):
    path = api.url_for(view)
    async with api.client() as client:
        response = await client.get(path)
        assert response.status_code == 418
        response = await client.get(path, allow_redirects=False)
        assert response.status_code == HTTPStatus.TEMPORARY_REDIRECT
Ejemplo n.º 3
0
async def _(api: Api, bp: Blueprint, hook: type[RequestHandlerProtocol]):
    api.add_blueprint("", bp)
    async with api.client() as client:
        response = await client.get("/")
        assert response.status_code == HTTPStatus.OK
        hook_view = api._view_cache[hook]
        hook_view.mock.assert_called_once()
Ejemplo n.º 4
0
async def _(api: Api, ws: type[RequestHandlerProtocol]):
    path = api.url_for(ws)
    async with api.client() as client:
        async with client.ws_connect(path, queries=params) as connection:
            resp = await connection.receive(str)
            assert resp == "bye"
            await connection.send("FOO")
Ejemplo n.º 5
0
async def _(api: Api, view: type[RequestHandlerProtocol]):
    path = api.url_for(view)
    async with api.client() as client:
        response = await client.get(path, timeout=2)
        assert response.status_code == HTTPStatus.OK
        expected_str = "".join([f"count {i}\n" for i in range(10)])
        assert response.text == expected_str
Ejemplo n.º 6
0
async def _(api: Api, view: type[RequestHandlerProtocol]):
    path = api.url_for(view)
    async with api.client() as client:
        response = await client.get(path)
        assert response.status_code == HTTPStatus.OK
        assert response.cookies["foo"] == "bar"
        assert response.cookies["spam"] == "baz"
        assert "willbe" not in response.cookies
Ejemplo n.º 7
0
async def _(api: Api, view: type[RequestHandlerProtocol]):
    path = api.url_for(view)
    async with api.client() as client:
        response = await client.get(path)
        assert response.status_code == HTTPStatus.OK
        headers = response.headers
        assert headers["authorization"] == "Test dummy"
        assert headers.get_list("foo") == ["baz", "spam"]
Ejemplo n.º 8
0
async def _(api: Api, view: type[RequestHandlerProtocol]):
    path = api.url_for(view)
    data = {
        "foo": "bar",
        "num": "42",
        "file": ("file.bin", b"abcde" * 1000 ** 2, "application/octet-stream"),
    }
    api.max_upload_bytes = 1 * 1000 ** 2
    async with api.client() as client:
        response = await client.post(path, files=data)
        assert response.status_code == HTTPStatus.REQUEST_ENTITY_TOO_LARGE
Ejemplo n.º 9
0
async def _(
    api: Api,
    bp: Blueprint,
    view: type[RequestHandlerProtocol],
    params: dict,
):
    api.add_blueprint("", bp)
    path = api.url_for(view, params)
    async with api.client() as client:
        with raises(NotFoundError):
            await client.get(path)
Ejemplo n.º 10
0
async def _(api: Api, bp: Blueprint):
    child_bp = Blueprint()

    @child_bp.route("/child")
    class Child:
        pass

    bp.add_blueprint("/and", child_bp)
    api.add_blueprint("/parent", bp)
    async with api.client() as client:
        response = await client.get("/parent/and/child")
        assert response.status_code == HTTPStatus.OK
Ejemplo n.º 11
0
async def _(
    api: Api,
    bp: Blueprint,
    view: type[RequestHandlerProtocol],
    params: dict,
):
    api.add_blueprint("", bp)
    path = api.url_for(view, params)
    async with api.client() as client:
        response = await client.get(path)
        assert response.status_code == HTTPStatus.OK
        assert response.json["number"] == params["number"]
        assert response.json["name"] == params["name"].upper()
Ejemplo n.º 12
0
async def _(api: Api, ws: type[RequestHandlerProtocol],
            handler: type[ErrorHandlerProtocol]):
    path = api.url_for(ws)
    async with api.client() as client:
        async with client.ws_connect(path) as conn:
            await conn.send("FOO")
            resp = await conn.receive(str)
            assert resp == "hello, FOO!"
            await conn.send("end")
            resp = await conn.receive(str)
            assert resp == "error!"
            resp = await conn.receive(str)
            assert resp == "GOT ERROR"
Ejemplo n.º 13
0
def mounted_api() -> Api:
    api = Api()
    api.register_component(MountedComponent)

    @api.route("/root-component")
    class RootView:
        async def on_get(self, req: Request, resp: Response, /,
                         **kw) -> Response:
            try:
                use_component(RootComponent)
            except KeyError:
                return resp.set_status(
                    HTTPStatus.NOT_FOUND).set_text("not found")
            return resp.set_status(HTTPStatus.OK).set_text("ok")
Ejemplo n.º 14
0
async def _(
    api: Api,
    handler: ErrorHandler,
    errors: dict[str, tuple[type[Exception], int]],
    view: type[RequestHandlerProtocol],
):
    api.add_error_handler(handler)
    async with api.client() as client:
        for k, v in errors.items():
            response = await client.get(f"/{k}")
            assert response.text == k
            assert response.status_code == v[1]

        with raises(KeyError):
            response = await client.get("/NotHandled")
Ejemplo n.º 15
0
async def _(api: Api, ws: type[RequestHandlerProtocol]):
    path = api.url_for(ws)
    async with api.client() as client:
        async with client.ws_connect(path) as conn:
            await conn.send(b"FOO")
            resp = await conn.receive(bytes)
            assert resp == b"hello, FOO!"
            await conn.send(b"BAR")
            resp = await conn.receive(bytes)
            assert resp == b"hello, BAR!"
            await conn.send(b"end")
            resp = await conn.receive(bytes)
            assert resp == b"bye"
            await conn.send(b"FOO")
            with raises(RuntimeError):
                await conn.receive(bytes)
Ejemplo n.º 16
0
async def _(api: Api, store: type[AnyComponentProtocol]):
    async with api.client():
        store_instance = use_component(store, api=api)
        assert isinstance(store_instance, store)
        store_instance._startup.assert_called_once()
        store_instance._shutdown.assert_not_called()

    store_instance._shutdown.assert_called_once()
Ejemplo n.º 17
0
async def _(
    api: Api,
    after: type[RequestHandlerProtocol],
    before: type[RequestHandlerProtocol],
    index: type[RequestHandlerProtocol],
):
    with raises(KeyError):
        # not cached means not called.
        api._view_cache[before]
    with raises(KeyError):
        api._view_cache[after]
    async with api.client() as client:
        path = api.url_for(index)
        response = await client.get(path)
        assert response.status_code == HTTPStatus.OK
        after_instance = api._view_cache[after]
        after_instance.mock.assert_called_once()
Ejemplo n.º 18
0
async def _(
    api: Api,
    path: str,
    code: int,
    _: type[RequestHandlerProtocol],
):
    async with api.client() as client:
        response = await client.get(path)
        assert response.status_code == code
Ejemplo n.º 19
0
async def _(api: Api) -> None:
    async with api.client() as client:
        resp = await client.get("/root-app/root-component")
        assert resp.status_code == HTTPStatus.OK
        resp = await client.get("/root-app/mounted-component")
        assert resp.status_code == HTTPStatus.NOT_FOUND
        resp = await client.get("/mounted-app/mounted-component")
        assert resp.status_code == HTTPStatus.OK
        resp = await client.get("/mounted-app/root-component")
        assert resp.status_code == HTTPStatus.NOT_FOUND
Ejemplo n.º 20
0
def mix_routes(api: Api, bp: Blueprint):
    api.routing = "slash"

    @bp.route("/foo", routing="strict")
    class Foo:
        async def on_post(self, req, resp):
            pass

    @bp.route("/bar")
    class Bar:
        async def on_post(self, req, resp):
            pass

    @bp.route("/baz", routing="no_slash")
    class Baz:
        async def on_post(self, req, resp):
            pass

    api.add_blueprint("/", bp)
    return api
Ejemplo n.º 21
0
async def _(api: Api):
    async with api.client() as client:
        resp = await client.get("/bar", allow_redirects=False)
        assert resp.status_code == HTTPStatus.PERMANENT_REDIRECT
        assert resp.headers["location"] == "/bar/"
        resp = await client.get("/foo", allow_redirects=False)
        assert resp.status_code == HTTPStatus.OK
        resp = await client.get("/baz/", allow_redirects=False)
        assert resp.status_code == HTTPStatus.PERMANENT_REDIRECT
        assert resp.headers["location"] == "/baz"
        resp = await client.post("/bar", allow_redirects=False)
        assert resp.status_code == HTTPStatus.PERMANENT_REDIRECT
Ejemplo n.º 22
0
async def _(api: Api):
    @api.route("/")
    class Index:
        allowed_methods = ["post"]

    async with api.client() as client:
        response = await client.post("/")
        assert response.status_code == HTTPStatus.OK
        response = await client.put("/")
        assert response.status_code == HTTPStatus.METHOD_NOT_ALLOWED
        allowed = response.headers["allow"]
        assert "POST" in allowed
        assert "GET" in allowed
Ejemplo n.º 23
0
async def _(api: Api, bp: Blueprint, path_code: tuple[str, int]):
    api.routing = "clone"
    api.add_blueprint("/", bp)
    api.add_blueprint("start", bp)
    async with api.client() as client:
        path, code = path_code
        response = await client.get(path, allow_redirects=False)
        assert response.status_code == code
Ejemplo n.º 24
0
async def _(api: Api, bp: Blueprint, path_code: tuple[str, int]):
    api.routing = "no_slash"
    api.add_blueprint("/", bp)
    api.add_blueprint("start", bp)
    async with api.client() as client:
        path, code = path_code
        response = await client.get(path, allow_redirects=False)
        assert response.status_code == code
        if response.status_code == HTTPStatus.PERMANENT_REDIRECT:
            location = response.headers["location"]
            assert location == path[:-1]
Ejemplo n.º 25
0
def api():
    return Api()
Ejemplo n.º 26
0
async def _(api: Api, timeout: type[RequestHandlerProtocol]):
    async with api.client() as client:
        with raises(asyncio.TimeoutError):
            await client.get("/timeout", timeout=0.001)
Ejemplo n.º 27
0
async def _(api: Api, view: type[RequestHandlerProtocol]):
    path = api.url_for(view)
    async with api.client() as cilent:
        response = await cilent.get(path)
        assert response.status_code == HTTPStatus.OK
        assert response.content == b"send some bytes"
Ejemplo n.º 28
0
async def _(api: Api, view: type[RequestHandlerProtocol], data: dict):
    path = api.url_for(view)
    async with api.client() as client:
        response = await client.get(path)
        assert response.status_code == HTTPStatus.OK
        assert dict(**response.json) == data
Ejemplo n.º 29
0
async def _(api: Api, view: type[RequestHandlerProtocol], name: str):
    path = api.url_for(view)
    async with api.client() as client:
        response = await client.post(path, content=name.encode("utf8"))
        assert response.status_code == HTTPStatus.OK
        assert escape(name) in response.text
Ejemplo n.º 30
0
def api():
    return Api(templates_dir="tests/templates")