mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
Acceptance test ability to customize blocked/throttled responses (#298)
* Acceptance test ability to customize blocked/throttled responses * Don't let customizations to blocklisted/throttled responses leak to other test cases
This commit is contained in:
parent
12710e5a07
commit
666dc3d894
3 changed files with 58 additions and 0 deletions
22
spec/acceptance/customizing_blocked_response_spec.rb
Normal file
22
spec/acceptance/customizing_blocked_response_spec.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
require_relative "../spec_helper"
|
||||
|
||||
describe "Customizing block responses" do
|
||||
it "can be customized" do
|
||||
Rack::Attack.blocklist("block 1.2.3.4") do |request|
|
||||
request.ip == "1.2.3.4"
|
||||
end
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
|
||||
assert_equal 403, last_response.status
|
||||
|
||||
Rack::Attack.blocklisted_response = lambda do |env|
|
||||
[503, {}, ["Blocked"]]
|
||||
end
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
|
||||
assert_equal 503, last_response.status
|
||||
assert_equal "Blocked", last_response.body
|
||||
end
|
||||
end
|
||||
28
spec/acceptance/customizing_throttled_response_spec.rb
Normal file
28
spec/acceptance/customizing_throttled_response_spec.rb
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
require_relative "../spec_helper"
|
||||
|
||||
describe "Customizing throttled response" do
|
||||
it "can be customized" do
|
||||
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
|
||||
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 60) do |request|
|
||||
request.ip
|
||||
end
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
|
||||
assert_equal 200, last_response.status
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
|
||||
assert_equal 429, last_response.status
|
||||
|
||||
Rack::Attack.throttled_response = lambda do |env|
|
||||
[503, {}, ["Throttled"]]
|
||||
end
|
||||
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
|
||||
assert_equal 503, last_response.status
|
||||
assert_equal "Throttled", last_response.body
|
||||
end
|
||||
end
|
||||
|
|
@ -23,9 +23,17 @@ class MiniTest::Spec
|
|||
|
||||
include Rack::Test::Methods
|
||||
|
||||
before do
|
||||
@_original_throttled_response = Rack::Attack.throttled_response
|
||||
@_original_blocklisted_response = Rack::Attack.blocklisted_response
|
||||
end
|
||||
|
||||
after do
|
||||
Rack::Attack.clear!
|
||||
Rack::Attack.instance_variable_set(:@cache, nil)
|
||||
|
||||
Rack::Attack.throttled_response = @_original_throttled_response
|
||||
Rack::Attack.blocklisted_response = @_original_blocklisted_response
|
||||
end
|
||||
|
||||
def app
|
||||
|
|
|
|||
Loading…
Reference in a new issue