diff options
| author | Tom Christie | 2013-08-14 05:15:57 -0700 | 
|---|---|---|
| committer | Tom Christie | 2013-08-14 05:15:57 -0700 | 
| commit | 44401273f7298185226c80bcb7d318e8ccbd2e37 (patch) | |
| tree | ffb91b87c952b0d74e76a4d3ac8cbc6922fcd906 | |
| parent | 5311f781fffa3b86dc3fe77d7520969b2232af47 (diff) | |
| parent | 1d8a80f5cc41f157403771439f15c9b7d615a43b (diff) | |
| download | django-rest-framework-44401273f7298185226c80bcb7d318e8ccbd2e37.tar.bz2 | |
Merge pull request #1038 from jsatt/nontime_throttling
don't set X-Throttle-Wait-Second header if throttle wait is None
| -rw-r--r-- | rest_framework/tests/test_throttling.py | 33 | ||||
| -rw-r--r-- | rest_framework/views.py | 2 | 
2 files changed, 33 insertions, 2 deletions
diff --git a/rest_framework/tests/test_throttling.py b/rest_framework/tests/test_throttling.py index 19bc691a..41bff692 100644 --- a/rest_framework/tests/test_throttling.py +++ b/rest_framework/tests/test_throttling.py @@ -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):      """ diff --git a/rest_framework/views.py b/rest_framework/views.py index 37bba7f0..d51233a9 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -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  | 
