forked from rack/rack-attack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR removes the
rescuing
blocks from Dalli/RedisProxy classes a…
…nd instead catches errors at the top level.
- Loading branch information
1 parent
27ada96
commit 7f72d4f
Showing
8 changed files
with
156 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../spec_helper" | ||
|
||
describe "error handling" do | ||
|
||
let(:store) do | ||
ActiveSupport::Cache::MemoryStore.new | ||
end | ||
|
||
before do | ||
Rack::Attack.cache.store = store | ||
|
||
Rack::Attack.blocklist("fail2ban pentesters") do |request| | ||
Rack::Attack::Fail2Ban.filter(request.ip, maxretry: 0, bantime: 600, findtime: 30) { true } | ||
end | ||
end | ||
|
||
describe '.allowed_errors' do | ||
before do | ||
allow(store).to receive(:read).and_raise(RuntimeError) | ||
end | ||
|
||
it 'has default value' do | ||
assert_equal Rack::Attack.allowed_errors, %w[Dalli::DalliError Redis::BaseError] | ||
end | ||
|
||
it 'can get and set value' do | ||
Rack::Attack.allowed_errors = %w[Foobar] | ||
assert_equal Rack::Attack.allowed_errors, %w[Foobar] | ||
end | ||
|
||
it 'can ignore error as Class' do | ||
Rack::Attack.allowed_errors = [RuntimeError] | ||
|
||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4" | ||
|
||
assert_equal 200, last_response.status | ||
end | ||
|
||
it 'can ignore error ancestor as Class' do | ||
Rack::Attack.allowed_errors = [StandardError] | ||
|
||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4" | ||
|
||
assert_equal 200, last_response.status | ||
end | ||
|
||
it 'can ignore error as String' do | ||
Rack::Attack.allowed_errors = %w[RuntimeError] | ||
|
||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4" | ||
|
||
assert_equal 200, last_response.status | ||
end | ||
|
||
it 'can ignore error error ancestor as String' do | ||
Rack::Attack.allowed_errors = %w[StandardError] | ||
|
||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4" | ||
|
||
assert_equal 200, last_response.status | ||
end | ||
|
||
it 'raises error if not ignored' do | ||
assert_raises(RuntimeError) do | ||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4" | ||
end | ||
end | ||
end | ||
|
||
describe '.allowed_errors?' do | ||
|
||
it 'can match String or Class' do | ||
Rack::Attack.allowed_errors = ['ArgumentError', RuntimeError] | ||
assert Rack::Attack.allow_error?(ArgumentError.new) | ||
assert Rack::Attack.allow_error?(RuntimeError.new) | ||
refute Rack::Attack.allow_error?(StandardError.new) | ||
end | ||
|
||
it 'can match Class ancestors' do | ||
Rack::Attack.allowed_errors = [StandardError] | ||
assert Rack::Attack.allow_error?(ArgumentError.new) | ||
refute Rack::Attack.allow_error?(Exception.new) | ||
end | ||
|
||
it 'can match String ancestors' do | ||
Rack::Attack.allowed_errors = ['StandardError'] | ||
assert Rack::Attack.allow_error?(ArgumentError.new) | ||
refute Rack::Attack.allow_error?(Exception.new) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters