Changeset View
Changeset View
Standalone View
Standalone View
swh/web/tests/api/test_throttling.py
Show All 13 Lines | |||||
from rest_framework.decorators import api_view | from rest_framework.decorators import api_view | ||||
from swh.web.api.throttling import SwhWebRateThrottle, throttle_scope | from swh.web.api.throttling import SwhWebRateThrottle, throttle_scope | ||||
from swh.web.settings.tests import ( | from swh.web.settings.tests import ( | ||||
scope1_limiter_rate, scope1_limiter_rate_post, | scope1_limiter_rate, scope1_limiter_rate_post, | ||||
scope2_limiter_rate, scope2_limiter_rate_post, | scope2_limiter_rate, scope2_limiter_rate_post, | ||||
scope3_limiter_rate, scope3_limiter_rate_post | scope3_limiter_rate, scope3_limiter_rate_post | ||||
) | ) | ||||
from swh.web.urls import urlpatterns | |||||
class MockViewScope1(APIView): | class MockViewScope1(APIView): | ||||
throttle_classes = (SwhWebRateThrottle,) | throttle_classes = (SwhWebRateThrottle,) | ||||
throttle_scope = 'scope1' | throttle_scope = 'scope1' | ||||
def get(self, request): | def get(self, request): | ||||
return Response('foo_get') | return Response('foo_get') | ||||
Show All 26 Lines | |||||
@throttle_scope('scope3') | @throttle_scope('scope3') | ||||
def mock_view_scope3(request): | def mock_view_scope3(request): | ||||
if request.method == 'GET': | if request.method == 'GET': | ||||
return Response('bar_get') | return Response('bar_get') | ||||
elif request.method == 'POST': | elif request.method == 'POST': | ||||
return Response('bar_post') | return Response('bar_post') | ||||
urlpatterns = [ | urlpatterns += [ | ||||
url(r'^scope1_class$', MockViewScope1.as_view()), | url(r'^scope1_class$', MockViewScope1.as_view()), | ||||
url(r'^scope2_func$', mock_view_scope2), | url(r'^scope2_func$', mock_view_scope2), | ||||
url(r'^scope3_class$', MockViewScope3.as_view()), | url(r'^scope3_class$', MockViewScope3.as_view()), | ||||
url(r'^scope3_func$', mock_view_scope3) | url(r'^scope3_func$', mock_view_scope3) | ||||
] | ] | ||||
def check_response(response, status_code, | def check_response(response, status_code, | ||||
▲ Show 20 Lines • Show All 119 Lines • Show Last 20 Lines |