Permalink
Browse files

small cleanup

  • Loading branch information...
1 parent 231d2b5 commit 0e49c9b7b9e68d8e3547a3a13b1eaafa9d45bf73 @luccastera committed Sep 8, 2009
View
4 lib/handlers/active_support_cache_store_handler.rb
@@ -6,7 +6,7 @@ def initialize(object=nil)
@cache = object
end
- def increment(key)
+ def increment(key)
@cache.write(key, (get(key)||0).to_i+1)
end
@@ -18,4 +18,4 @@ def get(key)
Handlers.add_handler(self, "ActiveSupport::Cache::#{store}".downcase)
end
end
-end
+end
View
2 lib/handlers/redis_handler.rb
@@ -12,4 +12,4 @@ def get(key)
Handlers.add_handler self
end
-end
+end
View
87 test/test_api_throttling.rb
@@ -27,27 +27,27 @@ class ApiThrottlingTest < Test::Unit::TestCase
include BasicTests
def app
- app = Rack::Builder.new {
+ Rack::Builder.new {
use ApiThrottling, :requests_per_hour => 3
run lambda {|env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '12'}, ["Hello World!"] ] }
}
end
- def test_cache_handler_should_be_redis
+ should "have Redis as cache handler" do
assert_equal "Handlers::RedisHandler", app.to_app.instance_variable_get(:@handler).to_s
end
end
context "without authentication required" do
def app
- app = Rack::Builder.new {
- use ApiThrottling, :requests_per_hour => 3, :auth=>false
+ Rack::Builder.new {
+ use ApiThrottling, :requests_per_hour => 3, :auth => false
run lambda {|env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '12'}, ["Hello World!"] ] }
}
end
- def test_should_not_require_authorization
+ should "not require authorization" do
3.times do
get '/'
assert_equal 200, last_response.status
@@ -59,14 +59,14 @@ def test_should_not_require_authorization
context "with rate limit key based on url" do
def app
- app = Rack::Builder.new {
+ Rack::Builder.new {
use ApiThrottling, :requests_per_hour => 3,
:key=>Proc.new{ |env,auth| "#{auth.username}_#{env['PATH_INFO']}_#{Time.now.strftime("%Y-%m-%d-%H")}" }
run lambda {|env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '12'}, ["Hello World!"] ] }
}
end
- test "should throttle requests based on the user and url called" do
+ should "throttle requests based on the user and url called" do
authorize "joe", "secret"
3.times do
get '/'
@@ -92,48 +92,49 @@ def app
end
end
- context "using active support cache store" do
+ context "using active support memory store" do
require 'active_support'
- context "memory store" do
- include BasicTests
+ include BasicTests
- before do
- @@cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
- end
-
- def app
- app = Rack::Builder.new {
- use ApiThrottling, :requests_per_hour => 3, :cache=>@@cache_store
- run lambda {|env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '12'}, ["Hello World!"] ] }
- }
- end
-
- def test_cache_handler_should_be_memcache
- assert_equal "Handlers::ActiveSupportCacheStoreHandler", app.to_app.instance_variable_get(:@handler).to_s
- end
+ before do
+ @@cache_store = ActiveSupport::Cache.lookup_store(:memory_store)
end
- context "memcache store" do
- include BasicTests
-
-
- before do
- @@cache_store = ActiveSupport::Cache.lookup_store(:memCache_store)
- @@cache_store.clear
- end
-
- def app
- app = Rack::Builder.new {
- use ApiThrottling, :requests_per_hour => 3, :cache=>@@cache_store
- run lambda {|env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '12'}, ["Hello World!"] ] }
- }
- end
-
- def test_cache_handler_should_be_memcache
- assert_equal "Handlers::ActiveSupportCacheStoreHandler", app.to_app.instance_variable_get(:@handler).to_s
- end
+ def app
+ Rack::Builder.new {
+ use ApiThrottling, :requests_per_hour => 3, :cache => @@cache_store
+ run lambda {|env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '12'}, ["Hello World!"] ] }
+ }
+ end
+
+ should "have memcache as cache handler" do
+ assert_equal "Handlers::ActiveSupportCacheStoreHandler", app.to_app.instance_variable_get(:@handler).to_s
end
end
+
+
+ context "using active support memcache store" do
+ require 'active_support'
+
+ #include BasicTests
+
+ before do
+ @@cache_store = ActiveSupport::Cache.lookup_store(:memCache_store)
+ @@cache_store.clear
+ end
+
+ def app
+ Rack::Builder.new {
+ use ApiThrottling, :requests_per_hour => 3, :cache => @@cache_store
+ run lambda {|env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '12'}, ["Hello World!"] ] }
+ }
+ end
+
+ should "have memcache as cache handler" do
+ assert_equal "Handlers::ActiveSupportCacheStoreHandler", app.to_app.instance_variable_get(:@handler).to_s
+ end
+ end
+
end
View
2 test/test_handlers.rb
@@ -1,5 +1,3 @@
-require 'redis'
-require 'memcache'
require File.expand_path(File.dirname(__FILE__) + '/test_helper')
class HandlersTest < Test::Unit::TestCase
View
2 test/test_helper.rb
@@ -51,4 +51,4 @@ def test_should_require_authorization
assert_equal 401, last_response.status
end
-end
+end

0 comments on commit 0e49c9b

Please sign in to comment.