|
@@ -39,7 +39,7 @@ class ThrottlingTestCase(TestCase):
|
|
|
def do_test():
|
|
|
view = MockView.as_view()
|
|
|
sum_delay = 0
|
|
|
- for delay, count in counts:
|
|
|
+ for delay, count, max_wait in counts:
|
|
|
sum_delay += delay
|
|
|
with mock.patch('desecapi.throttling.ScopedRatesThrottle.timer', return_value=time.time() + sum_delay):
|
|
|
for _ in range(count):
|
|
@@ -48,6 +48,7 @@ class ThrottlingTestCase(TestCase):
|
|
|
|
|
|
response = view(request)
|
|
|
self.assertEqual(response.status_code, status.HTTP_429_TOO_MANY_REQUESTS)
|
|
|
+ self.assertTrue(max_wait - 1 <= float(response['Retry-After']) <= max_wait)
|
|
|
|
|
|
cache.clear()
|
|
|
request = self.factory.get('/')
|
|
@@ -59,19 +60,19 @@ class ThrottlingTestCase(TestCase):
|
|
|
do_test()
|
|
|
|
|
|
def test_requests_are_throttled_4sec(self):
|
|
|
- self._test_requests_are_throttled(['4/sec'], [(0, 4), (1, 4)])
|
|
|
+ self._test_requests_are_throttled(['4/sec'], [(0, 4, 1), (1, 4, 1)])
|
|
|
|
|
|
def test_requests_are_throttled_4min(self):
|
|
|
- self._test_requests_are_throttled(['4/min'], [(0, 4)])
|
|
|
+ self._test_requests_are_throttled(['4/min'], [(0, 4, 60)])
|
|
|
|
|
|
def test_requests_are_throttled_multiple(self):
|
|
|
- self._test_requests_are_throttled(['5/s', '4/day'], [(0, 4)])
|
|
|
- self._test_requests_are_throttled(['4/s', '5/day'], [(0, 4)])
|
|
|
+ self._test_requests_are_throttled(['5/s', '4/day'], [(0, 4, 86400)])
|
|
|
+ self._test_requests_are_throttled(['4/s', '5/day'], [(0, 4, 1)])
|
|
|
|
|
|
def test_requests_are_throttled_multiple_cascade(self):
|
|
|
# We test that we can do 4 requests in the first second and only 2 in the second second
|
|
|
- self._test_requests_are_throttled(['4/s', '6/day'], [(0, 4), (1, 2)])
|
|
|
+ self._test_requests_are_throttled(['4/s', '6/day'], [(0, 4, 1), (1, 2, 86400)])
|
|
|
|
|
|
def test_requests_are_throttled_multiple_cascade_with_buckets(self):
|
|
|
# We test that we can do 4 requests in the first second and only 2 in the second second
|
|
|
- self._test_requests_are_throttled(['4/s', '6/day'], [(0, 4), (1, 2)], buckets=['foo', 'bar'])
|
|
|
+ self._test_requests_are_throttled(['4/s', '6/day'], [(0, 4, 1), (1, 2, 86400)], buckets=['foo', 'bar'])
|