sanic/tests/test_dynamic_routes.py

45 lines
1.3 KiB
Python
Raw Normal View History

import pytest
from sanic.response import text
from sanic.router import RouteExists
2018-12-30 11:18:06 +00:00
@pytest.mark.parametrize(
"method,attr, expected",
[
("get", "text", "OK1 test"),
("post", "text", "OK2 test"),
("put", "text", "OK2 test"),
("delete", "status", 405),
],
)
2018-08-26 15:43:14 +01:00
def test_overload_dynamic_routes(app, method, attr, expected):
2018-12-30 11:18:06 +00:00
@app.route("/overload/<param>", methods=["GET"])
async def handler1(request, param):
2018-12-30 11:18:06 +00:00
return text("OK1 " + param)
2018-12-30 11:18:06 +00:00
@app.route("/overload/<param>", methods=["POST", "PUT"])
async def handler2(request, param):
2018-12-30 11:18:06 +00:00
return text("OK2 " + param)
2018-12-30 11:18:06 +00:00
request, response = getattr(app.test_client, method)("/overload/test")
assert getattr(response, attr) == expected
2018-08-26 15:43:14 +01:00
def test_overload_dynamic_routes_exist(app):
2018-12-30 11:18:06 +00:00
@app.route("/overload/<param>", methods=["GET"])
async def handler1(request, param):
2018-12-30 11:18:06 +00:00
return text("OK1 " + param)
2018-12-30 11:18:06 +00:00
@app.route("/overload/<param>", methods=["POST", "PUT"])
async def handler2(request, param):
2018-12-30 11:18:06 +00:00
return text("OK2 " + param)
# if this doesn't raise an error, than at least the below should happen:
# assert response.text == 'Duplicated'
with pytest.raises(RouteExists):
2018-12-30 11:18:06 +00:00
@app.route("/overload/<param>", methods=["PUT", "DELETE"])
async def handler3(request, param):
2018-12-30 11:18:06 +00:00
return text("Duplicated")