mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-27 15:07:41 +00:00
Acceptance test ActiveSupport::Cache::MemoryStore (via activesupport) as cache store backend
This commit is contained in:
parent
1f05ff30d5
commit
bcc1f5857e
2 changed files with 103 additions and 72 deletions
38
spec/acceptance/stores/active_support_memory_store_spec.rb
Normal file
38
spec/acceptance/stores/active_support_memory_store_spec.rb
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
require_relative "../../spec_helper"
|
||||||
|
require_relative "../../support/cache_store_helper"
|
||||||
|
|
||||||
|
require "timecop"
|
||||||
|
|
||||||
|
describe "ActiveSupport::Cache::MemoryStore as a cache backend" do
|
||||||
|
before do
|
||||||
|
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.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
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
@ -17,96 +17,89 @@ describe Rack::Attack::Cache do
|
||||||
|
|
||||||
require 'connection_pool'
|
require 'connection_pool'
|
||||||
|
|
||||||
cache_stores = [
|
store = Rack::Attack::StoreProxy.build(ConnectionPool.new { Dalli::Client.new })
|
||||||
ActiveSupport::Cache::MemoryStore.new,
|
|
||||||
ConnectionPool.new { Dalli::Client.new }
|
|
||||||
]
|
|
||||||
|
|
||||||
cache_stores.each do |store|
|
describe "with #{store.class}" do
|
||||||
store = Rack::Attack::StoreProxy.build(store)
|
before do
|
||||||
|
@cache = Rack::Attack::Cache.new
|
||||||
|
@key = "rack::attack:cache-test-key"
|
||||||
|
@expires_in = 1
|
||||||
|
@cache.store = store
|
||||||
|
delete(@key)
|
||||||
|
end
|
||||||
|
|
||||||
describe "with #{store.class}" do
|
after { delete(@key) }
|
||||||
before do
|
|
||||||
@cache = Rack::Attack::Cache.new
|
describe "do_count once" do
|
||||||
@key = "rack::attack:cache-test-key"
|
it "should be 1" do
|
||||||
@expires_in = 1
|
@cache.send(:do_count, @key, @expires_in).must_equal 1
|
||||||
@cache.store = store
|
|
||||||
delete(@key)
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
after { delete(@key) }
|
describe "do_count twice" do
|
||||||
|
it "must be 2" do
|
||||||
describe "do_count once" do
|
@cache.send(:do_count, @key, @expires_in)
|
||||||
it "should be 1" do
|
@cache.send(:do_count, @key, @expires_in).must_equal 2
|
||||||
@cache.send(:do_count, @key, @expires_in).must_equal 1
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "do_count twice" do
|
describe "do_count after expires_in" do
|
||||||
it "must be 2" do
|
it "must be 1" do
|
||||||
@cache.send(:do_count, @key, @expires_in)
|
@cache.send(:do_count, @key, @expires_in)
|
||||||
@cache.send(:do_count, @key, @expires_in).must_equal 2
|
sleep_until_expired
|
||||||
end
|
@cache.send(:do_count, @key, @expires_in).must_equal 1
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "do_count after expires_in" do
|
describe "write" do
|
||||||
it "must be 1" do
|
it "should write a value to the store with prefix" do
|
||||||
@cache.send(:do_count, @key, @expires_in)
|
@cache.write("cache-test-key", "foobar", 1)
|
||||||
sleep_until_expired
|
store.read(@key).must_equal "foobar"
|
||||||
@cache.send(:do_count, @key, @expires_in).must_equal 1
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "write" do
|
describe "write after expiry" do
|
||||||
it "should write a value to the store with prefix" do
|
it "must not have a value" do
|
||||||
@cache.write("cache-test-key", "foobar", 1)
|
@cache.write("cache-test-key", "foobar", @expires_in)
|
||||||
store.read(@key).must_equal "foobar"
|
sleep_until_expired
|
||||||
end
|
store.read(@key).must_be :nil?
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "write after expiry" do
|
describe "read" do
|
||||||
it "must not have a value" do
|
it "must read the value with a prefix" do
|
||||||
@cache.write("cache-test-key", "foobar", @expires_in)
|
store.write(@key, "foobar", :expires_in => @expires_in)
|
||||||
sleep_until_expired
|
@cache.read("cache-test-key").must_equal "foobar"
|
||||||
store.read(@key).must_be :nil?
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "read" do
|
describe "delete" do
|
||||||
it "must read the value with a prefix" do
|
it "must delete the value" do
|
||||||
store.write(@key, "foobar", :expires_in => @expires_in)
|
store.write(@key, "foobar", :expires_in => @expires_in)
|
||||||
@cache.read("cache-test-key").must_equal "foobar"
|
@cache.read('cache-test-key').must_equal "foobar"
|
||||||
end
|
store.delete(@key)
|
||||||
|
assert_nil @cache.read('cache-test-key')
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "delete" do
|
describe "cache#delete" do
|
||||||
it "must delete the value" do
|
it "must delete the value" do
|
||||||
store.write(@key, "foobar", :expires_in => @expires_in)
|
@cache.write("cache-test-key", "foobar", 1)
|
||||||
@cache.read('cache-test-key').must_equal "foobar"
|
store.read(@key).must_equal "foobar"
|
||||||
store.delete(@key)
|
@cache.delete('cache-test-key')
|
||||||
assert_nil @cache.read('cache-test-key')
|
store.read(@key).must_be :nil?
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "cache#delete" do
|
describe "reset_count" do
|
||||||
it "must delete the value" do
|
it "must delete the value" do
|
||||||
@cache.write("cache-test-key", "foobar", 1)
|
period = 1.minute
|
||||||
store.read(@key).must_equal "foobar"
|
unprefixed_key = 'cache-test-key'
|
||||||
@cache.delete('cache-test-key')
|
@cache.count(unprefixed_key, period)
|
||||||
store.read(@key).must_be :nil?
|
period_key, _ = @cache.send(:key_and_expiry, 'cache-test-key', period)
|
||||||
end
|
store.read(period_key).to_i.must_equal 1
|
||||||
end
|
@cache.reset_count(unprefixed_key, period)
|
||||||
|
assert_nil store.read(period_key)
|
||||||
describe "reset_count" do
|
|
||||||
it "must delete the value" do
|
|
||||||
period = 1.minute
|
|
||||||
unprefixed_key = 'cache-test-key'
|
|
||||||
@cache.count(unprefixed_key, period)
|
|
||||||
period_key, _ = @cache.send(:key_and_expiry, 'cache-test-key', period)
|
|
||||||
store.read(period_key).to_i.must_equal 1
|
|
||||||
@cache.reset_count(unprefixed_key, period)
|
|
||||||
assert_nil store.read(period_key)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue