mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-27 15:07:41 +00:00
Merge pull request #274 from grzuy/help_debug_cache_issues
Help users understand more clearly when the store is misconfigured
This commit is contained in:
commit
922917d5a4
4 changed files with 38 additions and 7 deletions
|
|
@ -2,6 +2,9 @@ require 'rack'
|
||||||
require 'forwardable'
|
require 'forwardable'
|
||||||
|
|
||||||
class Rack::Attack
|
class Rack::Attack
|
||||||
|
class MisconfiguredStoreError < StandardError; end
|
||||||
|
class MissingStoreError < StandardError; end
|
||||||
|
|
||||||
autoload :Cache, 'rack/attack/cache'
|
autoload :Cache, 'rack/attack/cache'
|
||||||
autoload :PathNormalizer, 'rack/attack/path_normalizer'
|
autoload :PathNormalizer, 'rack/attack/path_normalizer'
|
||||||
autoload :Check, 'rack/attack/check'
|
autoload :Check, 'rack/attack/check'
|
||||||
|
|
|
||||||
|
|
@ -46,15 +46,20 @@ module Rack
|
||||||
end
|
end
|
||||||
|
|
||||||
def do_count(key, expires_in)
|
def do_count(key, expires_in)
|
||||||
result = store.increment(key, 1, :expires_in => expires_in)
|
if store.nil?
|
||||||
|
raise Rack::Attack::MissingStoreError
|
||||||
|
elsif !store.respond_to?(:increment)
|
||||||
|
raise Rack::Attack::MisconfiguredStoreError, "Store needs to respond to #increment"
|
||||||
|
else
|
||||||
|
result = store.increment(key, 1, :expires_in => expires_in)
|
||||||
|
|
||||||
# NB: Some stores return nil when incrementing uninitialized values
|
# NB: Some stores return nil when incrementing uninitialized values
|
||||||
if result.nil?
|
if result.nil?
|
||||||
store.write(key, 1, :expires_in => expires_in)
|
store.write(key, 1, :expires_in => expires_in)
|
||||||
|
end
|
||||||
|
result || 1
|
||||||
end
|
end
|
||||||
result || 1
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
20
spec/rack_attack_store_config_spec.rb
Normal file
20
spec/rack_attack_store_config_spec.rb
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
require_relative 'spec_helper'
|
||||||
|
|
||||||
|
describe 'Store configuration' do
|
||||||
|
it "gives clear error when store it's not configured if it's needed" do
|
||||||
|
Rack::Attack.throttle('ip/sec', limit: 1, period: 60) { |req| req.ip }
|
||||||
|
|
||||||
|
assert_raises(Rack::Attack::MissingStoreError) do
|
||||||
|
get '/'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "gives clear error when store isn't configured properly" do
|
||||||
|
Rack::Attack.cache.store = Object.new
|
||||||
|
Rack::Attack.throttle('ip/sec', limit: 1, period: 60) { |req| req.ip }
|
||||||
|
|
||||||
|
assert_raises(Rack::Attack::MisconfiguredStoreError) do
|
||||||
|
get '/'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -19,7 +19,10 @@ class MiniTest::Spec
|
||||||
|
|
||||||
include Rack::Test::Methods
|
include Rack::Test::Methods
|
||||||
|
|
||||||
after { Rack::Attack.clear! }
|
after do
|
||||||
|
Rack::Attack.clear!
|
||||||
|
Rack::Attack.instance_variable_set(:@cache, nil)
|
||||||
|
end
|
||||||
|
|
||||||
def app
|
def app
|
||||||
Rack::Builder.new {
|
Rack::Builder.new {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue