Esempio n. 1
0
def make_application(**settings) -> app.Application:
    """Create the web application instance after building the settings

    :param dict settings: kwarg settings passed in for the application

    """
    _set_default_settings(settings)
    if settings['environment'] in ('development', 'testing'):
        os.environ.pop('SENTRY_DSN', None)
        settings.setdefault('debug', True)

    urls = [
        web.url(r'/static/(.*)', web.StaticFileHandler,
                {'path': settings['static_path']})
    ]
    urls += endpoints.URLS

    settings['permissions'] = _get_permissions(urls)

    application = Application(
        urls,
        log_function=correlation.mixins.correlation_id_logger,
        **settings)

    # Content handling setup
    content.install(application, 'application/json', 'utf-8')
    content.add_transcoder(application, transcoders.JSONTranscoder())
    content.add_transcoder(
        application, transcoders.JSONTranscoder('application/json-patch+json'))
    content.add_transcoder(application, transcoders.MsgPackTranscoder())
    content.add_transcoder(application, html.HTMLTranscoder())

    # Instrument which libraries to include in sentry reports
    """
    sentry.install(
        app,
        include_paths=[
            'aiopg',
            'aioredis',
            'arrow',
            'imbi',
            'jsonpatch',
            'jsonschema',
            'sprockets.http',
            'sprockets.handlers.status',
            'sprockets.mixins.correlation',
            'sprockets.mixins.mediatype',
            'sprockets.mixins.metrics',
            'sprockets_postgres'],
        release=__version__,
        tags={'environment': settings['environment']})
    """
    return application
 def test_that_add_text_content_type_discards_charset_parameter(self):
     settings = content.install(self.context, 'application/json', 'utf-8')
     content.add_text_content_type(self.context,
                                   'application/json;charset=UTF-8', 'utf8',
                                   json.dumps, json.loads)
     transcoder = settings['application/json']
     self.assertIsInstance(transcoder, handlers.TextContentHandler)
 def test_that_add_text_content_type_creates_text_handler(self):
     settings = content.install(self.context, 'application/json')
     content.add_text_content_type(self.context, 'application/json', 'utf8',
                                   json.dumps, json.loads)
     transcoder = settings['application/json']
     self.assertIsInstance(transcoder, handlers.TextContentHandler)
     self.assertIs(transcoder._dumps, json.dumps)
     self.assertIs(transcoder._loads, json.loads)
 def test_that_add_binary_content_type_creates_binary_handler(self):
     settings = content.install(self.context, 'application/octet-stream')
     content.add_binary_content_type(self.context,
                                     'application/vnd.python.pickle',
                                     pickle.dumps, pickle.loads)
     transcoder = settings['application/vnd.python.pickle']
     self.assertIsInstance(transcoder, handlers.BinaryContentHandler)
     self.assertIs(transcoder._pack, pickle.dumps)
     self.assertIs(transcoder._unpack, pickle.loads)
 def test_that_get_settings_returns_installed_settings(self):
     settings = content.install(self.context, 'application/xml', 'utf8')
     other_settings = content.get_settings(self.context)
     self.assertIs(settings, other_settings)
 def test_that_install_creates_settings(self):
     settings = content.install(self.context, 'application/json', 'utf8')
     self.assertIsNotNone(settings)
     self.assertEqual(settings.default_content_type, 'application/json')
     self.assertEqual(settings.default_encoding, 'utf8')