Exemple #1
0
def test_create_routes_from_namespace(specs):
    handlers = Handlers()

    # not - strict
    try:
        routes = create_routes_from_namespace(specs, handlers, strict=False)
    except Exception:  # pylint: disable=W0703
        pytest.fail("Non-strict failed", pytrace=True)

    # strict
    with pytest.raises((RuntimeError, ValueError)):
        routes = create_routes_from_namespace(specs, handlers, strict=True)

    # Removing non-spec handler
    handlers.get_health_wrong = None
    routes = create_routes_from_namespace(specs, handlers, strict=True)

    assert len(routes) == len(specs.paths)
    for rdef in routes:
        assert rdef.method == "GET"
Exemple #2
0
def test_prepends_basepath(specs):

    # not - strict
    try:
        handlers = Handlers()
        routes = create_routes_from_namespace(specs, handlers, strict=False)
    except Exception:  # pylint: disable=W0703
        pytest.fail("Non-strict failed", pytrace=True)

    basepath = openapi.get_base_path(specs)
    for route in routes:
        assert route.path.startswith(basepath)
        assert route.handler.__name__[len("get_"):] in route.path
Exemple #3
0
def test_filtered_routing(specs):
    handlers = Handlers()
    found = get_handlers_from_namespace(handlers)

    hdl_sel = {name: hdl for name, hdl in found.items() if "i" in name}
    opr_iter = ((mth, url, opname, _tags)
                for mth, url, opname, _tags in iter_path_operations(specs)
                if "i" in opname)

    routes = map_handlers_with_operations(hdl_sel, opr_iter, strict=True)

    for rdef in routes:
        assert rdef.method == "GET"
        assert rdef.handler in hdl_sel.values()
def client(loop, aiohttp_client, specs):
    app = web.Application()

    # routes
    handlers = Handlers()
    routes = create_routes_from_namespace(specs, handlers, strict=False)
    app.router.add_routes(routes)

    # validators
    app[APP_OPENAPI_SPECS_KEY] = specs

    # middlewares
    base = openapi.get_base_path(specs)
    app.middlewares.append(error_middleware_factory(base))
    app.middlewares.append(envelope_middleware_factory(base))

    return loop.run_until_complete(aiohttp_client(app))
    routes = create_routes_from_namespace(specs, handlers, strict=False)
    app.router.add_routes(routes)

    # validators
    app[APP_OPENAPI_SPECS_KEY] = specs

    # middlewares
    base = openapi.get_base_path(specs)
    app.middlewares.append(error_middleware_factory(base))
    app.middlewares.append(envelope_middleware_factory(base))

    return loop.run_until_complete(aiohttp_client(app))


@pytest.mark.parametrize("path,expected_data",
                         [("/health", Handlers.get('health')),
                          ("/dict", Handlers.get('dict')),
                          ("/envelope", Handlers.get('envelope')['data']),
                          ("/list", Handlers.get('list')),
                          ("/attobj", Handlers.get('attobj')),
                          ("/string", Handlers.get('string')),
                          ("/number", Handlers.get('number')),
                          ("/mixed", Handlers.get('mixed'))])
async def test_envelope_middleware(path, expected_data, client, specs):
    base = openapi.get_base_path(specs)
    response = await client.get(base + path)
    payload = await response.json()

    assert is_enveloped(payload)

    data, error = unwrap_envelope(payload)
    # validators
    app[APP_OPENAPI_SPECS_KEY] = specs

    # middlewares
    base = openapi.get_base_path(specs)
    app.middlewares.append(error_middleware_factory(base))
    app.middlewares.append(envelope_middleware_factory(base))

    return event_loop.run_until_complete(aiohttp_client(app))


@pytest.mark.parametrize(
    "path,expected_data",
    [
        ("/health", Handlers.get("health")),
        ("/dict", Handlers.get("dict")),
        ("/envelope", Handlers.get("envelope")["data"]),
        ("/list", Handlers.get("list")),
        ("/attobj", Handlers.get("attobj")),
        ("/string", Handlers.get("string")),
        ("/number", Handlers.get("number")),
        ("/mixed", Handlers.get("mixed")),
    ],
)
async def test_envelope_middleware(path, expected_data, client, specs):
    base = openapi.get_base_path(specs)
    response = await client.get(base + path)
    payload = await response.json()

    assert is_enveloped(payload)