本文整理汇总了Python中sanic.utils.sanic_endpoint_test函数的典型用法代码示例。如果您正苦于以下问题:Python sanic_endpoint_test函数的具体用法?Python sanic_endpoint_test怎么用?Python sanic_endpoint_test使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sanic_endpoint_test函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_overload_routes
def test_overload_routes():
app = Sanic('test_dynamic_route')
@app.route('/overload', methods=['GET'])
async def handler1(request):
return text('OK1')
@app.route('/overload', methods=['POST', 'PUT'])
async def handler2(request):
return text('OK2')
request, response = sanic_endpoint_test(app, 'get', uri='/overload')
assert response.text == 'OK1'
request, response = sanic_endpoint_test(app, 'post', uri='/overload')
assert response.text == 'OK2'
request, response = sanic_endpoint_test(app, 'put', uri='/overload')
assert response.text == 'OK2'
request, response = sanic_endpoint_test(app, 'delete', uri='/overload')
assert response.status == 405
with pytest.raises(RouteExists):
@app.route('/overload', methods=['PUT', 'DELETE'])
async def handler3(request):
return text('Duplicated')
开发者ID:blurrcat,项目名称:sanic,代码行数:27,代码来源:test_routes.py
示例2: test_unmergeable_overload_routes
def test_unmergeable_overload_routes():
app = Sanic('test_dynamic_route')
@app.route('/overload_whole', methods=None)
async def handler1(request):
return text('OK1')
with pytest.raises(RouteExists):
@app.route('/overload_whole', methods=['POST', 'PUT'])
async def handler2(request):
return text('Duplicated')
request, response = sanic_endpoint_test(app, 'get', uri='/overload_whole')
assert response.text == 'OK1'
request, response = sanic_endpoint_test(app, 'post', uri='/overload_whole')
assert response.text == 'OK1'
@app.route('/overload_part', methods=['GET'])
async def handler1(request):
return text('OK1')
with pytest.raises(RouteExists):
@app.route('/overload_part')
async def handler2(request):
return text('Duplicated')
request, response = sanic_endpoint_test(app, 'get', uri='/overload_part')
assert response.text == 'OK1'
request, response = sanic_endpoint_test(app, 'post', uri='/overload_part')
assert response.status == 405
开发者ID:blurrcat,项目名称:sanic,代码行数:33,代码来源:test_routes.py
示例3: test_remove_static_route
def test_remove_static_route():
app = Sanic('test_remove_static_route')
async def handler1(request):
return text('OK1')
async def handler2(request):
return text('OK2')
app.add_route(handler1, '/test')
app.add_route(handler2, '/test2')
request, response = sanic_endpoint_test(app, uri='/test')
assert response.status == 200
request, response = sanic_endpoint_test(app, uri='/test2')
assert response.status == 200
app.remove_route('/test')
app.remove_route('/test2')
request, response = sanic_endpoint_test(app, uri='/test')
assert response.status == 404
request, response = sanic_endpoint_test(app, uri='/test2')
assert response.status == 404
开发者ID:blurrcat,项目名称:sanic,代码行数:26,代码来源:test_routes.py
示例4: test_remove_route_without_clean_cache
def test_remove_route_without_clean_cache():
app = Sanic('test_remove_static_route')
async def handler(request):
return text('OK')
app.add_route(handler, '/test')
request, response = sanic_endpoint_test(app, uri='/test')
assert response.status == 200
app.remove_route('/test', clean_cache=True)
request, response = sanic_endpoint_test(app, uri='/test')
assert response.status == 404
app.add_route(handler, '/test')
request, response = sanic_endpoint_test(app, uri='/test')
assert response.status == 200
app.remove_route('/test', clean_cache=False)
request, response = sanic_endpoint_test(app, uri='/test')
assert response.status == 200
开发者ID:blurrcat,项目名称:sanic,代码行数:25,代码来源:test_routes.py
示例5: test_bp_exception_handler
def test_bp_exception_handler():
app = Sanic('test_middleware')
blueprint = Blueprint('test_middleware')
@blueprint.route('/1')
def handler_1(request):
raise InvalidUsage("OK")
@blueprint.route('/2')
def handler_2(request):
raise ServerError("OK")
@blueprint.route('/3')
def handler_3(request):
raise NotFound("OK")
@blueprint.exception(NotFound, ServerError)
def handler_exception(request, exception):
return text("OK")
app.blueprint(blueprint)
request, response = sanic_endpoint_test(app, uri='/1')
assert response.status == 400
request, response = sanic_endpoint_test(app, uri='/2')
assert response.status == 200
assert response.text == 'OK'
request, response = sanic_endpoint_test(app, uri='/3')
assert response.status == 200
开发者ID:blurrcat,项目名称:sanic,代码行数:32,代码来源:test_blueprints.py
示例6: test_shorthand_routes_options
def test_shorthand_routes_options():
app = Sanic('test_shorhand_routes_options')
@app.options('/options')
def handler(request):
return text('OK')
request, response = sanic_endpoint_test(app, uri='/options', method='options')
assert response.status == 200
request, response = sanic_endpoint_test(app, uri='/options', method='get')
assert response.status == 405
开发者ID:blurrcat,项目名称:sanic,代码行数:12,代码来源:test_routes.py
示例7: test_shorthand_routes_patch
def test_shorthand_routes_patch():
app = Sanic('test_shorhand_routes_patch')
@app.patch('/patch')
def handler(request):
return text('OK')
request, response = sanic_endpoint_test(app, uri='/patch', method='patch')
assert response.text == 'OK'
request, response = sanic_endpoint_test(app, uri='/patch', method='get')
assert response.status == 405
开发者ID:blurrcat,项目名称:sanic,代码行数:12,代码来源:test_routes.py
示例8: test_method_not_allowed
def test_method_not_allowed():
app = Sanic('test_method_not_allowed')
@app.route('/test', methods=['GET'])
async def handler(request):
return text('OK')
request, response = sanic_endpoint_test(app, uri='/test')
assert response.status == 200
request, response = sanic_endpoint_test(app, method='post', uri='/test')
assert response.status == 405
开发者ID:blurrcat,项目名称:sanic,代码行数:12,代码来源:test_routes.py
示例9: test_unexisting_methods
def test_unexisting_methods():
app = Sanic('test_unexisting_methods')
class DummyView(HTTPMethodView):
def get(self, request):
return text('I am get method')
app.add_route(DummyView.as_view(), '/')
request, response = sanic_endpoint_test(app, method="get")
assert response.text == 'I am get method'
request, response = sanic_endpoint_test(app, method="post")
assert response.text == 'Error: Method POST not allowed for URL /'
开发者ID:blurrcat,项目名称:sanic,代码行数:13,代码来源:test_views.py
示例10: test_remove_dynamic_route
def test_remove_dynamic_route():
app = Sanic('test_remove_dynamic_route')
async def handler(request, name):
return text('OK')
app.add_route(handler, '/folder/<name>')
request, response = sanic_endpoint_test(app, uri='/folder/test123')
assert response.status == 200
app.remove_route('/folder/<name>')
request, response = sanic_endpoint_test(app, uri='/folder/test123')
assert response.status == 404
开发者ID:blurrcat,项目名称:sanic,代码行数:14,代码来源:test_routes.py
示例11: test_vhosts_with_list
def test_vhosts_with_list():
app = Sanic('test_vhosts')
@app.route('/', host=["hello.com", "world.com"])
async def handler(request):
return text("Hello, world!")
headers = {"Host": "hello.com"}
request, response = sanic_endpoint_test(app, headers=headers)
assert response.text == "Hello, world!"
headers = {"Host": "world.com"}
request, response = sanic_endpoint_test(app, headers=headers)
assert response.text == "Hello, world!"
开发者ID:blurrcat,项目名称:sanic,代码行数:14,代码来源:test_vhosts.py
示例12: test_static_routes
def test_static_routes():
app = Sanic('test_dynamic_route')
@app.route('/test')
async def handler1(request):
return text('OK1')
@app.route('/pizazz')
async def handler2(request):
return text('OK2')
request, response = sanic_endpoint_test(app, uri='/test')
assert response.text == 'OK1'
request, response = sanic_endpoint_test(app, uri='/pizazz')
assert response.text == 'OK2'
开发者ID:blurrcat,项目名称:sanic,代码行数:16,代码来源:test_routes.py
示例13: test_methods
def test_methods(method):
app = Sanic('test_methods')
class DummyView(HTTPMethodView):
def get(self, request):
return text('', headers={'method': 'GET'})
def post(self, request):
return text('', headers={'method': 'POST'})
def put(self, request):
return text('', headers={'method': 'PUT'})
def head(self, request):
return text('', headers={'method': 'HEAD'})
def options(self, request):
return text('', headers={'method': 'OPTIONS'})
def patch(self, request):
return text('', headers={'method': 'PATCH'})
def delete(self, request):
return text('', headers={'method': 'DELETE'})
app.add_route(DummyView.as_view(), '/')
request, response = sanic_endpoint_test(app, method=method)
assert response.headers['method'] == method
开发者ID:blurrcat,项目名称:sanic,代码行数:30,代码来源:test_views.py
示例14: test_with_middleware_response
def test_with_middleware_response():
app = Sanic('test_with_middleware_response')
results = []
@app.middleware('request')
async def process_response(request):
results.append(request)
@app.middleware('response')
async def process_response(request, response):
results.append(request)
results.append(response)
class DummyView(HTTPMethodView):
def get(self, request):
return text('I am get method')
app.add_route(DummyView.as_view(), '/')
request, response = sanic_endpoint_test(app)
assert response.text == 'I am get method'
assert type(results[0]) is Request
assert type(results[1]) is Request
assert isinstance(results[2], HTTPResponse)
开发者ID:blurrcat,项目名称:sanic,代码行数:27,代码来源:test_views.py
示例15: test_bp_listeners
def test_bp_listeners():
app = Sanic('test_middleware')
blueprint = Blueprint('test_middleware')
order = []
@blueprint.listener('before_server_start')
def handler_1(sanic, loop):
order.append(1)
@blueprint.listener('after_server_start')
def handler_2(sanic, loop):
order.append(2)
@blueprint.listener('after_server_start')
def handler_3(sanic, loop):
order.append(3)
@blueprint.listener('before_server_stop')
def handler_4(sanic, loop):
order.append(5)
@blueprint.listener('before_server_stop')
def handler_5(sanic, loop):
order.append(4)
@blueprint.listener('after_server_stop')
def handler_6(sanic, loop):
order.append(6)
app.blueprint(blueprint)
request, response = sanic_endpoint_test(app, uri='/')
assert order == [1,2,3,4,5,6]
开发者ID:blurrcat,项目名称:sanic,代码行数:35,代码来源:test_blueprints.py
示例16: test_payload_too_large_at_on_header_default
def test_payload_too_large_at_on_header_default():
data = 'a' * 1000
response = sanic_endpoint_test(
on_header_default_app, method='post', uri='/1',
gather_request=False, data=data)
assert response.status == 413
assert response.text == 'Error: Payload Too Large'
开发者ID:blurrcat,项目名称:sanic,代码行数:7,代码来源:test_payload_too_large.py
示例17: test_dynamic_route_int
def test_dynamic_route_int():
app = Sanic('test_dynamic_route_int')
results = []
@app.route('/folder/<folder_id:int>')
async def handler(request, folder_id):
results.append(folder_id)
return text('OK')
request, response = sanic_endpoint_test(app, uri='/folder/12345')
assert response.text == 'OK'
assert type(results[0]) is int
request, response = sanic_endpoint_test(app, uri='/folder/asdf')
assert response.status == 404
开发者ID:blurrcat,项目名称:sanic,代码行数:16,代码来源:test_routes.py
示例18: test_chained_redirect
def test_chained_redirect(redirect_app):
"""Test sanic_endpoint_test is working for redirection"""
request, response = sanic_endpoint_test(redirect_app, uri='/1')
assert request.url.endswith('/1')
assert response.status == 200
assert response.text == 'OK'
assert response.url.endswith('/3')
开发者ID:blurrcat,项目名称:sanic,代码行数:7,代码来源:test_redirect.py
示例19: test_static_add_route
def test_static_add_route():
app = Sanic('test_static_add_route')
async def handler1(request):
return text('OK1')
async def handler2(request):
return text('OK2')
app.add_route(handler1, '/test')
app.add_route(handler2, '/test2')
request, response = sanic_endpoint_test(app, uri='/test')
assert response.text == 'OK1'
request, response = sanic_endpoint_test(app, uri='/test2')
assert response.text == 'OK2'
开发者ID:blurrcat,项目名称:sanic,代码行数:17,代码来源:test_routes.py
示例20: test_exception_in_exception_handler_debug_off
def test_exception_in_exception_handler_debug_off(exception_app):
"""Test that an exception thrown in an error handler is handled"""
request, response = sanic_endpoint_test(
exception_app,
uri='/error_in_error_handler_handler',
debug=False)
assert response.status == 500
assert response.body == b'An error occurred while handling an error'
开发者ID:blurrcat,项目名称:sanic,代码行数:8,代码来源:test_exceptions.py
注:本文中的sanic.utils.sanic_endpoint_test函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论