mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-29 17:39:48 +03:00
Better throttle rate corner case handling
This commit is contained in:
parent
dd7adc7ed7
commit
c297600fb9
|
@ -106,15 +106,15 @@ class SimpleRateThrottle(BaseThrottle):
|
||||||
try:
|
try:
|
||||||
num, period = rate.split('/')
|
num, period = rate.split('/')
|
||||||
num_requests = int(num)
|
num_requests = int(num)
|
||||||
# Get rate multiplier value if available
|
period_timescale = re.findall(r'^\d*[s|m|h|d]', period)[0]
|
||||||
period_mult, _ = re.split('[s|m|h|d]', period, maxsplit=1)
|
period_char = period_timescale[-1]
|
||||||
period_char = re.findall('[s|m|h|d]', period)[0]
|
except (ValueError, IndexError):
|
||||||
except ValueError:
|
|
||||||
msg = "Incorrect throttle rate set for '%s' scope" % self.scope
|
msg = "Incorrect throttle rate set for '%s' scope" % self.scope
|
||||||
raise ImproperlyConfigured(msg)
|
raise ImproperlyConfigured(msg)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
period_mult = int(period_mult)
|
# Get rate multiplier value if available
|
||||||
|
period_mult = int(period_timescale[:-1])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
period_mult = 1
|
period_mult = 1
|
||||||
|
|
||||||
|
|
|
@ -474,11 +474,11 @@ class SimpleRateThrottleTests(TestCase):
|
||||||
with pytest.raises(ImproperlyConfigured):
|
with pytest.raises(ImproperlyConfigured):
|
||||||
SimpleRateThrottle()
|
SimpleRateThrottle()
|
||||||
|
|
||||||
SimpleRateThrottle.rate = '100/century'
|
SimpleRateThrottle.rate = '100/foos'
|
||||||
with pytest.raises(ImproperlyConfigured):
|
with pytest.raises(ImproperlyConfigured):
|
||||||
SimpleRateThrottle()
|
SimpleRateThrottle()
|
||||||
|
|
||||||
SimpleRateThrottle.rate = '100/10century'
|
SimpleRateThrottle.rate = '100/10foos'
|
||||||
with pytest.raises(ImproperlyConfigured):
|
with pytest.raises(ImproperlyConfigured):
|
||||||
SimpleRateThrottle()
|
SimpleRateThrottle()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user