mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-28 09:55:51 +00:00
Acceptance test ability to access match data in #throttled_response
This commit is contained in:
parent
da1f54b6fc
commit
3f5574c4e4
1 changed files with 32 additions and 1 deletions
|
|
@ -1,13 +1,15 @@
|
|||
require_relative "../spec_helper"
|
||||
|
||||
describe "Customizing throttled response" do
|
||||
it "can be customized" do
|
||||
before do
|
||||
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
||||
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 60) do |request|
|
||||
request.ip
|
||||
end
|
||||
end
|
||||
|
||||
it "can be customized" do
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
|
||||
assert_equal 200, last_response.status
|
||||
|
|
@ -25,4 +27,33 @@ describe "Customizing throttled response" do
|
|||
assert_equal 503, last_response.status
|
||||
assert_equal "Throttled", last_response.body
|
||||
end
|
||||
|
||||
it "exposes match data" do
|
||||
matched = nil
|
||||
match_type = nil
|
||||
match_data = nil
|
||||
match_discriminator = nil
|
||||
|
||||
Rack::Attack.throttled_response = lambda do |env|
|
||||
matched = env['rack.attack.matched']
|
||||
match_type = env['rack.attack.match_type']
|
||||
match_data = env['rack.attack.match_data']
|
||||
match_discriminator = env['rack.attack.match_discriminator']
|
||||
|
||||
[429, {}, ["Throttled"]]
|
||||
end
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
|
||||
assert_equal "by ip", matched
|
||||
assert_equal :throttle, match_type
|
||||
assert_equal 60, match_data[:period]
|
||||
assert_equal 1, match_data[:limit]
|
||||
assert_equal 2, match_data[:count]
|
||||
assert_equal "1.2.3.4", match_discriminator
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
assert_equal 3, match_data[:count]
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue