diff --git a/spec/acceptance/stores/active_support_dalli_store_spec.rb b/spec/acceptance/stores/active_support_dalli_store_spec.rb index 45ecf3d..5896443 100644 --- a/spec/acceptance/stores/active_support_dalli_store_spec.rb +++ b/spec/acceptance/stores/active_support_dalli_store_spec.rb @@ -16,28 +16,6 @@ if defined?(::Dalli) 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 - - 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 + it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) }) end end diff --git a/spec/acceptance/stores/active_support_mem_cache_store_spec.rb b/spec/acceptance/stores/active_support_mem_cache_store_spec.rb index 86de47c..74e40f0 100644 --- a/spec/acceptance/stores/active_support_mem_cache_store_spec.rb +++ b/spec/acceptance/stores/active_support_mem_cache_store_spec.rb @@ -15,28 +15,6 @@ if defined?(::Dalli) 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 - 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" - end - - assert Rack::Attack.cache.store.get(key) - - sleep 2.1 - - assert_nil Rack::Attack.cache.store.get(key) - end + it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.get(key) }) end end diff --git a/spec/acceptance/stores/active_support_memory_store_spec.rb b/spec/acceptance/stores/active_support_memory_store_spec.rb index 8657aa2..e047b44 100644 --- a/spec/acceptance/stores/active_support_memory_store_spec.rb +++ b/spec/acceptance/stores/active_support_memory_store_spec.rb @@ -14,27 +14,5 @@ describe "ActiveSupport::Cache::MemoryStore as a cache backend" 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 - - 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 + it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) }) end diff --git a/spec/acceptance/stores/active_support_redis_cache_store_pooled_spec.rb b/spec/acceptance/stores/active_support_redis_cache_store_pooled_spec.rb index 13da998..860b765 100644 --- a/spec/acceptance/stores/active_support_redis_cache_store_pooled_spec.rb +++ b/spec/acceptance/stores/active_support_redis_cache_store_pooled_spec.rb @@ -15,28 +15,6 @@ if defined?(::ConnectionPool) && defined?(::Redis) && defined?(::ActiveSupport:: 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 - - 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 + it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) }) end end diff --git a/spec/acceptance/stores/active_support_redis_cache_store_spec.rb b/spec/acceptance/stores/active_support_redis_cache_store_spec.rb index 4303a3f..c9c4a91 100644 --- a/spec/acceptance/stores/active_support_redis_cache_store_spec.rb +++ b/spec/acceptance/stores/active_support_redis_cache_store_spec.rb @@ -15,29 +15,6 @@ if defined?(::Redis) && defined?(::ActiveSupport::Cache::RedisCacheStore) 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 - - 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" - - # puts key - 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 + it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) }) end end diff --git a/spec/acceptance/stores/active_support_redis_store_spec.rb b/spec/acceptance/stores/active_support_redis_store_spec.rb index 9aa7d08..7e7ce54 100644 --- a/spec/acceptance/stores/active_support_redis_store_spec.rb +++ b/spec/acceptance/stores/active_support_redis_store_spec.rb @@ -15,28 +15,6 @@ if defined?(::ActiveSupport::Cache::RedisStore) 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 - - 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 + it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.read(key) }) end end diff --git a/spec/acceptance/stores/connection_pool_dalli_client_spec.rb b/spec/acceptance/stores/connection_pool_dalli_client_spec.rb index 1ccf5d2..5689fbd 100644 --- a/spec/acceptance/stores/connection_pool_dalli_client_spec.rb +++ b/spec/acceptance/stores/connection_pool_dalli_client_spec.rb @@ -17,28 +17,8 @@ if defined?(::Dalli) && defined?(::ConnectionPool) 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 - 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" - 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 + it_works_for_cache_backed_features(fetch_from_store: ->(key) { + Rack::Attack.cache.store.with { |client| client.fetch(key) } + }) end end diff --git a/spec/acceptance/stores/dalli_client_spec.rb b/spec/acceptance/stores/dalli_client_spec.rb index dedd9ab..08038c2 100644 --- a/spec/acceptance/stores/dalli_client_spec.rb +++ b/spec/acceptance/stores/dalli_client_spec.rb @@ -16,28 +16,6 @@ if defined?(::Dalli) 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 - 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" - end - - assert Rack::Attack.cache.store.fetch(key) - - sleep 2.1 - - assert_nil Rack::Attack.cache.store.fetch(key) - end + it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.fetch(key) }) end end diff --git a/spec/acceptance/stores/redis_spec.rb b/spec/acceptance/stores/redis_spec.rb index 2674514..4361566 100644 --- a/spec/acceptance/stores/redis_spec.rb +++ b/spec/acceptance/stores/redis_spec.rb @@ -15,28 +15,6 @@ if defined?(::Redis) 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 - - 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 + it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.get(key) }) end end diff --git a/spec/acceptance/stores/redis_store_spec.rb b/spec/acceptance/stores/redis_store_spec.rb index 82898e6..d7e8e11 100644 --- a/spec/acceptance/stores/redis_store_spec.rb +++ b/spec/acceptance/stores/redis_store_spec.rb @@ -15,28 +15,6 @@ if defined?(::Redis::Store) 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 - - 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 + it_works_for_cache_backed_features(fetch_from_store: ->(key) { Rack::Attack.cache.store.read(key) }) end end diff --git a/spec/support/cache_store_helper.rb b/spec/support/cache_store_helper.rb index 7655949..5b8f04b 100644 --- a/spec/support/cache_store_helper.rb +++ b/spec/support/cache_store_helper.rb @@ -1,7 +1,9 @@ # frozen_string_literal: true class Minitest::Spec - def self.it_works_for_cache_backed_features + def self.it_works_for_cache_backed_features(options) + fetch_from_store = options.fetch(:fetch_from_store) + it "works for throttle" do Rack::Attack.throttle("by ip", limit: 1, period: 60) do |request| request.ip @@ -56,5 +58,27 @@ class Minitest::Spec get "/" assert_equal 403, last_response.status end + + it "doesn't leak keys" do + Rack::Attack.throttle("by ip", limit: 1, period: 1) do |request| + request.ip + 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" + end + + assert fetch_from_store.call(key) + + sleep 2.1 + + assert_nil fetch_from_store.call(key) + end end end