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


Python Application.add_request方法代码示例

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


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

示例1: test_url_arg

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    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,代码行数:35,代码来源:test_action.py

示例2: test_log_format_callable

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    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,代码行数:37,代码来源:test_app.py

示例3: test_error_bad_error

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    def test_error_bad_error(self):

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

        app = Application()
        app.add_request(my_action)

        environ = {'wsgi.errors': StringIO()}
        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","member":"error","message":"Invalid value \'MyBadError\' (type \'str\') '
                         'for member \'error\', expected type \'my_action_error\'"}')
        self.assertEqual(environ['wsgi.errors'].getvalue(), '')

        app.validate_output = False
        environ = {'wsgi.errors': StringIO()}
        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":"MyBadError"}')
        self.assertEqual(environ['wsgi.errors'].getvalue(), '')
开发者ID:craigahobbs,项目名称:chisel,代码行数:31,代码来源:test_action.py

示例4: test_request

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    def test_request(self):

        def request1(environ, unused_start_response):
            ctx = environ[Environ.CTX]
            return ctx.response_text(HTTPStatus.OK, 'request1')

        def request2(environ, unused_start_response):
            ctx = environ[Environ.CTX]
            return ctx.response_text(HTTPStatus.OK, 'request2 ' + ctx.url_args['arg'] + ' ' + ctx.url_args.get('arg2', '?'))

        app = Application()
        app.add_request(Request(request1, urls=(
            ('GET', '/request1a'),
            (None, '/request1b')
        )))
        app.add_request(Request(request2, urls=(
            ('GET', '/request2a/{arg}'),
            (None, '/request2b/{arg}/bar/{arg2}/bonk')
        )))

        # Exact method and exact URL
        status, _, response = app.request('GET', '/request1a')
        self.assertEqual(status, '200 OK')
        self.assertEqual(response, b'request1')

        # Wrong method and exact URL
        status, _, response = app.request('POST', '/request1a')
        self.assertEqual(status, '405 Method Not Allowed')
        self.assertEqual(response, b'Method Not Allowed')

        # Any method and exact URL
        status, _, response = app.request('GET', '/request1b')
        self.assertEqual(status, '200 OK')
        self.assertEqual(response, b'request1')

        # Exact method and regex URL
        status, _, response = app.request('GET', '/request2a/foo')
        self.assertEqual(status, '200 OK')
        self.assertEqual(response, b'request2 foo ?')

        # Wrong method and regex URL
        status, _, response = app.request('POST', '/request2a/foo')
        self.assertEqual(status, '405 Method Not Allowed')
        self.assertEqual(response, b'Method Not Allowed')

        # Any method and regex URL
        status, _, response = app.request('POST', '/request2b/foo/bar/blue/bonk')
        self.assertEqual(status, '200 OK')
        self.assertEqual(response, b'request2 foo blue')

        # URL not found
        status, _, response = app.request('GET', '/request3')
        self.assertEqual(status, '404 Not Found')
        self.assertEqual(response, b'Not Found')
开发者ID:craigahobbs,项目名称:chisel,代码行数:56,代码来源:test_app.py

示例5: test_request_exception

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    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,代码行数:14,代码来源:test_app.py

示例6: test_request_not_found

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    def test_request_not_found(self):

        @request(doc='''
This is the request documentation.
''')
        def my_request(unused_environ, unused_start_response):
            pass
        application = Application()
        application.add_request(DocAction())
        application.add_request(my_request)

        status, unused_headers, response = application.request('GET', '/doc', query_string='name=my_unknown_request')
        self.assertEqual(status, '404 Not Found')
        self.assertEqual(response, b'Unknown Request')
开发者ID:craigahobbs,项目名称:chisel,代码行数:16,代码来源:test_doc.py

示例7: test_error_none_output

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    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,代码行数:17,代码来源:test_action.py

示例8: test_request_string_response

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    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,代码行数:17,代码来源:test_app.py

示例9: test_error_unexpected_custom

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    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,代码行数:17,代码来源:test_action.py

示例10: test_decorator_other

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    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,代码行数:18,代码来源:test_action.py

示例11: test_error_raised_status

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    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,代码行数:19,代码来源:test_action.py

示例12: test_error_array_output

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    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,代码行数:19,代码来源:test_action.py

示例13: test_headers

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    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,代码行数:19,代码来源:test_action.py

示例14: test_error_invalid_query_string

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    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,代码行数:19,代码来源:test_action.py

示例15: test_error_response

# 需要导入模块: from chisel import Application [as 别名]
# 或者: from chisel.Application import add_request [as 别名]
    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,代码行数:19,代码来源:test_action.py


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