From 957b39fa8d6ff0f9866d0f697c4ff93a343613b7 Mon Sep 17 00:00:00 2001 From: Christopher Grebs Date: Tue, 30 Jul 2019 09:40:32 +0200 Subject: [PATCH] Wrap test in try/finally, cleanup duration determination --- rest_framework/views.py | 6 ++---- tests/test_throttling.py | 17 +++++++++-------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/rest_framework/views.py b/rest_framework/views.py index 5f52adaa5..da85cccec 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -363,10 +363,8 @@ class APIView(View): if duration is not None ] - if durations: - self.throttled(request, max(durations)) - else: - self.throttled(request, None) + duration = max(durations) if durations else None + self.throttled(request, duration) def determine_version(self, request, *args, **kwargs): """ diff --git a/tests/test_throttling.py b/tests/test_throttling.py index 2cc60a1a7..eb34a6c91 100644 --- a/tests/test_throttling.py +++ b/tests/test_throttling.py @@ -168,16 +168,17 @@ class ThrottlingTests(TestCase): assert int(response['retry-after']) == 60 previous_rate = User3SecRateThrottle.rate - User3SecRateThrottle.rate = '1/sec' + try: + User3SecRateThrottle.rate = '1/sec' - for dummy in range(24): - response = MockView_DoubleThrottling.as_view()(request) + for dummy in range(24): + response = MockView_DoubleThrottling.as_view()(request) - assert response.status_code == 429 - assert int(response['retry-after']) == 60 - - # reset - User3SecRateThrottle.rate = previous_rate + assert response.status_code == 429 + assert int(response['retry-after']) == 60 + finally: + # reset + User3SecRateThrottle.rate = previous_rate def ensure_response_header_contains_proper_throttle_field(self, view, expected_headers): """