예제 #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"})
예제 #2
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")
예제 #3
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
예제 #4
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
예제 #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)
        assert response.status_code == HTTPStatus.OK
        headers = response.headers
        assert headers["authorization"] == "Test dummy"
        assert headers.get_list("foo") == ["baz", "spam"]
예제 #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
예제 #7
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)
예제 #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
예제 #9
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"
예제 #10
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()
예제 #11
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)
예제 #12
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()
예제 #13
0
async def _(api: Api, view: type[RequestHandlerProtocol]):
    path = api.url_for(view)
    async with api.client() as client:
        response = await client.get(path, headers=wildcard_headers)
        assert response.status_code == HTTPStatus.OK
예제 #14
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
예제 #15
0
async def _(api: Api, view: type[RequestHandlerProtocol]):
    path = api.url_for(view)
    async with api.client() as client:
        response = await client.post(path, form=form_data)
        assert response.status_code == HTTPStatus.OK
예제 #16
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
예제 #17
0
async def _(api: Api, view: type[RequestHandlerProtocol], query: tuple[str, dict]):
    path = api.url_for(view, {"path": query[0]})
    async with api.client() as client:
        response = await client.get(path, queries=query[1])
        assert response.status_code == HTTPStatus.OK
예제 #18
0
async def _(api: Api, view: type[RequestHandlerProtocol], cookies: dict[str, str]):
    path = api.url_for(view)
    async with api.client() as client:
        response = await client.get(path, cookies=cookies)
        assert response.status_code == HTTPStatus.OK
예제 #19
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"
예제 #20
0
async def _(api: Api, bp: Blueprint, view: type[RequestHandlerProtocol],
            params: dict):
    path = api.url_for(view, params)
    async with api.client() as client:
        response = await client.get(path)
        assert response.json == params