当前位置: 首页>>代码示例>>Python>>正文


Python chisel.Application类代码示例

本文整理汇总了Python中chisel.Application的典型用法代码示例。如果您正苦于以下问题:Python Application类的具体用法?Python Application怎么用?Python Application使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Application类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_url_arg

    def test_url_arg(self): # pylint: disable=invalid-name

        @action(method='GET', urls='/my_action/{a}', spec='''\
action my_action
  input
    int a
    int b
  output
    int sum
''')
        def my_action(unused_app, req):
            self.assertEqual(req['a'], 5)
            return {'sum': req['a'] + req['b']}

        app = Application()
        app.add_request(my_action)

        environ = {'wsgi.errors': StringIO()}
        status, headers, response = app.request('GET', '/my_action/5', query_string='b=7', environ=environ)
        self.assertEqual(status, '200 OK')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"sum":12}')
        self.assertEqual(environ['wsgi.errors'].getvalue(), '')

        environ = {'wsgi.errors': StringIO()}
        status, headers, response = app.request('GET', '/my_action/5', query_string='a=3&b=7', environ=environ)
        self.assertEqual(status, '400 Bad Request')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"InvalidInput","message":"Duplicate URL argument member \'a\'"}')
        self.assertRegex(
            environ['wsgi.errors'].getvalue(),
            r"WARNING \[\d+ / \d+\] Duplicate URL argument member 'a' for action 'my_action'"
        )
开发者ID:craigahobbs,项目名称:chisel,代码行数:33,代码来源:test_action.py

示例2: test_log_format_callable

    def test_log_format_callable(self):

        def my_wsgi(environ, start_response):
            ctx = environ[Environ.CTX]
            ctx.log.warning('Hello log')
            start_response(HTTPStatus.OK, [('Content-Type', 'text/plain')])
            return ['Hello'.encode('utf-8')]

        class MyFormatter:

            def __init__(self, ctx):
                assert ctx is not None

            @staticmethod
            def format(record):
                return record.getMessage()

            @staticmethod
            def formatTime(record, unused_datefmt=None): # pylint: disable=invalid-name
                return record.getMessage()

            @staticmethod
            def formatException(unused_exc_info): # pylint: disable=invalid-name
                return 'Bad'

        app = Application()
        app.add_request(Request(my_wsgi))
        app.log_format = MyFormatter

        environ = {'wsgi.errors': StringIO()}
        status, headers, response = app.request('GET', '/my_wsgi', environ=environ)
        self.assertEqual(response, 'Hello'.encode('utf-8'))
        self.assertEqual(status, '200 OK')
        self.assertTrue(('Content-Type', 'text/plain') in headers)
        self.assertEqual(environ['wsgi.errors'].getvalue(), 'Hello log\n')
开发者ID:craigahobbs,项目名称:chisel,代码行数:35,代码来源:test_app.py

示例3: __init__

    def __init__(self, index):
        Application.__init__(self)
        self.log_level = logging.INFO
        self.index = index

        # Add requests
        self.add_request(DocAction())
        self.add_request(pypi_index)
        self.add_request(pypi_download)
        self.add_request(pypi_upload)
开发者ID:craigahobbs,项目名称:mrpypi,代码行数:10,代码来源:app.py

示例4: test_request_exception

    def test_request_exception(self):

        def request1(unused_environ, unused_start_response):
            raise Exception('')

        app = Application()
        app.add_request(Request(request1))

        status, headers, response = app.request('GET', '/request1')
        self.assertEqual(status, '500 Internal Server Error')
        self.assertTrue(('Content-Type', 'text/plain') in headers)
        self.assertEqual(response, b'Internal Server Error')
开发者ID:craigahobbs,项目名称:chisel,代码行数:12,代码来源:test_app.py

示例5: test_request_string_response

    def test_request_string_response(self):

        def string_response(environ, unused_start_response):
            ctx = environ[Environ.CTX]
            return ctx.response(HTTPStatus.OK, 'text/plain', 'Hello World')

        app = Application()
        app.add_request(Request(string_response))

        environ = {'wsgi.errors': StringIO()}
        status, headers, response = app.request('GET', '/string_response', environ=environ)
        self.assertEqual(status, '500 Internal Server Error')
        self.assertListEqual(headers, [('Content-Type', 'text/plain')])
        self.assertEqual(response, b'Internal Server Error')
        self.assertIn('response content cannot be of type str or bytes', environ['wsgi.errors'].getvalue())
开发者ID:craigahobbs,项目名称:chisel,代码行数:15,代码来源:test_app.py

示例6: test_error_none_output

    def test_error_none_output(self):

        @action(spec='''\
action my_action
''')
        def my_action(unused_app, unused_req):
            pass

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '200 OK')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{}')
开发者ID:craigahobbs,项目名称:chisel,代码行数:15,代码来源:test_action.py

