Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
37d49429
Commit
37d49429
authored
Jan 19, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Raise assertion errors if @api_view decorator is applied incorrectly. Fixes #596.
parent
af3fd098
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
0 deletions
+31
-0
rest_framework/decorators.py
+9
-0
rest_framework/tests/decorators.py
+22
-0
No files found.
rest_framework/decorators.py
View file @
37d49429
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):
...
@@ -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
]
...
...
rest_framework/tests/decorators.py
View file @
37d49429
...
@@ -28,6 +28,28 @@ class DecoratorTestCase(TestCase):
...
@@ -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'
])
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment