Merge pull request #360 from seemethere/fix_route_overloading_for_dynamic_routes
Fixes route overloading for dynamic routes
This commit is contained in:
46
tests/test_dynamic_routes.py
Normal file
46
tests/test_dynamic_routes.py
Normal file
@@ -0,0 +1,46 @@
|
||||
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')
|
||||
@@ -1,43 +1,45 @@
|
||||
import pytest as pytest
|
||||
|
||||
from sanic import Sanic
|
||||
from sanic.response import text, HTTPResponse
|
||||
from sanic.views import HTTPMethodView
|
||||
from sanic.blueprints import Blueprint
|
||||
from sanic.request import Request
|
||||
from sanic.utils import sanic_endpoint_test
|
||||
from sanic.constants import HTTP_METHODS
|
||||
|
||||
|
||||
def test_methods():
|
||||
@pytest.mark.parametrize('method', HTTP_METHODS)
|
||||
def test_methods(method):
|
||||
app = Sanic('test_methods')
|
||||
|
||||
class DummyView(HTTPMethodView):
|
||||
|
||||
def get(self, request):
|
||||
return text('I am get method')
|
||||
return text('', headers={'method': 'GET'})
|
||||
|
||||
def post(self, request):
|
||||
return text('I am post method')
|
||||
return text('', headers={'method': 'POST'})
|
||||
|
||||
def put(self, request):
|
||||
return text('I am put method')
|
||||
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('I am patch method')
|
||||
return text('', headers={'method': 'PATCH'})
|
||||
|
||||
def delete(self, request):
|
||||
return text('I am delete method')
|
||||
return text('', headers={'method': 'DELETE'})
|
||||
|
||||
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 == 'I am post method'
|
||||
request, response = sanic_endpoint_test(app, method="put")
|
||||
assert response.text == 'I am put method'
|
||||
request, response = sanic_endpoint_test(app, method="patch")
|
||||
assert response.text == 'I am patch method'
|
||||
request, response = sanic_endpoint_test(app, method="delete")
|
||||
assert response.text == 'I am delete method'
|
||||
request, response = sanic_endpoint_test(app, method=method)
|
||||
assert response.headers['method'] == method
|
||||
|
||||
|
||||
def test_unexisting_methods():
|
||||
|
||||
Reference in New Issue
Block a user