Skip to content

Commit

Permalink
tests for persistence and configurability
Browse files Browse the repository at this point in the history
  • Loading branch information
Swizec committed Mar 11, 2013
1 parent be6d3c5 commit 8ad2c97
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 48 deletions.
46 changes: 24 additions & 22 deletions lib/billy/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ def store(method, url, status, headers, content)

@cache[key(method, url)] = cached

puts "WRITING"
puts key(method, url)
if Billy.config.persist_cache
puts "PERSISTING"
Dir.mkdir(Billy.config.cache_path) unless File.exists?(Billy.config.cache_path)

begin
File.open("spec/cache/"+key(method, url)+".yml", 'w') {
|f| f.write(cached.to_yaml(:Encoding => :Utf8))
}
rescue StandardError => e
begin
File.open(Billy.config.cache_path+key(method, url)+".yml", 'w') {
|f| f.write(cached.to_yaml(:Encoding => :Utf8))
}
rescue StandardError => e
end
end
end

Expand All @@ -53,32 +55,32 @@ def reset
end

def load_dir
puts "LOADING DIR"
Dir.glob("spec/cache/*.yml") { |filename|
data = begin
YAML.load(File.open(filename))
rescue ArgumentError => e
puts "Could not parse YAML: #{e.message}"
end
if Billy.config.persist_cache
puts "LOADING DIR"
Dir.glob(Billy.config.cache_path+"*.yml") { |filename|
data = begin
YAML.load(File.open(filename))
rescue ArgumentError => e
puts "Could not parse YAML: #{e.message}"
end

puts key(data[:method], data[:url])
puts key(data[:method], data[:url])

@cache[key(data[:method], data[:url])] = data
}
puts "DONE LOADING"
@cache[key(data[:method], data[:url])] = data
}
puts "DONE LOADING"
end
end

def key(method, url)
url = URI(url)
no_params = url.scheme+'://'+url.host+url.path

if Billy.config.ignore_params.include?(no_params)
url = no_params
else
url = url.to_s
url = URI(no_params)
end

method+'_'+URI(url).host+'_'+Digest::SHA1.hexdigest(url)
method+'_'+url.host+'_'+Digest::SHA1.hexdigest(url.to_s)
end
end
end
2 changes: 1 addition & 1 deletion lib/billy/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize
@whitelist = DEFAULT_WHITELIST
@ignore_params = []
@persist_cache = false
@cache_path = ''
@cache_path = '/tmp'
end
end

Expand Down
5 changes: 5 additions & 0 deletions lib/billy/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def reset_cache
@cache.reset
end

def restore_cache
@cache.reset
@cache.load_dir
end

protected

def find_stub(method, url)
Expand Down
71 changes: 46 additions & 25 deletions spec/requests/proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,41 +106,62 @@
end
end

context "persisting cache" do
around do |example|
Billy.configure { |c|
c.persist_cache = true
c.cache_path = '/tmp/cache'
c.ignore_params = []
}
example.run
Billy.configure { |c|
c.persist_cache = false
c.cache_path = ''
}
end

context "cache persistence" do
def key(method, url)
url = proxy.url+url

url = URI(url)
no_params = 'http://localhost'+url.path
no_params = url.scheme+'://'+url.host+url.path

if Billy.config.ignore_params.include?(no_params)
url = no_params
else
url = url.to_s
url = URI(no_params)
end

method+'_localhost_'+Digest::SHA1.hexdigest(url)
method+'_'+url.host+'_'+Digest::SHA1.hexdigest(url.to_s)
end

it 'should persist' do
r = http.get('/foo')
r.body.should == 'GET /foo'
context "enabled" do
around do |example|
Billy.configure { |c|
c.persist_cache = true
c.cache_path = '/tmp/cache'
c.ignore_params = []
}
example.run
Billy.configure { |c|
c.persist_cache = false
c.cache_path = ''
}
end

puts 'KEY'
puts key('get', '/foo')
it 'should persist' do
r = http.get('/foo')
r.body.should == 'GET /foo'

#puts Billy::Cache.any_instance.key('GET', '/foo')
File.exists?('/tmp/cache'+key('GET', '/foo')).should be_true
end
end

context "disabled" do
around do |example|
Billy.configure { |c|
c.persist_cache = false
c.cache_path = '/tmp/cache'
c.ignore_params = []
}
example.run
Billy.configure { |c|
c.persist_cache = false
c.cache_path = ''
}
end

it 'shouldnt persist' do
r = http.get('/foo')
r.body.should == 'GET /foo'

File.exists?('/tmp/cache'+key('GET', '/foo')).should be_false
end
end
end
end
Expand Down

0 comments on commit 8ad2c97

Please sign in to comment.