mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
Merge pull request #297 from grzuy/acceptance_test_store_config
Acceptance test cache store config for throttle without Rails
This commit is contained in:
commit
12710e5a07
2 changed files with 48 additions and 20 deletions
48
spec/acceptance/cache_store_config_for_throttle_spec.rb
Normal file
48
spec/acceptance/cache_store_config_for_throttle_spec.rb
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
require_relative "../spec_helper"
|
||||
|
||||
describe "Cache store config when throttling without Rails" do
|
||||
before do
|
||||
Rack::Attack.throttle("by ip", limit: 1, period: 60) do |request|
|
||||
request.ip
|
||||
end
|
||||
end
|
||||
|
||||
it "gives semantic error if no store was configured" do
|
||||
assert_raises(Rack::Attack::MissingStoreError) do
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
end
|
||||
end
|
||||
|
||||
it "gives semantic error if incompatible store was configured" do
|
||||
Rack::Attack.cache.store = Object.new
|
||||
|
||||
assert_raises(Rack::Attack::MisconfiguredStoreError) do
|
||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||
end
|
||||
end
|
||||
|
||||
it "works with any object that responds to #increment" do
|
||||
basic_store_class = Class.new do
|
||||
attr_accessor :counts
|
||||
|
||||
def initialize
|
||||
@counts = {}
|
||||
end
|
||||
|
||||
def increment(key, count, options)
|
||||
@counts[key] ||= 0
|
||||
@counts[key] += 1
|
||||
end
|
||||
end
|
||||
|
||||
Rack::Attack.cache.store = basic_store_class.new
|
||||
|
||||
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
|
||||
end
|
||||
end
|
||||
|
|
@ -1,20 +0,0 @@
|
|||
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
|
||||
Loading…
Reference in a new issue