def test_file_path(self):
        router = OpenApiRouter()
        (resource, method_map, _, uri) = router.find("/foo")

        assert "controllers.foo.Foo" in str(resource)

        assert len(method_map) >= 4
        assert "GET" in method_map
        assert "POST" in method_map
        assert "PUT" in method_map
        assert "OPTIONS" in method_map

        get_method = method_map["GET"]
        post_method = method_map["POST"]
        put_method = method_map["PUT"]

        get_resp = TestResponse()
        post_resp = TestResponse()
        put_resp = TestResponse()

        get_method({}, get_resp)
        post_method({}, post_resp)
        put_method({}, put_resp)

        assert get_resp.status == "200 OK"
        assert post_resp.status == "200 OK"
        assert put_resp.status == "418 I'm a teapot"

        assert get_resp.json == {"method": "get"}
        assert post_resp.json == {"method": "post"}
        assert put_resp.json == {"method": "put"}

        assert uri == "/foo"
    def test_raw_yaml(self):
        spec = {
            "paths": {
                "/foo": {
                    "get": {
                        "operationId": "controllers.foo.Foo.on_get"
                    }
                }
            }
        }

        router = OpenApiRouter(raw_yaml=yaml.dump(spec))
        (resource, method_map, _, uri) = router.find("/foo")

        assert "controllers.foo.Foo" in str(resource)

        assert len(method_map) >= 2
        assert "GET" in method_map
        assert "OPTIONS" in method_map

        get_method = method_map["GET"]
        get_resp = TestResponse()
        get_method({}, get_resp)

        assert get_resp.status == "200 OK"
        assert get_resp.json == {"method": "get"}

        assert uri == "/foo"
Пример #3
0
    def test_raw_yaml(self):
        spec = {
            'paths': {
                '/foo': {
                    'get': {
                        'operationId': 'controllers.foo.Foo.on_get'
                    }
                }
            }
        }

        router = OpenApiRouter(raw_yaml=yaml.dump(spec))
        (resource, method_map, _, uri) = router.find('/foo')

        assert 'controllers.foo.Foo' in str(resource)

        assert len(method_map) >= 2
        assert 'GET' in method_map
        assert 'OPTIONS' in method_map

        get_method = method_map['GET']
        get_resp = TestResponse()
        get_method({}, get_resp)

        assert get_resp.status == '200 OK'
        assert get_resp.json == {"method": "get"}

        assert uri == '/foo'
    def test_base_path_v3(self):
        spec = {
            "servers": [{
                "url": "http://localhost/v1"
            }],
            "paths": {
                "/foo": {
                    "get": {
                        "operationId": "controllers.foo.Foo.on_get"
                    }
                }
            },
        }

        router = OpenApiRouter(raw_json=json.dumps(spec))
        (resource, method_map, _, uri) = router.find("/v1/foo")

        assert "controllers.foo.Foo" in str(resource)
        assert len(method_map) >= 2
        assert "GET" in method_map
        assert "OPTIONS" in method_map

        get_method = method_map["GET"]
        get_resp = TestResponse()
        get_method({}, get_resp)

        assert get_resp.status == "200 OK"
        assert get_resp.json == {"method": "get"}

        assert uri == "/v1/foo"
Пример #5
0
    def test_base_path_v3(self):
        spec = {
            'servers': [{
                'url': 'http://localhost/v1'
            }],
            'paths': {
                '/foo': {
                    'get': {
                        'operationId': 'controllers.foo.Foo.on_get'
                    }
                }
            }
        }

        router = OpenApiRouter(raw_json=json.dumps(spec))
        (resource, method_map, _, uri) = router.find('/v1/foo')

        assert 'controllers.foo.Foo' in str(resource)
        assert len(method_map) >= 2
        assert 'GET' in method_map
        assert 'OPTIONS' in method_map

        get_method = method_map['GET']
        get_resp = TestResponse()
        get_method({}, get_resp)

        assert get_resp.status == '200 OK'
        assert get_resp.json == {"method": "get"}

        assert uri == '/v1/foo'
Пример #6
0
    def test_file_path(self):
        router = OpenApiRouter()
        (resource, method_map, _, uri) = router.find('/foo')

        assert 'controllers.foo.Foo' in str(resource)

        assert len(method_map) >= 4
        assert 'GET' in method_map
        assert 'POST' in method_map
        assert 'PUT' in method_map
        assert 'OPTIONS' in method_map

        get_method = method_map['GET']
        post_method = method_map['POST']
        put_method = method_map['PUT']

        get_resp = TestResponse()
        post_resp = TestResponse()
        put_resp = TestResponse()

        get_method({}, get_resp)
        post_method({}, post_resp)
        put_method({}, put_resp)

        assert get_resp.status == '200 OK'
        assert post_resp.status == '200 OK'
        assert put_resp.status == "418 I'm a teapot"

        assert get_resp.json == {"method": "get"}
        assert post_resp.json == {"method": "post"}
        assert put_resp.json == {"method": "put"}

        assert uri == '/foo'
Пример #7
0
    def test_base_path_v2(self):
        spec = {
            'basePath': '/v1',
            'paths': {
                '/foo': {
                    'get': {
                        'operationId': 'controllers.foo.Foo.on_get'
                    }
                }
            }
        }

        router = OpenApiRouter(raw_json=json.dumps(spec))
        (resource, method_map, _, uri) = router.find('/v1/foo')

        assert 'controllers.foo.Foo' in str(resource)
        assert len(method_map) == 1
        assert 'GET' in method_map

        get_method = method_map['GET']
        get_resp = TestResponse()
        get_method({}, get_resp)

        assert get_resp.status == '200 OK'
        assert get_resp.json == {"method": "get"}

        assert uri == '/v1/foo'
 def test_bad_file(self):
     with pytest.raises(FileNotFoundError):
         OpenApiRouter(file_path="kdjh.yaml")
 def test_missing_default(self):
     with pytest.raises(FileNotFoundError):
         OpenApiRouter()
Пример #10
0
from wsgiref import simple_server

import falcon

from falcon_openapi import OpenApiRouter

api = application = falcon.API(router=OpenApiRouter(
    file_path="openapi-spec.yaml"))

if __name__ == "__main__":
    httpd = simple_server.make_server("127.0.0.1", 8000, api)
    httpd.serve_forever()
Пример #11
0
import falcon

#Middlewares

from falcon_openapi import OpenApiRouter
from falcon_marshmallow import Marshmallow as MarshmallowMiddleware
from middleware.PrometheusMiddlewares import RequestsCounterMiddleware, RequestsTimerMiddleware
from middleware.SQLAlchemyMiddleware import SQLAlchemySessionManager
from middleware.RequestOwnerMiddleware import InjectOwnerIntoContextMiddleware

# #Models and Schemas
from models.ProductModel import Base
from utils.db import setup_db

#setup Db. Strip base from ProductModel.
Session = setup_db(Base)
sqlalchemy = SQLAlchemySessionManager(Session)

router = OpenApiRouter(file_path='specs/product.yml')
app = falcon.API(middleware=[
    InjectOwnerIntoContextMiddleware(),
    RequestsTimerMiddleware(),
    RequestsCounterMiddleware(),
    MarshmallowMiddleware(),
    sqlalchemy,
],
                 router=router)

if __name__ == '__main__':
    run_simple('0.0.0.0', 5000, app, use_reloader=True, use_debugger=True)