mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
Merge pull request #1508 from uploadcare/set-retry-after
set Retry-After header when throttled
This commit is contained in:
commit
3de09f1df0
|
@ -72,7 +72,7 @@ class UnsupportedMediaType(APIException):
|
|||
class Throttled(APIException):
|
||||
status_code = status.HTTP_429_TOO_MANY_REQUESTS
|
||||
default_detail = 'Request was throttled.'
|
||||
extra_detail = "Expected available in %d second%s."
|
||||
extra_detail = " Expected available in %d second%s."
|
||||
|
||||
def __init__(self, wait=None, detail=None):
|
||||
if wait is None:
|
||||
|
|
|
@ -62,6 +62,7 @@ def exception_handler(exc):
|
|||
headers['WWW-Authenticate'] = exc.auth_header
|
||||
if getattr(exc, 'wait', None):
|
||||
headers['X-Throttle-Wait-Seconds'] = '%d' % exc.wait
|
||||
headers['Retry-After'] = '%d' % exc.wait
|
||||
|
||||
return Response({'detail': exc.detail},
|
||||
status=exc.status_code,
|
||||
|
|
|
@ -118,8 +118,10 @@ class ThrottlingTests(TestCase):
|
|||
response = view.as_view()(request)
|
||||
if expect is not None:
|
||||
self.assertEqual(response['X-Throttle-Wait-Seconds'], expect)
|
||||
self.assertEqual(response['Retry-After'], expect)
|
||||
else:
|
||||
self.assertFalse('X-Throttle-Wait-Seconds' in response)
|
||||
self.assertFalse('Retry-After' in response)
|
||||
|
||||
def test_seconds_fields(self):
|
||||
"""
|
||||
|
@ -172,11 +174,13 @@ class ThrottlingTests(TestCase):
|
|||
|
||||
response = MockView_NonTimeThrottling.as_view()(request)
|
||||
self.assertFalse('X-Throttle-Wait-Seconds' in response)
|
||||
self.assertFalse('Retry-After' in response)
|
||||
|
||||
self.assertTrue(MockView_NonTimeThrottling.throttle_classes[0].called)
|
||||
|
||||
response = MockView_NonTimeThrottling.as_view()(request)
|
||||
self.assertFalse('X-Throttle-Wait-Seconds' in response)
|
||||
self.assertFalse('Retry-After' in response)
|
||||
|
||||
|
||||
class ScopedRateThrottleTests(TestCase):
|
||||
|
|
Loading…
Reference in New Issue
Block a user