Raise assertion errors if @api_view decorator is applied incorrectly. Fixes #596.

This commit is contained in:
Tom Christie 2013-01-19 15:51:14 +00:00
parent af3fd09845
commit 37d49429ca
2 changed files with 31 additions and 0 deletions

View File

@ -1,4 +1,5 @@
from rest_framework.views import APIView from rest_framework.views import APIView
import types
def api_view(http_method_names): def api_view(http_method_names):
@ -23,6 +24,14 @@ def api_view(http_method_names):
# pass # pass
# WrappedAPIView.__doc__ = func.doc <--- Not possible to do this # WrappedAPIView.__doc__ = func.doc <--- Not possible to do this
# api_view applied without (method_names)
assert not(isinstance(http_method_names, types.FunctionType)), \
'@api_view missing list of allowed HTTP methods'
# api_view applied with eg. string instead of list of strings
assert isinstance(http_method_names, (list, tuple)), \
'@api_view expected a list of strings, recieved %s' % type(http_method_names).__name__
allowed_methods = set(http_method_names) | set(('options',)) allowed_methods = set(http_method_names) | set(('options',))
WrappedAPIView.http_method_names = [method.lower() for method in allowed_methods] WrappedAPIView.http_method_names = [method.lower() for method in allowed_methods]

View File

@ -28,6 +28,28 @@ class DecoratorTestCase(TestCase):
response.request = request response.request = request
return APIView.finalize_response(self, request, response, *args, **kwargs) return APIView.finalize_response(self, request, response, *args, **kwargs)
def test_api_view_incorrect(self):
"""
If @api_view is not applied correct, we should raise an assertion.
"""
@api_view
def view(request):
return Response()
request = self.factory.get('/')
self.assertRaises(AssertionError, view, request)
def test_api_view_incorrect_arguments(self):
"""
If @api_view is missing arguments, we should raise an assertion.
"""
with self.assertRaises(AssertionError):
@api_view('GET')
def view(request):
return Response()
def test_calling_method(self): def test_calling_method(self):
@api_view(['GET']) @api_view(['GET'])