rack-attack 5.3.1 → 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/Rakefile +3 -2
- data/lib/rack/attack.rb +23 -22
- data/lib/rack/attack/cache.rb +4 -3
- data/lib/rack/attack/check.rb +6 -8
- data/lib/rack/attack/store_proxy.rb +1 -1
- data/lib/rack/attack/store_proxy/redis_cache_store_proxy.rb +2 -2
- data/lib/rack/attack/store_proxy/redis_proxy.rb +54 -0
- data/lib/rack/attack/store_proxy/redis_store_proxy.rb +1 -22
- data/lib/rack/attack/throttle.rb +14 -11
- data/lib/rack/attack/track.rb +3 -3
- data/lib/rack/attack/version.rb +1 -1
- data/spec/acceptance/stores/active_support_dalli_store_spec.rb +41 -0
- data/spec/acceptance/stores/active_support_mem_cache_store_spec.rb +40 -0
- data/spec/acceptance/stores/{mem_cache_store_spec.rb → active_support_memory_store_spec.rb} +5 -5
- data/spec/acceptance/stores/{redis_cache_store_pooled_spec.rb → active_support_redis_cache_store_pooled_spec.rb} +4 -4
- data/spec/acceptance/stores/{redis_cache_store_spec.rb → active_support_redis_cache_store_spec.rb} +4 -4
- data/spec/acceptance/stores/active_support_redis_store_spec.rb +40 -0
- data/spec/acceptance/stores/connection_pool_dalli_client_spec.rb +42 -0
- data/spec/acceptance/stores/dalli_client_spec.rb +41 -0
- data/spec/acceptance/stores/redis_spec.rb +42 -0
- data/spec/acceptance/stores/redis_store_spec.rb +40 -0
- data/spec/integration/offline_spec.rb +21 -19
- data/spec/rack_attack_throttle_spec.rb +4 -4
- data/spec/rack_attack_track_spec.rb +4 -4
- data/spec/spec_helper.rb +15 -9
- metadata +84 -146
- data/spec/integration/rack_attack_cache_spec.rb +0 -124
@@ -1,124 +0,0 @@
|
|
1
|
-
require_relative '../spec_helper'
|
2
|
-
|
3
|
-
describe Rack::Attack::Cache do
|
4
|
-
# A convenience method for deleting a key from cache.
|
5
|
-
# Slightly different than @cache.delete, which adds a prefix.
|
6
|
-
def delete(key)
|
7
|
-
if @cache.store.respond_to?(:delete)
|
8
|
-
@cache.store.delete(key)
|
9
|
-
else
|
10
|
-
@cache.store.del(key)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def sleep_until_expired
|
15
|
-
sleep(@expires_in * 1.1) # Add 10% to reduce errors
|
16
|
-
end
|
17
|
-
|
18
|
-
require 'active_support/cache/dalli_store'
|
19
|
-
require 'active_support/cache/mem_cache_store'
|
20
|
-
require 'active_support/cache/redis_store'
|
21
|
-
require 'active_support/cache/redis_cache_store' if ActiveSupport.version.to_s.to_f >= 5.2
|
22
|
-
require 'connection_pool'
|
23
|
-
|
24
|
-
cache_stores = [
|
25
|
-
ActiveSupport::Cache::MemoryStore.new,
|
26
|
-
ActiveSupport::Cache::DalliStore.new("127.0.0.1"),
|
27
|
-
ActiveSupport::Cache::RedisStore.new("127.0.0.1"),
|
28
|
-
ActiveSupport::Cache::MemCacheStore.new("127.0.0.1"),
|
29
|
-
Dalli::Client.new,
|
30
|
-
ConnectionPool.new { Dalli::Client.new },
|
31
|
-
Redis::Store.new
|
32
|
-
]
|
33
|
-
|
34
|
-
cache_stores << ActiveSupport::Cache::RedisCacheStore.new if defined?(ActiveSupport::Cache::RedisCacheStore)
|
35
|
-
|
36
|
-
cache_stores.each do |store|
|
37
|
-
store = Rack::Attack::StoreProxy.build(store)
|
38
|
-
|
39
|
-
describe "with #{store.class}" do
|
40
|
-
before {
|
41
|
-
@cache = Rack::Attack::Cache.new
|
42
|
-
@key = "rack::attack:cache-test-key"
|
43
|
-
@expires_in = 1
|
44
|
-
@cache.store = store
|
45
|
-
delete(@key)
|
46
|
-
}
|
47
|
-
|
48
|
-
after { delete(@key) }
|
49
|
-
|
50
|
-
describe "do_count once" do
|
51
|
-
it "should be 1" do
|
52
|
-
@cache.send(:do_count, @key, @expires_in).must_equal 1
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
describe "do_count twice" do
|
57
|
-
it "must be 2" do
|
58
|
-
@cache.send(:do_count, @key, @expires_in)
|
59
|
-
@cache.send(:do_count, @key, @expires_in).must_equal 2
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe "do_count after expires_in" do
|
64
|
-
it "must be 1" do
|
65
|
-
@cache.send(:do_count, @key, @expires_in)
|
66
|
-
sleep_until_expired
|
67
|
-
@cache.send(:do_count, @key, @expires_in).must_equal 1
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe "write" do
|
72
|
-
it "should write a value to the store with prefix" do
|
73
|
-
@cache.write("cache-test-key", "foobar", 1)
|
74
|
-
store.read(@key).must_equal "foobar"
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
describe "write after expiry" do
|
79
|
-
it "must not have a value" do
|
80
|
-
@cache.write("cache-test-key", "foobar", @expires_in)
|
81
|
-
sleep_until_expired
|
82
|
-
store.read(@key).must_be :nil?
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
describe "read" do
|
87
|
-
it "must read the value with a prefix" do
|
88
|
-
store.write(@key, "foobar", :expires_in => @expires_in)
|
89
|
-
@cache.read("cache-test-key").must_equal "foobar"
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
describe "delete" do
|
94
|
-
it "must delete the value" do
|
95
|
-
store.write(@key, "foobar", :expires_in => @expires_in)
|
96
|
-
@cache.read('cache-test-key').must_equal "foobar"
|
97
|
-
store.delete(@key)
|
98
|
-
assert_nil @cache.read('cache-test-key')
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
describe "cache#delete" do
|
103
|
-
it "must delete the value" do
|
104
|
-
@cache.write("cache-test-key", "foobar", 1)
|
105
|
-
store.read(@key).must_equal "foobar"
|
106
|
-
@cache.delete('cache-test-key')
|
107
|
-
store.read(@key).must_be :nil?
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
describe "reset_count" do
|
112
|
-
it "must delete the value" do
|
113
|
-
period = 1.minute
|
114
|
-
unprefixed_key = 'cache-test-key'
|
115
|
-
@cache.count(unprefixed_key, period)
|
116
|
-
period_key, _ = @cache.send(:key_and_expiry, 'cache-test-key', period)
|
117
|
-
store.read(period_key).to_i.must_equal 1
|
118
|
-
@cache.reset_count(unprefixed_key, period)
|
119
|
-
assert_nil store.read(period_key)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|