mirror of
https://github.com/samsonjs/rack-attack.git
synced 2026-04-27 15:07:41 +00:00
Merge branch 'integration'
This commit is contained in:
commit
95264f9d19
5 changed files with 130 additions and 116 deletions
|
|
@ -6,8 +6,13 @@ rvm:
|
||||||
- 1.9.3
|
- 1.9.3
|
||||||
- 2.0.0
|
- 2.0.0
|
||||||
- 2.1.0
|
- 2.1.0
|
||||||
|
- 2.1.1
|
||||||
- jruby-19mode
|
- jruby-19mode
|
||||||
|
|
||||||
gemfile:
|
gemfile:
|
||||||
- gemfiles/activesupport3.2
|
- gemfiles/activesupport3.2
|
||||||
- gemfiles/activesupport4.0
|
- gemfiles/activesupport4.0
|
||||||
|
|
||||||
|
services:
|
||||||
|
- redis
|
||||||
|
- memcached
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
# Changlog
|
# Changlog
|
||||||
|
|
||||||
## Master
|
## v3.0.0 - 15 March 2014
|
||||||
* Change default blacklisted response to 403 Forbidden (thanks @carpodaster).
|
* Change default blacklisted response to 403 Forbidden (thanks @carpodaster).
|
||||||
* Fail gracefully when Redis store is not available; rescue exeption and don't
|
* Fail gracefully when Redis store is not available; rescue exeption and don't
|
||||||
throttle request. (thanks @wkimeria)
|
throttle request. (thanks @wkimeria)
|
||||||
|
* TravisCI runs integration tests.
|
||||||
|
|
||||||
## v2.3.0 - 11 October 2013 (master)
|
## v2.3.0 - 11 October 2013
|
||||||
* Allow throttle `limit` argument to be a proc. (thanks @lunks)
|
* Allow throttle `limit` argument to be a proc. (thanks @lunks)
|
||||||
* Add Allow2Ban, complement of Fail2Ban. (thanks @jormon)
|
* Add Allow2Ban, complement of Fail2Ban. (thanks @jormon)
|
||||||
* Improved TravisCI testing
|
* Improved TravisCI testing
|
||||||
|
|
|
||||||
13
Rakefile
13
Rakefile
|
|
@ -2,8 +2,17 @@ require "rubygems"
|
||||||
require "bundler/setup"
|
require "bundler/setup"
|
||||||
require 'rake/testtask'
|
require 'rake/testtask'
|
||||||
|
|
||||||
Rake::TestTask.new do |t|
|
namespace :test do
|
||||||
t.pattern = "spec/*_spec.rb"
|
Rake::TestTask.new(:units) do |t|
|
||||||
|
t.pattern = "spec/*_spec.rb"
|
||||||
|
end
|
||||||
|
|
||||||
|
Rake::TestTask.new(:integration) do |t|
|
||||||
|
t.pattern = "spec/integration/*_spec.rb"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
desc 'Run tests'
|
||||||
|
task :test => %w[test:units test:integration]
|
||||||
|
|
||||||
task :default => :test
|
task :default => :test
|
||||||
|
|
|
||||||
111
spec/integration/rack_attack_cache_spec.rb
Normal file
111
spec/integration/rack_attack_cache_spec.rb
Normal file
|
|
@ -0,0 +1,111 @@
|
||||||
|
require_relative '../spec_helper'
|
||||||
|
|
||||||
|
describe Rack::Attack::Cache do
|
||||||
|
def delete(key)
|
||||||
|
if @cache.store.respond_to?(:delete)
|
||||||
|
@cache.store.delete(key)
|
||||||
|
else
|
||||||
|
@cache.store.del(key)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def sleep_until_expired
|
||||||
|
sleep(@expires_in * 1.1) # Add 10% to reduce errors
|
||||||
|
end
|
||||||
|
|
||||||
|
require 'active_support/cache/dalli_store'
|
||||||
|
require 'active_support/cache/redis_store'
|
||||||
|
cache_stores = [
|
||||||
|
ActiveSupport::Cache::MemoryStore.new,
|
||||||
|
ActiveSupport::Cache::DalliStore.new("localhost"),
|
||||||
|
ActiveSupport::Cache::RedisStore.new("localhost"),
|
||||||
|
Redis::Store.new
|
||||||
|
]
|
||||||
|
|
||||||
|
cache_stores.each do |store|
|
||||||
|
store = Rack::Attack::StoreProxy.build(store)
|
||||||
|
describe "with #{store.class}" do
|
||||||
|
|
||||||
|
before {
|
||||||
|
@cache ||= Rack::Attack::Cache.new
|
||||||
|
@key = "rack::attack:cache-test-key"
|
||||||
|
@expires_in = 1
|
||||||
|
@cache.store = store
|
||||||
|
delete(@key)
|
||||||
|
}
|
||||||
|
|
||||||
|
after { delete(@key) }
|
||||||
|
|
||||||
|
describe "do_count once" do
|
||||||
|
it "should be 1" do
|
||||||
|
@cache.send(:do_count, @key, @expires_in).must_equal 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "do_count twice" do
|
||||||
|
it "must be 2" do
|
||||||
|
@cache.send(:do_count, @key, @expires_in)
|
||||||
|
@cache.send(:do_count, @key, @expires_in).must_equal 2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
describe "do_count after expires_in" do
|
||||||
|
it "must be 1" do
|
||||||
|
@cache.send(:do_count, @key, @expires_in)
|
||||||
|
sleep_until_expired
|
||||||
|
@cache.send(:do_count, @key, @expires_in).must_equal 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "write" do
|
||||||
|
it "should write a value to the store with prefix" do
|
||||||
|
@cache.write("cache-test-key", "foobar", 1)
|
||||||
|
store.read(@key).must_equal "foobar"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "write after expiry" do
|
||||||
|
it "must not have a value" do
|
||||||
|
@cache.write("cache-test-key", "foobar", @expires_in)
|
||||||
|
sleep_until_expired
|
||||||
|
store.read(@key).must_be :nil?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "read" do
|
||||||
|
it "must read the value with a prefix" do
|
||||||
|
store.write(@key, "foobar", :expires_in => @expires_in)
|
||||||
|
@cache.read("cache-test-key").must_equal "foobar"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "should not error if redis is not running" do
|
||||||
|
before {
|
||||||
|
@cache = Rack::Attack::Cache.new
|
||||||
|
@key = "rack::attack:cache-test-key"
|
||||||
|
@expires_in = 1
|
||||||
|
# Use presumably unused port for Redis client
|
||||||
|
@cache.store = ActiveSupport::Cache::RedisStore.new(:host => '127.0.0.1', :port => 3333)
|
||||||
|
}
|
||||||
|
describe "write" do
|
||||||
|
it "should not raise exception" do
|
||||||
|
@cache.write("cache-test-key", "foobar", 1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "read" do
|
||||||
|
it "should not raise exception" do
|
||||||
|
@cache.read("cache-test-key")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "do_count" do
|
||||||
|
it "should not raise exception" do
|
||||||
|
@cache.send(:do_count, @key, @expires_in)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
@ -1,112 +0,0 @@
|
||||||
require_relative 'spec_helper'
|
|
||||||
|
|
||||||
if ENV['TEST_INTEGRATION']
|
|
||||||
describe Rack::Attack::Cache do
|
|
||||||
def delete(key)
|
|
||||||
if @cache.store.respond_to?(:delete)
|
|
||||||
@cache.store.delete(key)
|
|
||||||
else
|
|
||||||
@cache.store.del(key)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
require 'active_support/cache/dalli_store'
|
|
||||||
require 'active_support/cache/redis_store'
|
|
||||||
cache_stores = [
|
|
||||||
ActiveSupport::Cache::MemoryStore.new,
|
|
||||||
ActiveSupport::Cache::DalliStore.new("localhost"),
|
|
||||||
ActiveSupport::Cache::RedisStore.new("localhost"),
|
|
||||||
Redis::Store.new
|
|
||||||
]
|
|
||||||
|
|
||||||
cache_stores.each do |store|
|
|
||||||
store = Rack::Attack::StoreProxy.build(store)
|
|
||||||
describe "with #{store.class}" do
|
|
||||||
|
|
||||||
before {
|
|
||||||
@cache ||= Rack::Attack::Cache.new
|
|
||||||
@key = "rack::attack:cache-test-key"
|
|
||||||
@expires_in = 1
|
|
||||||
@cache.store = store
|
|
||||||
delete(@key)
|
|
||||||
}
|
|
||||||
|
|
||||||
after { delete(@key) }
|
|
||||||
|
|
||||||
describe "do_count once" do
|
|
||||||
it "should be 1" do
|
|
||||||
@cache.send(:do_count, @key, @expires_in).must_equal 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "do_count twice" do
|
|
||||||
it "must be 2" do
|
|
||||||
@cache.send(:do_count, @key, @expires_in)
|
|
||||||
@cache.send(:do_count, @key, @expires_in).must_equal 2
|
|
||||||
end
|
|
||||||
end
|
|
||||||
describe "do_count after expires_in" do
|
|
||||||
it "must be 1" do
|
|
||||||
@cache.send(:do_count, @key, @expires_in)
|
|
||||||
sleep @expires_in # sigh
|
|
||||||
@cache.send(:do_count, @key, @expires_in).must_equal 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "write" do
|
|
||||||
it "should write a value to the store with prefix" do
|
|
||||||
@cache.write("cache-test-key", "foobar", 1)
|
|
||||||
store.read(@key).must_equal "foobar"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "write after expiry" do
|
|
||||||
it "must not have a value" do
|
|
||||||
@cache.write("cache-test-key", "foobar", @expires_in)
|
|
||||||
sleep @expires_in # tick... tick... tick...
|
|
||||||
store.read(@key).must_be :nil?
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "read" do
|
|
||||||
it "must read the value with a prefix" do
|
|
||||||
store.write(@key, "foobar", :expires_in => @expires_in)
|
|
||||||
@cache.read("cache-test-key").must_equal "foobar"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "should not error if redis is not running" do
|
|
||||||
before {
|
|
||||||
@cache = Rack::Attack::Cache.new
|
|
||||||
@key = "rack::attack:cache-test-key"
|
|
||||||
@expires_in = 1
|
|
||||||
# Use ip reserved for documentation to ensure it does not exist
|
|
||||||
# http://tools.ietf.org/html/rfc5737
|
|
||||||
@cache.store = ActiveSupport::Cache::RedisStore.new(:host => '203.0.113.0', :port => 3333)
|
|
||||||
}
|
|
||||||
describe "write" do
|
|
||||||
it "should not raise exception" do
|
|
||||||
@cache.write("cache-test-key", "foobar", 1)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "read" do
|
|
||||||
it "should not raise exception" do
|
|
||||||
@cache.read("cache-test-key")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "do_count" do
|
|
||||||
it "should not raise exception" do
|
|
||||||
@cache.send(:do_count, @key, @expires_in)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
else
|
|
||||||
puts 'Skipping cache store integration tests (set ENV["TEST_INTEGRATION"] to enable)'
|
|
||||||
end
|
|
||||||
Loading…
Reference in a new issue