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
44401273
Commit
44401273
authored
Aug 14, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1038 from jsatt/nontime_throttling
don't set X-Throttle-Wait-Second header if throttle wait is None
parents
5311f781
1d8a80f5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
33 additions
and
2 deletions
+33
-2
rest_framework/tests/test_throttling.py
+32
-1
rest_framework/views.py
+1
-1
No files found.
rest_framework/tests/test_throttling.py
View file @
44401273
...
...
@@ -7,7 +7,7 @@ from django.contrib.auth.models import User
from
django.core.cache
import
cache
from
rest_framework.test
import
APIRequestFactory
from
rest_framework.views
import
APIView
from
rest_framework.throttling
import
UserRateThrottle
,
ScopedRateThrottle
from
rest_framework.throttling
import
BaseThrottle
,
UserRateThrottle
,
ScopedRateThrottle
from
rest_framework.response
import
Response
...
...
@@ -21,6 +21,14 @@ class User3MinRateThrottle(UserRateThrottle):
scope
=
'minutes'
class
NonTimeThrottle
(
BaseThrottle
):
def
allow_request
(
self
,
request
,
view
):
if
not
hasattr
(
self
.
__class__
,
'called'
):
self
.
__class__
.
called
=
True
return
True
return
False
class
MockView
(
APIView
):
throttle_classes
=
(
User3SecRateThrottle
,)
...
...
@@ -35,6 +43,13 @@ class MockView_MinuteThrottling(APIView):
return
Response
(
'foo'
)
class
MockView_NonTimeThrottling
(
APIView
):
throttle_classes
=
(
NonTimeThrottle
,)
def
get
(
self
,
request
):
return
Response
(
'foo'
)
class
ThrottlingTests
(
TestCase
):
def
setUp
(
self
):
"""
...
...
@@ -140,6 +155,22 @@ class ThrottlingTests(TestCase):
(
80
,
None
)
))
def
test_non_time_throttle
(
self
):
"""
Ensure for second based throttles.
"""
request
=
self
.
factory
.
get
(
'/'
)
self
.
assertFalse
(
hasattr
(
MockView_NonTimeThrottling
.
throttle_classes
[
0
],
'called'
))
response
=
MockView_NonTimeThrottling
.
as_view
()(
request
)
self
.
assertFalse
(
'X-Throttle-Wait-Seconds'
in
response
)
self
.
assertTrue
(
MockView_NonTimeThrottling
.
throttle_classes
[
0
]
.
called
)
response
=
MockView_NonTimeThrottling
.
as_view
()(
request
)
self
.
assertFalse
(
'X-Throttle-Wait-Seconds'
in
response
)
class
ScopedRateThrottleTests
(
TestCase
):
"""
...
...
rest_framework/views.py
View file @
44401273
...
...
@@ -269,7 +269,7 @@ class APIView(View):
Handle any exception that occurs, by returning an appropriate response,
or re-raising the error.
"""
if
isinstance
(
exc
,
exceptions
.
Throttled
):
if
isinstance
(
exc
,
exceptions
.
Throttled
)
and
exc
.
wait
is
not
None
:
# Throttle wait header
self
.
headers
[
'X-Throttle-Wait-Seconds'
]
=
'
%
d'
%
exc
.
wait
...
...
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