mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-26 14:57:47 +00:00
Only require redis stores when running their respective appraisal
This commit is contained in:
parent
bd2ade8977
commit
a72bfb5fc7
12 changed files with 159 additions and 94 deletions
19
Appraisals
19
Appraisals
|
|
@ -37,3 +37,22 @@ appraise "connection_pool_dalli" do
|
||||||
gem "connection_pool", "~> 2.2"
|
gem "connection_pool", "~> 2.2"
|
||||||
gem "dalli", "~> 2.7"
|
gem "dalli", "~> 2.7"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
appraise "active_support_redis_cache_store" do
|
||||||
|
gem "activesupport", "~> 5.2.0"
|
||||||
|
gem "redis", "~> 4.0"
|
||||||
|
end
|
||||||
|
|
||||||
|
appraise "active_support_redis_cache_store_pooled" do
|
||||||
|
gem "activesupport", "~> 5.2.0"
|
||||||
|
gem "connection_pool", "~> 2.2"
|
||||||
|
gem "redis", "~> 4.0"
|
||||||
|
end
|
||||||
|
|
||||||
|
appraise "redis_store" do
|
||||||
|
gem "redis-store", "~> 1.5"
|
||||||
|
end
|
||||||
|
|
||||||
|
appraise "active_support_redis_store" do
|
||||||
|
gem "redis-activesupport", "~> 5.0"
|
||||||
|
end
|
||||||
|
|
|
||||||
8
gemfiles/active_support_redis_cache_store.gemfile
Normal file
8
gemfiles/active_support_redis_cache_store.gemfile
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
# This file was generated by Appraisal
|
||||||
|
|
||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
gem "activesupport", "~> 5.2.0"
|
||||||
|
gem "redis", "~> 4.0"
|
||||||
|
|
||||||
|
gemspec path: "../"
|
||||||
9
gemfiles/active_support_redis_cache_store_pooled.gemfile
Normal file
9
gemfiles/active_support_redis_cache_store_pooled.gemfile
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
# This file was generated by Appraisal
|
||||||
|
|
||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
gem "activesupport", "~> 5.2.0"
|
||||||
|
gem "connection_pool", "~> 2.2"
|
||||||
|
gem "redis", "~> 4.0"
|
||||||
|
|
||||||
|
gemspec path: "../"
|
||||||
7
gemfiles/active_support_redis_store.gemfile
Normal file
7
gemfiles/active_support_redis_store.gemfile
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file was generated by Appraisal
|
||||||
|
|
||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
gem "redis-activesupport", "~> 5.0"
|
||||||
|
|
||||||
|
gemspec path: "../"
|
||||||
7
gemfiles/redis_store.gemfile
Normal file
7
gemfiles/redis_store.gemfile
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file was generated by Appraisal
|
||||||
|
|
||||||
|
source "https://rubygems.org"
|
||||||
|
|
||||||
|
gem "redis-store", "~> 1.5"
|
||||||
|
|
||||||
|
gemspec path: "../"
|
||||||
|
|
@ -48,5 +48,4 @@ Gem::Specification.new do |s|
|
||||||
# which rack-attack uses only for testing compatibility in test suite.
|
# which rack-attack uses only for testing compatibility in test suite.
|
||||||
s.add_development_dependency 'actionpack', '>= 3.0.0'
|
s.add_development_dependency 'actionpack', '>= 3.0.0'
|
||||||
s.add_development_dependency 'activesupport', '>= 3.0.0'
|
s.add_development_dependency 'activesupport', '>= 3.0.0'
|
||||||
s.add_development_dependency 'redis-activesupport'
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,42 +1,40 @@
|
||||||
require_relative "../../spec_helper"
|
require_relative "../../spec_helper"
|
||||||
|
|
||||||
if defined?(::ConnectionPool)
|
if defined?(::ConnectionPool) && defined?(::Redis) && defined?(::ActiveSupport::Cache::RedisCacheStore)
|
||||||
require_relative "../../support/cache_store_helper"
|
require_relative "../../support/cache_store_helper"
|
||||||
require "timecop"
|
require "timecop"
|
||||||
|
|
||||||
if ActiveSupport.version >= Gem::Version.new("5.2.0")
|
describe "ActiveSupport::Cache::RedisCacheStore (pooled) as a cache backend" do
|
||||||
describe "ActiveSupport::Cache::RedisCacheStore (pooled) as a cache backend" do
|
before do
|
||||||
before do
|
Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new(pool_size: 2)
|
||||||
Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new(pool_size: 2)
|
end
|
||||||
|
|
||||||
|
after do
|
||||||
|
Rack::Attack.cache.store.clear
|
||||||
|
end
|
||||||
|
|
||||||
|
it_works_for_cache_backed_features
|
||||||
|
|
||||||
|
it "doesn't leak keys" do
|
||||||
|
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
||||||
|
request.ip
|
||||||
end
|
end
|
||||||
|
|
||||||
after do
|
key = nil
|
||||||
Rack::Attack.cache.store.clear
|
|
||||||
|
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
||||||
|
# we pre-calculate in local variable `key`
|
||||||
|
Timecop.freeze do
|
||||||
|
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
||||||
|
|
||||||
|
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||||
end
|
end
|
||||||
|
|
||||||
it_works_for_cache_backed_features
|
assert Rack::Attack.cache.store.fetch(key)
|
||||||
|
|
||||||
it "doesn't leak keys" do
|
sleep 2.1
|
||||||
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
|
||||||
request.ip
|
|
||||||
end
|
|
||||||
|
|
||||||
key = nil
|
assert_nil Rack::Attack.cache.store.fetch(key)
|
||||||
|
|
||||||
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
|
||||||
# we pre-calculate in local variable `key`
|
|
||||||
Timecop.freeze do
|
|
||||||
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
|
||||||
|
|
||||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
|
||||||
end
|
|
||||||
|
|
||||||
assert Rack::Attack.cache.store.fetch(key)
|
|
||||||
|
|
||||||
sleep 2.1
|
|
||||||
|
|
||||||
assert_nil Rack::Attack.cache.store.fetch(key)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,9 @@
|
||||||
require_relative "../../spec_helper"
|
require_relative "../../spec_helper"
|
||||||
require_relative "../../support/cache_store_helper"
|
|
||||||
|
|
||||||
require "timecop"
|
if defined?(::Redis) && defined?(::ActiveSupport::Cache::RedisCacheStore)
|
||||||
|
require_relative "../../support/cache_store_helper"
|
||||||
|
require "timecop"
|
||||||
|
|
||||||
if ActiveSupport.version >= Gem::Version.new("5.2.0")
|
|
||||||
describe "ActiveSupport::Cache::RedisCacheStore as a cache backend" do
|
describe "ActiveSupport::Cache::RedisCacheStore as a cache backend" do
|
||||||
before do
|
before do
|
||||||
Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new
|
Rack::Attack.cache.store = ActiveSupport::Cache::RedisCacheStore.new
|
||||||
|
|
|
||||||
|
|
@ -1,39 +1,40 @@
|
||||||
require_relative "../../spec_helper"
|
require_relative "../../spec_helper"
|
||||||
require_relative "../../support/cache_store_helper"
|
|
||||||
|
|
||||||
require "redis-activesupport"
|
if defined?(::ActiveSupport::Cache::RedisStore)
|
||||||
require "timecop"
|
require_relative "../../support/cache_store_helper"
|
||||||
|
require "timecop"
|
||||||
|
|
||||||
describe "ActiveSupport::Cache::RedisStore as a cache backend" do
|
describe "ActiveSupport::Cache::RedisStore as a cache backend" do
|
||||||
before do
|
before do
|
||||||
Rack::Attack.cache.store = ActiveSupport::Cache::RedisStore.new
|
Rack::Attack.cache.store = ActiveSupport::Cache::RedisStore.new
|
||||||
end
|
|
||||||
|
|
||||||
after do
|
|
||||||
Rack::Attack.cache.store.flushdb
|
|
||||||
end
|
|
||||||
|
|
||||||
it_works_for_cache_backed_features
|
|
||||||
|
|
||||||
it "doesn't leak keys" do
|
|
||||||
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
|
||||||
request.ip
|
|
||||||
end
|
end
|
||||||
|
|
||||||
key = nil
|
after do
|
||||||
|
Rack::Attack.cache.store.flushdb
|
||||||
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
|
||||||
# we pre-calculate in local variable `key`
|
|
||||||
Timecop.freeze do
|
|
||||||
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
|
||||||
|
|
||||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
assert Rack::Attack.cache.store.read(key)
|
it_works_for_cache_backed_features
|
||||||
|
|
||||||
sleep 2.1
|
it "doesn't leak keys" do
|
||||||
|
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
||||||
|
request.ip
|
||||||
|
end
|
||||||
|
|
||||||
assert_nil Rack::Attack.cache.store.read(key)
|
key = nil
|
||||||
|
|
||||||
|
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
||||||
|
# we pre-calculate in local variable `key`
|
||||||
|
Timecop.freeze do
|
||||||
|
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
||||||
|
|
||||||
|
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert Rack::Attack.cache.store.read(key)
|
||||||
|
|
||||||
|
sleep 2.1
|
||||||
|
|
||||||
|
assert_nil Rack::Attack.cache.store.read(key)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,39 +1,40 @@
|
||||||
require_relative "../../spec_helper"
|
require_relative "../../spec_helper"
|
||||||
require_relative "../../support/cache_store_helper"
|
require_relative "../../support/cache_store_helper"
|
||||||
|
|
||||||
require "redis-store"
|
if defined?(::Redis::Store)
|
||||||
require "timecop"
|
require "timecop"
|
||||||
|
|
||||||
describe "ActiveSupport::Cache::RedisStore as a cache backend" do
|
describe "ActiveSupport::Cache::RedisStore as a cache backend" do
|
||||||
before do
|
before do
|
||||||
Rack::Attack.cache.store = ::Redis::Store.new
|
Rack::Attack.cache.store = ::Redis::Store.new
|
||||||
end
|
|
||||||
|
|
||||||
after do
|
|
||||||
Rack::Attack.cache.store.flushdb
|
|
||||||
end
|
|
||||||
|
|
||||||
it_works_for_cache_backed_features
|
|
||||||
|
|
||||||
it "doesn't leak keys" do
|
|
||||||
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
|
||||||
request.ip
|
|
||||||
end
|
end
|
||||||
|
|
||||||
key = nil
|
after do
|
||||||
|
Rack::Attack.cache.store.flushdb
|
||||||
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
|
||||||
# we pre-calculate in local variable `key`
|
|
||||||
Timecop.freeze do
|
|
||||||
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
|
||||||
|
|
||||||
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
assert Rack::Attack.cache.store.read(key)
|
it_works_for_cache_backed_features
|
||||||
|
|
||||||
sleep 2.1
|
it "doesn't leak keys" do
|
||||||
|
Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request|
|
||||||
|
request.ip
|
||||||
|
end
|
||||||
|
|
||||||
assert_nil Rack::Attack.cache.store.read(key)
|
key = nil
|
||||||
|
|
||||||
|
# Freeze time during these statement to be sure that the key used by rack attack is the same
|
||||||
|
# we pre-calculate in local variable `key`
|
||||||
|
Timecop.freeze do
|
||||||
|
key = "rack::attack:#{Time.now.to_i}:by ip:1.2.3.4"
|
||||||
|
|
||||||
|
get "/", {}, "REMOTE_ADDR" => "1.2.3.4"
|
||||||
|
end
|
||||||
|
|
||||||
|
assert Rack::Attack.cache.store.read(key)
|
||||||
|
|
||||||
|
sleep 2.1
|
||||||
|
|
||||||
|
assert_nil Rack::Attack.cache.store.read(key)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
require 'active_support/cache'
|
require 'active_support/cache'
|
||||||
require 'redis-activesupport'
|
|
||||||
require_relative '../spec_helper'
|
require_relative '../spec_helper'
|
||||||
|
|
||||||
OfflineExamples = Minitest::SharedExamples.new do
|
OfflineExamples = Minitest::SharedExamples.new do
|
||||||
|
|
@ -16,13 +15,15 @@ OfflineExamples = Minitest::SharedExamples.new do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'when Redis is offline' do
|
if defined?(::ActiveSupport::Cache::RedisStore)
|
||||||
include OfflineExamples
|
describe 'when Redis is offline' do
|
||||||
|
include OfflineExamples
|
||||||
|
|
||||||
before do
|
before do
|
||||||
@cache = Rack::Attack::Cache.new
|
@cache = Rack::Attack::Cache.new
|
||||||
# Use presumably unused port for Redis client
|
# Use presumably unused port for Redis client
|
||||||
@cache.store = ActiveSupport::Cache::RedisStore.new(:host => '127.0.0.1', :port => 3333)
|
@cache.store = ActiveSupport::Cache::RedisStore.new(:host => '127.0.0.1', :port => 3333)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,21 @@ begin
|
||||||
rescue LoadError
|
rescue LoadError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
require "redis"
|
||||||
|
rescue LoadError
|
||||||
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
require "redis-activesupport"
|
||||||
|
rescue LoadError
|
||||||
|
end
|
||||||
|
|
||||||
|
begin
|
||||||
|
require "redis-store"
|
||||||
|
rescue LoadError
|
||||||
|
end
|
||||||
|
|
||||||
class MiniTest::Spec
|
class MiniTest::Spec
|
||||||
include Rack::Test::Methods
|
include Rack::Test::Methods
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue