本文整理汇总了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'"
)
示例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')
示例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)
示例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')
示例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())
示例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'), '{}')
示例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"}')
示例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)
示例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"}')
示例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\'"}')
示例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'), '{}')
示例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(), '')
示例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"}')
示例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\'"}')
示例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\'"}')