File tree Expand file tree Collapse file tree 3 files changed +17
-0
lines changed Expand file tree Collapse file tree 3 files changed +17
-0
lines changed Original file line number Diff line number Diff line change 7
7
from django .utils .decorators import method_decorator
8
8
from django .views .generic import View
9
9
10
+ from rest_framework .decorators import api_view
11
+ from rest_framework .test import APIRequestFactory
12
+
10
13
from django_ratelimit .decorators import ratelimit
11
14
from django_ratelimit .exceptions import Ratelimited
12
15
from django_ratelimit .core import (get_usage , is_ratelimited ,
13
16
_split_rate , _get_ip )
14
17
15
18
16
19
rf = RequestFactory ()
20
+ rest_rf = APIRequestFactory ()
17
21
18
22
19
23
class MockUser :
@@ -152,6 +156,17 @@ def view(request):
152
156
assert not view (rf .post ('/' , {'foo' : 'b' }))
153
157
assert view (rf .post ('/' , {'foo' : 'b' }))
154
158
159
+ def test_key_data (self ):
160
+ @api_view (['POST' ])
161
+ @ratelimit (key = 'data:foo' , rate = '1/m' , block = False )
162
+ def view (request ):
163
+ return request .limited
164
+
165
+ assert not view (rest_rf .post ('/' , {'foo' : 'a' }))
166
+ assert view (rest_rf .post ('/' , {'foo' : 'a' }))
167
+ assert not view (rest_rf .post ('/' , {'foo' : 'b' }))
168
+ assert view (rest_rf .post ('/' , {'foo' : 'b' }))
169
+
155
170
def test_key_header (self ):
156
171
def _req ():
157
172
req = rf .post ('/' )
Original file line number Diff line number Diff line change 4
4
5
5
INSTALLED_APPS = (
6
6
'django_ratelimit' ,
7
+ 'rest_framework' ,
7
8
)
8
9
9
10
RATELIMIT_USE_CACHE = 'default'
Original file line number Diff line number Diff line change 15
15
django41: Django>=4.1,<4.2
16
16
django42: Django>=4.2,<4.3
17
17
djangomain: https://github.com/django/django/archive/main.tar.gz
18
+ djangorestframework~=3.14.0
18
19
pymemcache>=4.0,<5.0
19
20
django-redis>=5.2,<6.0
20
21
flake8
You can’t perform that action at this time.
0 commit comments