Skip to content

Commit

Permalink
less global test vars and some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Jan 27, 2016
1 parent 3acecee commit 1838a52
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 28 deletions.
10 changes: 5 additions & 5 deletions test/cache_test.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require_relative 'test_helper'

def dumb_app(env)
body = block_given? ? [yield] : ['Hi']
[ 200, {'Content-Type' => 'text/plain'}, body ]
end

describe Rack::Cache do
def dumb_app(_env)
body = block_given? ? [yield] : ['Hi']
[ 200, {'Content-Type' => 'text/plain'}, body ]
end

before { @app = method(:dumb_app) }

it 'takes a backend and returns a middleware component' do
Expand Down
2 changes: 1 addition & 1 deletion test/cachecontrol_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
require 'rack/cache/cachecontrol'
require 'rack/cache/metastore'

describe 'Rack::Cache::CacheControl' do
describe Rack::Cache::CacheControl do
it 'takes no args and initializes with an empty set of values' do
cache_control = Rack::Cache::CacheControl.new
assert cache_control.empty?
Expand Down
2 changes: 1 addition & 1 deletion test/context_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require_relative 'test_helper'
require 'rack/cache/context'

describe 'Rack::Cache::Context' do
describe Rack::Cache::Context do
before { setup_cache_context }
after { teardown_cache_context }

Expand Down
27 changes: 15 additions & 12 deletions test/entitystore_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@
require 'rack/cache/entitystore'
require 'rack/cache/metastore'

class Object
def sha_like?
length == 40 && self =~ /^[0-9a-z]+$/
end
end

module RackCacheEntityStoreImplementation
def self.included(base)
base.class_eval do
Expand All @@ -20,17 +14,15 @@ def self.included(base)

it 'stores bodies with #write' do
key, size = @store.write(['My wild love went riding,'])
refute key.nil?
assert key.sha_like?
assert_sha_like key

data = @store.read(key)
data.must_equal 'My wild love went riding,'
end

it 'takes a ttl parameter for #write' do
key, size = @store.write(['My wild love went riding,'], 0)
refute key.nil?
assert key.sha_like?
assert_sha_like key

data = @store.read(key)
data.must_equal 'My wild love went riding,'
Expand Down Expand Up @@ -96,15 +88,23 @@ def self.included(base)
end
end

describe 'Rack::Cache::EntityStore' do
describe Rack::Cache::EntityStore do
def assert_sha_like(object)
assert object
object.length.must_equal 40
object.must_match /^[0-9a-z]+$/
end

describe 'Heap' do
before { @store = Rack::Cache::EntityStore::Heap.new }

include RackCacheEntityStoreImplementation

it 'takes a Hash to ::new' do
@store = Rack::Cache::EntityStore::Heap.new('foo' => ['bar'])
@store.read('foo').must_equal 'bar'
end

it 'uses its own Hash with no args to ::new' do
@store.read('foo').must_equal nil
end
Expand All @@ -115,6 +115,7 @@ def self.included(base)
@temp_dir = create_temp_directory
@store = Rack::Cache::EntityStore::Disk.new(@temp_dir)
end

after do
@store = nil
remove_entry_secure @temp_dir
Expand All @@ -126,15 +127,17 @@ def self.included(base)
@store = Rack::Cache::EntityStore::Disk.new(path)
assert File.directory? path
end

it 'produces a body that responds to #to_path' do
key, size = @store.write(['Some shells for her hair.'])
body = @store.open(key)
assert body.respond_to? :to_path
path = "#{@temp_dir}/#{key[0..1]}/#{key[2..-1]}"
body.to_path.must_equal path
end

it 'spreads data over a 36² hash radius' do
(<<-PROSE).each_line { |line| assert @store.write([line]).first.sha_like? }
(<<-PROSE).each_line { |line| assert_sha_like @store.write([line]).first }
My wild love went riding,
She rode all the day;
She rode to the devil,
Expand Down
4 changes: 1 addition & 3 deletions test/key_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
require_relative 'test_helper'
require 'rack/cache/key'

describe 'A Rack::Cache::Key' do
# Helper Methods =============================================================

describe Rack::Cache::Key do
def mock_request(*args)
uri, opts = args
env = Rack::MockRequest.env_for(uri, opts || {})
Expand Down
4 changes: 3 additions & 1 deletion test/options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@ def initialize
end
end

describe 'Rack::Cache::Options' do
describe Rack::Cache::Options do
before { @options = MockOptions.new }

describe '#set' do
it 'sets a Symbol option as rack-cache.symbol' do
@options.set :bar, 'baz'
@options.options['rack-cache.bar'].must_equal 'baz'
end

it 'sets a String option as string' do
@options.set 'foo.bar', 'bling'
@options.options['foo.bar'].must_equal 'bling'
end

it 'sets all key/value pairs when given a Hash' do
@options.set :foo => 'bar', :bar => 'baz', 'foo.bar' => 'bling'
@options.foo.must_equal 'bar'
Expand Down
2 changes: 1 addition & 1 deletion test/request_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require_relative 'test_helper'
require 'rack/cache/request'

describe 'Rack::Cache::Request' do
describe Rack::Cache::Request do
it 'is marked as no_cache when the Cache-Control header includes the no-cache directive' do
request = Rack::Cache::Request.new('HTTP_CACHE_CONTROL' => 'public, no-cache')
assert request.no_cache?
Expand Down
2 changes: 1 addition & 1 deletion test/response_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative 'test_helper'

describe 'Rack::Cache::Response' do
describe Rack::Cache::Response do
before do
@now = Time.httpdate(Time.now.httpdate)
@one_hour_ago = Time.httpdate((Time.now - (60**2)).httpdate)
Expand Down
13 changes: 10 additions & 3 deletions test/storage_test.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
require_relative 'test_helper'
require 'rack/cache/storage'

describe 'Rack::Cache::Storage' do
describe Rack::Cache::Storage do
before do
@storage = Rack::Cache::Storage.new
end

it "fails when an unknown URI scheme is provided" do
lambda { @storage.resolve_metastore_uri('foo:/') }.must_raise
end

it "creates a new MetaStore for URI if none exists" do
@storage.resolve_metastore_uri('heap:/').
must_be_kind_of Rack::Cache::MetaStore
end

it "returns an existing MetaStore instance for URI that exists" do
store = @storage.resolve_metastore_uri('heap:/')
@storage.resolve_metastore_uri('heap:/').must_equal store
end

it "creates a new EntityStore for URI if none exists" do
@storage.resolve_entitystore_uri('heap:/').
must_be_kind_of Rack::Cache::EntityStore
end

it "returns an existing EntityStore instance for URI that exists" do
store = @storage.resolve_entitystore_uri('heap:/')
@storage.resolve_entitystore_uri('heap:/').must_equal store
end

it "clears all URI -> store mappings with #clear" do
meta = @storage.resolve_metastore_uri('heap:/')
entity = @storage.resolve_entitystore_uri('heap:/')
Expand All @@ -50,6 +55,7 @@
before do
@temp_dir = create_temp_directory
end

after do
remove_entry_secure @temp_dir
@temp_dir = nil
Expand All @@ -60,6 +66,7 @@
@storage.resolve_metastore_uri(uri + @temp_dir).
must_be_kind_of Rack::Cache::MetaStore
end

it "resolves #{uri} entity store URIs" do
@storage.resolve_entitystore_uri(uri + @temp_dir).
must_be_kind_of Rack::Cache::EntityStore
Expand All @@ -76,19 +83,19 @@
@storage.resolve_metastore_uri(uri).
must_be_kind_of Rack::Cache::MetaStore
end

it "resolves #{scheme} entity store URIs" do
uri = scheme + '//' + ENV['MEMCACHED']
@storage.resolve_entitystore_uri(uri).
must_be_kind_of Rack::Cache::EntityStore
end
end

it 'supports namespaces in memcached: URIs' do
uri = "memcached://" + ENV['MEMCACHED'] + "/namespace"
@storage.resolve_metastore_uri(uri).
must_be_kind_of Rack::Cache::MetaStore
end
end

end

end

0 comments on commit 1838a52

Please sign in to comment.