File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -123,6 +123,7 @@ def self.without_modules(*modules)
123
123
BasicImplicitRender ,
124
124
StrongParameters ,
125
125
RateLimiting ,
126
+ Caching ,
126
127
127
128
DataStreaming ,
128
129
DefaultHeaders ,
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require "abstract_unit"
4
+
5
+ class ApiRateLimitedController < ActionController ::API
6
+ self . cache_store = ActiveSupport ::Cache ::MemoryStore . new
7
+ rate_limit to : 2 , within : 2 . seconds , only : :limited_to_two
8
+
9
+ def limited_to_two
10
+ head :ok
11
+ end
12
+ end
13
+
14
+ class ApiRateLimitingTest < ActionController ::TestCase
15
+ tests ApiRateLimitedController
16
+
17
+ setup do
18
+ ApiRateLimitedController . cache_store . clear
19
+ end
20
+
21
+ test "exceeding basic limit" do
22
+ get :limited_to_two
23
+ get :limited_to_two
24
+ assert_response :ok
25
+
26
+ get :limited_to_two
27
+ assert_response :too_many_requests
28
+ end
29
+
30
+ test "limit resets after time" do
31
+ get :limited_to_two
32
+ get :limited_to_two
33
+ assert_response :ok
34
+
35
+ travel_to Time . now + 3 . seconds do
36
+ get :limited_to_two
37
+ assert_response :ok
38
+ end
39
+ end
40
+ end
You can’t perform that action at this time.
0 commit comments