0a5fa72099
This is by no means the final solution but it's a start in the right direction. Eventually what needs to happen is we need to reduce the complexity of the routing. CompsitionView can probably be removed later on in favor of better Route objects. Also in the next version of sanic we need to move merge_route and add_parameter out of the add_route logic and just have them as standalone methods. The tests should cover everything that we need so that if any changes are made we can identify regression.
47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
from sanic import Sanic
|
|
from sanic.response import text
|
|
from sanic.utils import sanic_endpoint_test
|
|
from sanic.router import RouteExists
|
|
import pytest
|
|
|
|
|
|
@pytest.mark.parametrize("method,attr, expected", [
|
|
("get", "text", "OK1 test"),
|
|
("post", "text", "OK2 test"),
|
|
("put", "text", "OK2 test"),
|
|
("delete", "status", 405),
|
|
])
|
|
def test_overload_dynamic_routes(method, attr, expected):
|
|
app = Sanic('test_dynamic_route')
|
|
|
|
@app.route('/overload/<param>', methods=['GET'])
|
|
async def handler1(request, param):
|
|
return text('OK1 ' + param)
|
|
|
|
@app.route('/overload/<param>', methods=['POST', 'PUT'])
|
|
async def handler2(request, param):
|
|
return text('OK2 ' + param)
|
|
|
|
request, response = sanic_endpoint_test(
|
|
app, method, uri='/overload/test')
|
|
assert getattr(response, attr) == expected
|
|
|
|
|
|
def test_overload_dynamic_routes_exist():
|
|
app = Sanic('test_dynamic_route')
|
|
|
|
@app.route('/overload/<param>', methods=['GET'])
|
|
async def handler1(request, param):
|
|
return text('OK1 ' + param)
|
|
|
|
@app.route('/overload/<param>', methods=['POST', 'PUT'])
|
|
async def handler2(request, param):
|
|
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):
|
|
@app.route('/overload/<param>', methods=['PUT', 'DELETE'])
|
|
async def handler3(request):
|
|
return text('Duplicated')
|