From 0e49c9b7b9e68d8e3547a3a13b1eaafa9d45bf73 Mon Sep 17 00:00:00 2001 From: Luc Castera Date: Tue, 8 Sep 2009 11:04:12 -0400 Subject: [PATCH] small cleanup --- .../active_support_cache_store_handler.rb | 4 +- lib/handlers/redis_handler.rb | 2 +- test/test_api_throttling.rb | 87 ++++++++++--------- test/test_handlers.rb | 2 - test/test_helper.rb | 2 +- 5 files changed, 48 insertions(+), 49 deletions(-) diff --git a/lib/handlers/active_support_cache_store_handler.rb b/lib/handlers/active_support_cache_store_handler.rb index 8da4e95..487c9cd 100644 --- a/lib/handlers/active_support_cache_store_handler.rb +++ b/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 \ No newline at end of file +end diff --git a/lib/handlers/redis_handler.rb b/lib/handlers/redis_handler.rb index ba1e536..316141e 100644 --- a/lib/handlers/redis_handler.rb +++ b/lib/handlers/redis_handler.rb @@ -12,4 +12,4 @@ def get(key) Handlers.add_handler self end -end \ No newline at end of file +end diff --git a/test/test_api_throttling.rb b/test/test_api_throttling.rb index 65a1426..46b2f3b 100644 --- a/test/test_api_throttling.rb +++ b/test/test_api_throttling.rb @@ -27,13 +27,13 @@ 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 @@ -41,13 +41,13 @@ def test_cache_handler_should_be_redis 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 diff --git a/test/test_handlers.rb b/test/test_handlers.rb index 72c0e4c..1a66ef0 100644 --- a/test/test_handlers.rb +++ b/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 diff --git a/test/test_helper.rb b/test/test_helper.rb index b6dec44..d22366e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -51,4 +51,4 @@ def test_should_require_authorization assert_equal 401, last_response.status end -end \ No newline at end of file +end