Exemplo n.º 1
0
def test_recipe_book(api: API, numbers):
    api.recipe(numbers)

    response = api.client.get("/numbers/integers/3.5")
    assert response.status_code == 200
    assert response.json() == {"value": 3}

    response = api.client.get("/numbers/floats/1")
    assert response.status_code == 200
    assert response.json() == {"value": 1.0}
Exemplo n.º 2
0
def test_if_prefix_not_given_then_routes_mounted_at_slash_name(api: API):
    numbers = Recipe("numbers")

    @numbers.route("/real")
    async def real(req, res):
        pass

    api.recipe(numbers)

    assert api.url_for("numbers:real") == "/numbers/real"
Exemplo n.º 3
0
def test_if_prefix_then_routes_mounted_at_prefix(api: API):
    numbers = Recipe("numbers", prefix="/numbers-yo")

    @numbers.route("/real")
    async def real(req, res):
        pass

    api.recipe(numbers)

    assert api.url_for("numbers:real") == "/numbers-yo/real"
Exemplo n.º 4
0
def test_on_async_function_view(api: API):
    numbers = Recipe("numbers")

    with async_function_hooks() as (before, after):

        @numbers.before(before)
        @numbers.after(after)
        @numbers.route("/real")
        async def real_numbers(req, res):
            pass

        api.recipe(numbers)
        api.client.get("/numbers/real")
Exemplo n.º 5
0
def test_on_class_based_view(api: API):
    numbers = Recipe("numbers")

    with async_function_hooks() as (before, after):

        @numbers.before(before)
        @numbers.route("/real")
        class RealNumbers:
            @numbers.after(after)
            async def get(self, req, res):
                pass

        api.recipe(numbers)
        api.client.get("/numbers/real")
def test_render_sync_template_in_recipe_route(template_file: TemplateWrapper,
                                              api: API):
    numbers = Recipe("numbers")

    @numbers.route("/sync")
    def get_numbers_sync(req, res):
        res.html = numbers.template_sync(template_file.name,
                                         **template_file.context)

    api.recipe(numbers)

    response = api.client.get("/numbers/sync")
    assert response.status_code == 200
    assert response.text == template_file.rendered
Exemplo n.º 7
0
def test_websocket_recipe_route(api: API):
    chat = Recipe("chat")

    @chat.websocket_route("/room/{name}",
                          receive_type="json",
                          send_type="text")
    async def chat_room(ws: WebSocket, name: str):
        async with ws:
            message = await ws.receive()
            await ws.send(f"[{name}]: {message['text']}")

    api.recipe(chat)

    with api.client.websocket_connect("/chat/room/test") as client:
        client.send_json({"text": "Hello"})
        assert client.receive_text() == "[test]: Hello"
Exemplo n.º 8
0
def test_redirect(api: API):
    numbers = Recipe("numbers")

    @numbers.route("/R")
    async def R(req, res):
        numbers.redirect(name="numbers:real")

    @numbers.route("/real")
    async def real(req, res):
        res.text = "inf"

    api.recipe(numbers)

    response = api.client.get("/numbers/R")
    assert response.status_code == 200
    assert response.text == "inf"
Exemplo n.º 9
0
def test_use_url_for(api: API):
    foo = Recipe("foo")

    @foo.route("/bar")
    async def bar(req, res):
        pass

    @foo.route("/fez")
    async def fez(req, res):
        res.html = foo.template_string(
            "<a href=\"{{ url_for('foo:bar') }}\">To bar</a>")

    api.recipe(foo)

    response = api.client.get("/foo/fez")
    assert response.status_code == 200
    assert response.text == '<a href="/foo/bar">To bar</a>'
Exemplo n.º 10
0
# 👇 Executes `requires_token()` before the view
@courses.route("/top")
@hooks.before(requires_token)
async def get_top_courses(req, res):
    try:
        # Query parameters!
        n = int(req.query_params.get("n"))
    except TypeError:
        n = 5
    courses = [storage.get(pk)._asdict() for pk in analytics.top(n=n)]
    res.media = courses


# Mounts the routes of `courses` at `/courses` on `api`
api.recipe(courses)


# Custom error handlers!
@api.error_handler(HTTPError)
def handle_json(req, res, exc):
    res.media = {
        "error": exc.status_phrase,
        "status": exc.status_code,
        "message": "Duh!",
    }
    res.status_code = exc.status_code


if __name__ == "__main__":
    api.run()
Exemplo n.º 11
0
def test_if_templates_dir_given_then_it_is_used(api: API):
    other_dir = "my_recipe/templates"
    numbers = Recipe("numbers", templates_dir=other_dir)
    api.recipe(numbers)
    assert numbers.templates_dir == other_dir != api.templates_dir
Exemplo n.º 12
0
def test_if_templates_dir_is_that_of_api_by_default(api: API):
    numbers = Recipe("numbers")
    api.recipe(numbers)
    assert numbers.templates_dir == api.templates_dir