mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-03-25 09:25:49 +00:00
Only require dalli when running dalli appraisal
This commit is contained in:
parent
93fc1641ea
commit
79de0d53e1
7 changed files with 136 additions and 123 deletions
|
|
@ -49,6 +49,5 @@ Gem::Specification.new do |s|
|
|||
s.add_development_dependency 'actionpack', '>= 3.0.0'
|
||||
s.add_development_dependency 'activesupport', '>= 3.0.0'
|
||||
s.add_development_dependency 'connection_pool'
|
||||
s.add_development_dependency 'dalli'
|
||||
s.add_development_dependency 'redis-activesupport'
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,39 +1,41 @@
|
|||
require_relative "../../spec_helper"
|
||||
require_relative "../../support/cache_store_helper"
|
||||
|
||||
require "active_support/cache/dalli_store"
|
||||
require "timecop"
|
||||
if defined?(::Dalli)
|
||||
require_relative "../../support/cache_store_helper"
|
||||
require "active_support/cache/dalli_store"
|
||||
require "timecop"
|
||||
|
||||
describe "ActiveSupport::Cache::DalliStore as a cache backend" do
|
||||
before do
|
||||
Rack::Attack.cache.store = ActiveSupport::Cache::DalliStore.new
|
||||
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
|
||||
describe "ActiveSupport::Cache::DalliStore as a cache backend" do
|
||||
before do
|
||||
Rack::Attack.cache.store = ActiveSupport::Cache::DalliStore.new
|
||||
end
|
||||
|
||||
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"
|
||||
after do
|
||||
Rack::Attack.cache.store.clear
|
||||
end
|
||||
|
||||
assert Rack::Attack.cache.store.fetch(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.fetch(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.fetch(key)
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil Rack::Attack.cache.store.fetch(key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,38 +1,40 @@
|
|||
require_relative "../../spec_helper"
|
||||
require_relative "../../support/cache_store_helper"
|
||||
|
||||
require "timecop"
|
||||
if defined?(::Dalli)
|
||||
require_relative "../../support/cache_store_helper"
|
||||
require "timecop"
|
||||
|
||||
describe "ActiveSupport::Cache::MemCacheStore as a cache backend" do
|
||||
before do
|
||||
Rack::Attack.cache.store = ActiveSupport::Cache::MemCacheStore.new
|
||||
end
|
||||
|
||||
after do
|
||||
Rack::Attack.cache.store.flush_all
|
||||
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
|
||||
describe "ActiveSupport::Cache::MemCacheStore as a cache backend" do
|
||||
before do
|
||||
Rack::Attack.cache.store = ActiveSupport::Cache::MemCacheStore.new
|
||||
end
|
||||
|
||||
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"
|
||||
after do
|
||||
Rack::Attack.cache.store.flush_all
|
||||
end
|
||||
|
||||
assert Rack::Attack.cache.store.get(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.get(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.get(key)
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil Rack::Attack.cache.store.get(key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,40 +1,42 @@
|
|||
require_relative "../../spec_helper"
|
||||
require_relative "../../support/cache_store_helper"
|
||||
|
||||
require "connection_pool"
|
||||
require "dalli"
|
||||
require "timecop"
|
||||
if defined?(::Dalli)
|
||||
require_relative "../../support/cache_store_helper"
|
||||
require "connection_pool"
|
||||
require "dalli"
|
||||
require "timecop"
|
||||
|
||||
describe "ConnectionPool with Dalli::Client as a cache backend" do
|
||||
before do
|
||||
Rack::Attack.cache.store = ConnectionPool.new { Dalli::Client.new }
|
||||
end
|
||||
|
||||
after do
|
||||
Rack::Attack.cache.store.with { |client| client.flush_all }
|
||||
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
|
||||
describe "ConnectionPool with Dalli::Client as a cache backend" do
|
||||
before do
|
||||
Rack::Attack.cache.store = ConnectionPool.new { Dalli::Client.new }
|
||||
end
|
||||
|
||||
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"
|
||||
after do
|
||||
Rack::Attack.cache.store.with { |client| client.flush_all }
|
||||
end
|
||||
|
||||
assert(Rack::Attack.cache.store.with { |client| client.fetch(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.with { |client| client.fetch(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.with { |client| client.fetch(key) })
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil(Rack::Attack.cache.store.with { |client| client.fetch(key) })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,39 +1,41 @@
|
|||
require_relative "../../spec_helper"
|
||||
require_relative "../../support/cache_store_helper"
|
||||
|
||||
require "dalli"
|
||||
require "timecop"
|
||||
if defined?(::Dalli)
|
||||
require_relative "../../support/cache_store_helper"
|
||||
require "dalli"
|
||||
require "timecop"
|
||||
|
||||
describe "Dalli::Client as a cache backend" do
|
||||
before do
|
||||
Rack::Attack.cache.store = Dalli::Client.new
|
||||
end
|
||||
|
||||
after do
|
||||
Rack::Attack.cache.store.flush_all
|
||||
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
|
||||
describe "Dalli::Client as a cache backend" do
|
||||
before do
|
||||
Rack::Attack.cache.store = Dalli::Client.new
|
||||
end
|
||||
|
||||
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"
|
||||
after do
|
||||
Rack::Attack.cache.store.flush_all
|
||||
end
|
||||
|
||||
assert Rack::Attack.cache.store.fetch(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.fetch(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.fetch(key)
|
||||
|
||||
sleep 2.1
|
||||
|
||||
assert_nil Rack::Attack.cache.store.fetch(key)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
require 'active_support/cache'
|
||||
require 'redis-activesupport'
|
||||
require 'dalli'
|
||||
require_relative '../spec_helper'
|
||||
|
||||
OfflineExamples = Minitest::SharedExamples.new do
|
||||
|
|
@ -27,17 +26,19 @@ describe 'when Redis is offline' do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'when Memcached is offline' do
|
||||
include OfflineExamples
|
||||
if defined?(::Dalli)
|
||||
describe 'when Memcached is offline' do
|
||||
include OfflineExamples
|
||||
|
||||
before do
|
||||
Dalli.logger.level = Logger::FATAL
|
||||
before do
|
||||
Dalli.logger.level = Logger::FATAL
|
||||
|
||||
@cache = Rack::Attack::Cache.new
|
||||
@cache.store = Dalli::Client.new('127.0.0.1:22122')
|
||||
end
|
||||
@cache = Rack::Attack::Cache.new
|
||||
@cache.store = Dalli::Client.new('127.0.0.1:22122')
|
||||
end
|
||||
|
||||
after do
|
||||
Dalli.logger.level = Logger::INFO
|
||||
after do
|
||||
Dalli.logger.level = Logger::INFO
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -13,6 +13,11 @@ if RUBY_ENGINE == "ruby"
|
|||
require "byebug"
|
||||
end
|
||||
|
||||
begin
|
||||
require "dalli"
|
||||
rescue LoadError
|
||||
end
|
||||
|
||||
class MiniTest::Spec
|
||||
include Rack::Test::Methods
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue