Skip to content

Commit

Permalink
small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
luccastera committed Sep 8, 2009
1 parent 231d2b5 commit 0e49c9b
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 49 deletions.
4 changes: 2 additions & 2 deletions lib/handlers/active_support_cache_store_handler.rb
Expand Up @@ -6,7 +6,7 @@ def initialize(object=nil)
@cache = object @cache = object
end end


def increment(key) def increment(key)
@cache.write(key, (get(key)||0).to_i+1) @cache.write(key, (get(key)||0).to_i+1)
end end


Expand All @@ -18,4 +18,4 @@ def get(key)
Handlers.add_handler(self, "ActiveSupport::Cache::#{store}".downcase) Handlers.add_handler(self, "ActiveSupport::Cache::#{store}".downcase)
end end
end end
end end
2 changes: 1 addition & 1 deletion lib/handlers/redis_handler.rb
Expand Up @@ -12,4 +12,4 @@ def get(key)


Handlers.add_handler self Handlers.add_handler self
end end
end end
87 changes: 44 additions & 43 deletions test/test_api_throttling.rb
Expand Up @@ -27,27 +27,27 @@ class ApiThrottlingTest < Test::Unit::TestCase
include BasicTests include BasicTests


def app def app
app = Rack::Builder.new { Rack::Builder.new {
use ApiThrottling, :requests_per_hour => 3 use ApiThrottling, :requests_per_hour => 3
run lambda {|env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '12'}, ["Hello World!"] ] } run lambda {|env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '12'}, ["Hello World!"] ] }
} }
end 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 assert_equal "Handlers::RedisHandler", app.to_app.instance_variable_get(:@handler).to_s
end end


end end


context "without authentication required" do context "without authentication required" do
def app def app
app = Rack::Builder.new { Rack::Builder.new {
use ApiThrottling, :requests_per_hour => 3, :auth=>false use ApiThrottling, :requests_per_hour => 3, :auth => false
run lambda {|env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '12'}, ["Hello World!"] ] } run lambda {|env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '12'}, ["Hello World!"] ] }
} }
end end


def test_should_not_require_authorization should "not require authorization" do
3.times do 3.times do
get '/' get '/'
assert_equal 200, last_response.status assert_equal 200, last_response.status
Expand All @@ -59,14 +59,14 @@ def test_should_not_require_authorization


context "with rate limit key based on url" do context "with rate limit key based on url" do
def app def app
app = Rack::Builder.new { Rack::Builder.new {
use ApiThrottling, :requests_per_hour => 3, use ApiThrottling, :requests_per_hour => 3,
:key=>Proc.new{ |env,auth| "#{auth.username}_#{env['PATH_INFO']}_#{Time.now.strftime("%Y-%m-%d-%H")}" } :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!"] ] } run lambda {|env| [200, {'Content-Type' => 'text/plain', 'Content-Length' => '12'}, ["Hello World!"] ] }
} }
end 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" authorize "joe", "secret"
3.times do 3.times do
get '/' get '/'
Expand All @@ -92,48 +92,49 @@ def app
end end
end end


context "using active support cache store" do context "using active support memory store" do
require 'active_support' require 'active_support'


context "memory store" do include BasicTests
include BasicTests


before do before do
@@cache_store = ActiveSupport::Cache.lookup_store(:memory_store) @@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
end end


context "memcache store" do def app
include BasicTests 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!"] ] }
before do }
@@cache_store = ActiveSupport::Cache.lookup_store(:memCache_store) end
@@cache_store.clear
end should "have memcache as cache handler" do

assert_equal "Handlers::ActiveSupportCacheStoreHandler", app.to_app.instance_variable_get(:@handler).to_s
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
end end
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 end
2 changes: 0 additions & 2 deletions test/test_handlers.rb
@@ -1,5 +1,3 @@
require 'redis'
require 'memcache'
require File.expand_path(File.dirname(__FILE__) + '/test_helper') require File.expand_path(File.dirname(__FILE__) + '/test_helper')


class HandlersTest < Test::Unit::TestCase class HandlersTest < Test::Unit::TestCase
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -51,4 +51,4 @@ def test_should_require_authorization
assert_equal 401, last_response.status assert_equal 401, last_response.status
end end


end end

0 comments on commit 0e49c9b

Please sign in to comment.