Update for HTTPMethodView compatibility

This commit is contained in:
Eli Uriegas 2017-01-27 22:05:46 -06:00
parent 41c52487ee
commit 13803bdb30
3 changed files with 28 additions and 17 deletions

1
sanic/constants.py Normal file
View File

@ -0,0 +1 @@
HTTP_METHODS = ('GET', 'POST', 'PUT', 'HEAD', 'OPTIONS', 'PATCH', 'DELETE')

View File

@ -6,6 +6,7 @@ from inspect import isawaitable, stack, getmodulename
from traceback import format_exc
from .config import Config
from .constants import HTTP_METHODS
from .exceptions import Handler
from .exceptions import ServerError
from .log import log
@ -90,6 +91,9 @@ class Sanic:
def patch(self, uri, host=None):
return self.route(uri, methods=["PATCH"], host=host)
def delete(self, uri, host=None):
return self.route(uri, methods=["DELETE"], host=host)
def add_route(self, handler, uri, methods=frozenset({'GET'}), host=None):
"""
A helper method to register class instance or
@ -98,9 +102,13 @@ class Sanic:
:param handler: function or class instance
:param uri: path of the URL
:param methods: list or tuple of methods allowed
:param methods: list or tuple of methods allowed, these are overridden
if using a HTTPMethodView
:return: function or class instance
"""
# Handle HTTPMethodView differently
if hasattr(handler, 'view_class'):
methods = frozenset(HTTP_METHODS)
self.route(uri=uri, methods=methods, host=host)(handler)
return handler

View File

@ -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('I am GET method')
def post(self, request):
return text('I am post method')
return text('I am POST method')
def put(self, request):
return text('I am put method')
return text('I am PUT method')
def head(self, request):
return text('I am HEAD method')
def options(self, request):
return text('I am OPTIONS method')
def patch(self, request):
return text('I am patch method')
return text('I am PATCH method')
def delete(self, request):
return text('I am delete method')
return text('I am DELETE 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 == '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.text == 'I am {} method'.format(method)
def test_unexisting_methods():