示例7: test_error_unexpected_custom

    def test_error_unexpected_custom(self):

        @action(wsgi_response=True, spec='''\
action my_action
''')
        def my_action(unused_app, unused_req):
            raise Exception('FAIL')

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '500 Internal Server Error')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"UnexpectedError"}')
开发者ID:craigahobbs,项目名称:chisel,代码行数:15,代码来源:test_action.py

示例8: test_decorator_other

    def test_decorator_other(self):

        # Action decorator with urls, custom response callback, and validate response bool
        @action(urls=('/foo',), wsgi_response=True, spec='''\
action my_action_default
''')
        def my_action_default(ctx, unused_req):
            return ctx.response_text(HTTPStatus.OK)

        app = Application()
        app.add_request(my_action_default)
        self.assertEqual(my_action_default.name, 'my_action_default')
        self.assertEqual(my_action_default.urls, (('POST', '/foo'),))
        self.assertTrue(isinstance(my_action_default.model, ActionModel))
        self.assertEqual(my_action_default.model.name, 'my_action_default')
        self.assertEqual(my_action_default.wsgi_response, True)
开发者ID:craigahobbs,项目名称:chisel,代码行数:16,代码来源:test_action.py

示例9: test_error_raised

    def test_error_raised(self):

        @action(spec='''\
action my_action
  errors
    MyError
''')
        def my_action(unused_app, unused_req):
            raise ActionError('MyError')

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '400 Bad Request')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"MyError"}')
开发者ID:craigahobbs,项目名称:chisel,代码行数:17,代码来源:test_action.py

示例10: test_error_invalid_query_string

    def test_error_invalid_query_string(self):

        @action(method='GET', spec='''\
action my_action
  input
    int a
''')
        def my_action(unused_app, unused_req):
            return {}

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('GET', '/my_action', query_string='a')
        self.assertEqual(status, '400 Bad Request')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"InvalidInput","message":"Invalid key/value pair \'a\'"}')
开发者ID:craigahobbs,项目名称:chisel,代码行数:17,代码来源:test_action.py

示例11: test_headers

    def test_headers(self):

        @action(spec='''\
action my_action
''')
        def my_action(ctx, unused_req):
            ctx.add_header('MyHeader', 'MyInitialValue')
            ctx.add_header('MyHeader', 'MyValue')
            return {}

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action')
        self.assertEqual(status, '200 OK')
        self.assertEqual(headers, [('Content-Type', 'application/json'), ('MyHeader', 'MyValue')])
        self.assertEqual(response.decode('utf-8'), '{}')
开发者ID:craigahobbs,项目名称:chisel,代码行数:17,代码来源:test_action.py

示例12: test_error_raise_builtin

    def test_error_raise_builtin(self):

        @action(spec='''\
action my_action
''')
        def my_action(unused_app, unused_req):
            raise ActionError('UnexpectedError', status=HTTPStatus.INTERNAL_SERVER_ERROR)

        app = Application()
        app.add_request(my_action)

        environ = {'wsgi.errors': StringIO()}
        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}', environ=environ)
        self.assertEqual(status, '500 Internal Server Error')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"UnexpectedError"}')
        self.assertEqual(environ['wsgi.errors'].getvalue(), '')
开发者ID:craigahobbs,项目名称:chisel,代码行数:17,代码来源:test_action.py

示例13: test_error_raised_status

    def test_error_raised_status(self):

        @action(spec='''\
action my_action
  errors
    MyError
''')
        def my_action(unused_app, unused_req):
            raise ActionError('MyError', message='My message', status=HTTPStatus.NOT_FOUND)

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '404 Not Found')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"MyError","message":"My message"}')
开发者ID:craigahobbs,项目名称:chisel,代码行数:17,代码来源:test_action.py

示例14: test_error_array_output

    def test_error_array_output(self):

        @action(spec='''\
action my_action
''')
        def my_action(unused_app, unused_req):
            return []

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '500 Internal Server Error')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'),
                         '{"error":"InvalidOutput","message":"Invalid value [] (type \'list\'), '
                         'expected type \'my_action_output\'"}')
开发者ID:craigahobbs,项目名称:chisel,代码行数:17,代码来源:test_action.py

示例15: test_error_response

    def test_error_response(self):

        @action(spec='''\
action my_action
  errors
    MyError
''')
        def my_action(unused_app, unused_req):
            return {'error': 'MyError'}

        app = Application()
        app.add_request(my_action)

        status, headers, response = app.request('POST', '/my_action', wsgi_input=b'{}')
        self.assertEqual(status, '500 Internal Server Error')
        self.assertEqual(sorted(headers), [('Content-Type', 'application/json')])
        self.assertEqual(response.decode('utf-8'), '{"error":"InvalidOutput","message":"Unknown member \'error\'"}')
开发者ID:craigahobbs,项目名称:chisel,代码行数:17,代码来源:test_action.py


注:本文中的chisel.Application类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。