|
4 | 4 | require "timecop" |
5 | 5 |
|
6 | 6 | describe "#throttle" do |
| 7 | + let(:notifications) { [] } |
| 8 | + |
7 | 9 | before do |
8 | 10 | Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new |
9 | 11 | end |
|
138 | 140 | request.ip |
139 | 141 | end |
140 | 142 |
|
141 | | - notification_matched = nil |
142 | | - notification_type = nil |
143 | | - notification_data = nil |
144 | | - notification_discriminator = nil |
145 | | - |
146 | 143 | ActiveSupport::Notifications.subscribe("throttle.rack_attack") do |_name, _start, _finish, _id, payload| |
147 | | - notification_matched = payload[:request].env["rack.attack.matched"] |
148 | | - notification_type = payload[:request].env["rack.attack.match_type"] |
149 | | - notification_data = payload[:request].env['rack.attack.match_data'] |
150 | | - notification_discriminator = payload[:request].env['rack.attack.match_discriminator'] |
| 144 | + notifications.push(payload) |
151 | 145 | end |
152 | 146 |
|
153 | 147 | get "/", {}, "REMOTE_ADDR" => "5.6.7.8" |
154 | 148 |
|
155 | 149 | assert_equal 200, last_response.status |
156 | | - assert_nil notification_matched |
157 | | - assert_nil notification_type |
158 | | - assert_nil notification_data |
159 | | - assert_nil notification_discriminator |
| 150 | + assert notifications.empty? |
160 | 151 |
|
161 | 152 | get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
162 | 153 |
|
163 | 154 | assert_equal 200, last_response.status |
164 | | - assert_nil notification_matched |
165 | | - assert_nil notification_type |
166 | | - assert_nil notification_data |
167 | | - assert_nil notification_discriminator |
| 155 | + assert notifications.empty? |
168 | 156 |
|
169 | 157 | get "/", {}, "REMOTE_ADDR" => "1.2.3.4" |
170 | 158 |
|
171 | 159 | assert_equal 429, last_response.status |
172 | | - assert_equal "by ip", notification_matched |
173 | | - assert_equal :throttle, notification_type |
174 | | - assert_equal 60, notification_data[:period] |
175 | | - assert_equal 1, notification_data[:limit] |
176 | | - assert_equal 2, notification_data[:count] |
177 | | - assert_equal "1.2.3.4", notification_discriminator |
| 160 | + |
| 161 | + assert_equal 1, notifications.size |
| 162 | + notification = notifications.pop |
| 163 | + assert_equal "by ip", notification[:request].env["rack.attack.matched"] |
| 164 | + assert_equal :throttle, notification[:request].env["rack.attack.match_type"] |
| 165 | + assert_equal 60, notification[:request].env["rack.attack.match_data"][:period] |
| 166 | + assert_equal 1, notification[:request].env["rack.attack.match_data"][:limit] |
| 167 | + assert_equal 2, notification[:request].env["rack.attack.match_data"][:count] |
| 168 | + assert_equal "1.2.3.4", notification[:request].env["rack.attack.match_discriminator"] |
178 | 169 | end |
179 | 170 | end |
0 commit comments