Exemple #1
0
async def test_default_router_compatibility(aiohttp_client):
    app = create_app()

    # aiohug routes
    aiohug_routes = RouteTableDef()

    @aiohug_routes.get("/aiohug")
    async def aiohug_ping():
        return "pong"

    app.add_routes(aiohug_routes)

    # aiohttp routes
    aiohttp_routes = web.RouteTableDef()

    @aiohttp_routes.get("/aiohttp")
    async def aiohttp_ping(request):
        return web.Response(body="pong")

    app.add_routes(aiohttp_routes)

    client = await aiohttp_client(app)

    aiohug_resp = await client.get("/aiohug")
    assert aiohug_resp.status == 200

    aiohttp_resp = await client.get("/aiohttp")
    assert aiohttp_resp.status == 200
Exemple #2
0
def app() -> web.Application:
    routes = RouteTableDef()

    @routes.get("/")
    async def ping():
        return "pong"

    app = web.Application()
    app.add_routes(routes)
    app.add_routes(swagger_handlers)
    return app
Exemple #3
0
async def test_hello(aiohttp_client):
    routes = RouteTableDef()

    @routes.get("/hello/{name}/")
    async def hello(name: fields.String(), greeting: fields.String() = "Hello"):
        return {"msg": f"{greeting}, {name}"}

    app = create_app()
    app.add_routes(routes)

    client = await aiohttp_client(app)
    resp = await client.get("/hello/Lucy/")
    assert resp.status == 200
    assert {"msg": "Hello, Lucy"} == await resp.json()
Exemple #4
0
async def test_json_body(aiohttp_client):
    routes = RouteTableDef()

    @routes.get("/")
    async def with_body(body):
        return body

    app = create_app()
    app.add_routes(routes)

    client = await aiohttp_client(app)
    payload = {"msg": "Hello, Lucy"}
    resp = await client.get("/", json=payload)
    assert resp.status == 200
    assert await resp.json() == payload
Exemple #5
0
async def test_ping_with_request(aiohttp_client):
    routes = RouteTableDef()

    @routes.get("/ping/")
    async def ping(request):
        return "pong"

    app = create_app()
    app.add_routes(routes)

    client = await aiohttp_client(app)
    resp = await client.get("/ping/")
    assert resp.status == 200
    text = await resp.text()
    assert "pong" in text
Exemple #6
0
async def test_cast(aiohttp_client):
    routes = RouteTableDef()

    @routes.get("/number/{number}/")
    async def return_number(number: fields.Int()):
        return {"number": number}

    app = create_app()
    app.add_routes(routes)

    client = await aiohttp_client(app)
    number = 5
    resp = await client.get(f"/number/{number}/")
    assert resp.status == 200
    assert await resp.json() == {"number": number}
async def test_decorator(aiohttp_client):
    routes = RouteTableDef()

    @routes.get("/ping/")
    @decorator
    async def hello():
        return "pong"

    app = create_app()
    app.add_routes(routes)

    client = await aiohttp_client(app)
    resp = await client.get("/ping/")
    assert resp.status == 200
    text = await resp.text()
    assert "a_pong" in text
Exemple #8
0
async def test_not_valid_field(aiohttp_client):
    routes = RouteTableDef()

    @routes.get("/number/{number}/")
    async def return_number(number: fields.Int()):
        return {"number": number}

    app = create_app()
    app.add_routes(routes)

    client = await aiohttp_client(app)
    resp = await client.get(f"/number/notanumber/")
    assert resp.status == 409
    assert await resp.json() == {
        "data": {"number": ["Not a valid integer."]},
        "status": "error",
    }
Exemple #9
0
async def test_json_body_with_schema_instance(aiohttp_client):
    routes = RouteTableDef()

    class RequestSchema(Schema):
        count = fields.Int()

    @routes.get("/")
    async def with_body(body: RequestSchema()):
        return body

    app = create_app()
    app.add_routes(routes)

    client = await aiohttp_client(app)
    resp = await client.get("/", json={"count": "5"})
    assert resp.status == 200
    assert await resp.json() == {"count": 5}
Exemple #10
0
async def test_variable_not_provided_with_base_types(aiohttp_client):
    routes = RouteTableDef()

    @routes.get("/")
    async def return_number(number: int):
        return {"number": number}

    app = create_app()
    app.add_routes(routes)

    client = await aiohttp_client(app)
    resp = await client.get("/")
    assert resp.status == 409
    assert await resp.json() == {
        "data": {"number": ["Required argument"]},
        "status": "error",
    }
Exemple #11
0
async def test_not_valid_schema(aiohttp_client):
    routes = RouteTableDef()

    class RequestSchema(Schema):
        a = fields.Int()
        b = fields.Int()

    @routes.get("/")
    async def with_body(body: RequestSchema):
        return body

    app = create_app()
    app.add_routes(routes)
    client = await aiohttp_client(app)
    resp = await client.get("/", json={"a": "5", "b": "c"})
    assert resp.status == 409
    assert await resp.json() == {
        "data": {"body": {"b": ["Not a valid integer."]}},
        "status": "error",
    }
Exemple #12
0
import os
import yaml
from aiohttp import web

from aiohug import RouteTableDef
import aiohug_swagger as swagger

routes = RouteTableDef()


@swagger.spec(exclude=True)
@routes.get("/swagger.json")
async def swagger_json(request):
    return swagger.generate_swagger(request.app)


@swagger.spec(exclude=True)
@routes.get("/swagger.yaml")
async def swagger_yaml(request):
    return web.Response(text=yaml.dump(swagger.generate_swagger(request.app)),
                        content_type="text/yaml")


async def _render_template(template):
    template_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                 "templates", template)
    with open(template_path) as template:
        return template.read().replace("{{ swagger_url }}", "/swagger.json")


@swagger.spec(exclude=True)