How to use cornice - 10 common examples

To help you get started, we’ve selected a few cornice examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Cornices / cornice / tests / test_pyramidhook.py View on Github external
def test(self):
        # Compiled regexs are, apparently, non-pickleable
        service = Service(name="test", path="/", schema={'a': re.compile('')})
        service.add_view('GET', lambda _:_)
        register_service_views(self.config, service)
github Cornices / cornice / tests / test_pyramidhook.py View on Github external
def setUp(self):
        self.config = testing.setUp()
        self.config.include("cornice")
        self.config.add_route('proute', '/from_pyramid')
        self.config.scan("tests.test_pyramidhook")

        def handle_response(request):
            return {'service': request.current_service.name,
                    'route': request.matched_route.name}
        rserv = Service(name="ServiceWPyramidRoute", pyramid_route="proute")
        rserv.add_view('GET', handle_response)

        register_service_views(self.config, rserv)
        self.app = TestApp(CatchErrors(self.config.make_wsgi_app()))
github Cornices / cornice / tests / test_pyramidhook.py View on Github external
def test_no_route_or_path(self):
        with self.assertRaises(TypeError):
            Service(name="broken service",)
github Cornices / cornice / tests / validationapp.py View on Github external
Email,
        drop,
        null,
        deferred
    )

    from cornice.validators import colander_validator, colander_body_validator

    COLANDER = True
except ImportError:
    COLANDER = False

if COLANDER:

    # services for colander validation
    signup = Service(name="signup", path="/signup")
    bound = Service(name="bound", path="/bound")
    group_signup = Service(name="group signup", path="/group_signup")
    foobar = Service(name="foobar", path="/foobar")
    foobaz = Service(name="foobaz", path="/foobaz")
    email_service = Service(name='newsletter', path='/newsletter')
    item_service = Service(name='item', path='/item/{item_id}')
    form_service = Service(name="form", path="/form")


    class SignupSchema(MappingSchema):
        username = SchemaNode(String())

    @deferred
    def deferred_missing(node, kw):
        import random
        return kw.get('missing_foo') or random.random()
github Cornices / cornice / tests / validationapp.py View on Github external
except ImportError:
        EXCLUDE = 'exclude'
    from cornice.validators import (
        marshmallow_validator,
        marshmallow_body_validator
    )

    MARSHMALLOW = True
except ImportError:
    MARSHMALLOW = False

if MARSHMALLOW:
    # services for marshmallow validation

    m_signup = Service(name="m_signup", path="/m_signup")
    m_bound = Service(name="m_bound", path="/m_bound")
    m_group_signup = Service(name="m_group signup", path="/m_group_signup")
    m_foobar = Service(name="m_foobar", path="/m_foobar")
    m_foobaz = Service(name="m_foobaz", path="/m_foobaz")
    m_email_service = Service(name='m_newsletter', path='/m_newsletter')
    m_item_service = Service(name='m_item', path='/m_item/{item_id}')
    m_form_service = Service(name="m_form", path="/m_form")


    class MSignupSchema(marshmallow.Schema):
        class Meta:
            strict = True
            unknown = EXCLUDE
        username = marshmallow.fields.String()

    import random
github Cornices / cornice / tests / test_resource_callable.py View on Github external
    @view(renderer='json', accept=_accept)
    def get(self):
        return FRUITS.get(int(self.request.matchdict['id']))
github Cornices / cornice / tests / test_resource.py View on Github external
    @view(renderer='json', accept='text/json')
    def collection_post(self):
        return {'test': 'yeah'}
github Cornices / cornice / tests / test_service.py View on Github external
def test_service_instanciation(self):
        service = Service("coconuts", "/migrate")
        self.assertEqual(service.name, "coconuts")
        self.assertEqual(service.path, "/migrate")
        self.assertEqual(service.renderer, Service.renderer)

        service = Service("coconuts", "/migrate", renderer="html")
        self.assertEqual(service.renderer, "html")

        # test that lists are also set
        validators = [lambda x: True, ]
        service = Service("coconuts", "/migrate", validators=validators)
        self.assertEqual(service.validators, validators)
github Cornices / cornice.ext.swagger / tests / test_swagger.py View on Github external
def test_callable_default_security(self):

        def get_security(service, method):
            definitions = service.definitions
            for definition in definitions:
                met, view, args = definition
                if met == method:
                    break

            if 'security' in args:
                return [{'basicAuth': []}]
            else:
                return []

        service = Service("IceCream", "/icecream/{flavour}")

        @service.get()
        def view_get(self, request):
            return service

        @service.post(security='foo')
        def view_post(self, request):
            return service

        swagger = CorniceSwagger([service])
        swagger.swagger = {'securityDefinitions': {'basicAuth': {'type': 'basic'}}}
        swagger.default_security = get_security
        spec = swagger.generate()
        validate(spec)
        security = spec['paths']['/icecream/{flavour}']['post']['security']
        self.assertEquals(security, [{'basicAuth': []}])
github Cornices / cornice / tests / test_service.py View on Github external
def test_get_contenttypes(self):
        # defining a service with different "content_type" headers, we should
        # be able to retrieve this information easily
        service = Service("color", "/favorite-color")
        service.add_view("GET", lambda x: "blue", content_type="text/plain")
        self.assertEquals(service.get_contenttypes("GET"), ['text/plain'])

        service.add_view("GET", lambda x: "blue",
                         content_type="application/json")
        self.assertEquals(service.get_contenttypes("GET"),
                          ['text/plain', 'application/json'])

        # adding a view for the POST method should not break everything :-)
        service.add_view("POST", lambda x: "ok", content_type=('foo/bar'))
        self.assertEquals(service.get_contenttypes("GET"),
                          ['text/plain', 'application/json'])
        # and of course the list of supported ingress content-types should be
        # available for the "POST" as well.
        self.assertEquals(service.get_contenttypes("POST"),
                          ['foo/bar'])