这是视图:
@action(detail=False, permission_classes=(AllowAny,))
def anon_test_view(self, request):
limit = int(settings.REST_FRAMEWORK['DEFAULT_THROTTLE_RATES']['anon'].split('/')[0])
host_ip = '127.0.0.1'
anon_key = f'throttle_user_{host_ip}'
leaves = limit - len(cache.get(anon_key))
return Response({"ok": True, "leaves": leaves}, status=status.HTTP_200_OK)
这是测试:
TEST_THROTTLE_RATES = {
'anon': '40/day',
'user': '40/day',
}
...
# ограничение числа запросов от Анонимуса
test_rate_setting = copy.deepcopy(settings.REST_FRAMEWORK)
test_rate_setting['DEFAULT_THROTTLE_RATES'] = TEST_THROTTLE_RATES
@override_settings(REST_FRAMEWORK=test_rate_setting)
def testAnonRateCount(self):
cache.clear()
limit = int(settings.REST_FRAMEWORK['DEFAULT_THROTTLE_RATES']['anon'].split('/')[0])
try:
for i in range(limit + 1):
too_rate_request = self.app.get('/api/anon_test_view/')
print('leaves for anon:{}'.format(too_rate_request.json['leaves']))
except AppError as e:
responce_msg = e.args[0]
assert responce_msg.startswith('Bad response: 429')
简单地说:在设置中anonrate为400,但是在测试中我想将其限制为40,如果我不覆盖设置,则测试通过(除非发生),但没有覆盖异常,但不会发生-为什么?