From 13803bdb304e78355a8b45078ca8c36adedc6775 Mon Sep 17 00:00:00 2001 From: Eli Uriegas Date: Fri, 27 Jan 2017 22:05:46 -0600 Subject: [PATCH] Update for HTTPMethodView compatibility --- sanic/constants.py | 1 + sanic/sanic.py | 10 +++++++++- tests/test_views.py | 34 ++++++++++++++++++---------------- 3 files changed, 28 insertions(+), 17 deletions(-) create mode 100644 sanic/constants.py diff --git a/sanic/constants.py b/sanic/constants.py new file mode 100644 index 00000000..8bb97ed3 --- /dev/null +++ b/sanic/constants.py @@ -0,0 +1 @@ +HTTP_METHODS = ('GET', 'POST', 'PUT', 'HEAD', 'OPTIONS', 'PATCH', 'DELETE') \ No newline at end of file diff --git a/sanic/sanic.py b/sanic/sanic.py index 00b1961b..1e4de2fb 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -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 diff --git a/tests/test_views.py b/tests/test_views.py index 24647cf6..3c695500 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -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():