1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

implement mock for S3::Files#get_url

This commit is contained in:
Simon Rozet 2010-04-21 01:04:38 +08:00 committed by Wesley Beary
parent d9f28880a1
commit b406ec113c
3 changed files with 29 additions and 12 deletions

View file

@ -108,9 +108,19 @@ module Fog
end
def get_object_url(bucket_name, object_name, expires)
raise MockNotImplemented.new("Contributions welcome!")
unless bucket_name
raise ArgumentError.new('bucket_name is required')
end
unless object_name
raise ArgumentError.new('object_name is required')
end
url({
:headers => {},
:host => "#{bucket_name}.#{@host}",
:method => 'GET',
:path => CGI.escape(object_name)
}, expires)
end
end
end
end

View file

@ -61,7 +61,19 @@ module Fog
Mock.reset_data(keys)
end
module Utils
def url(params, expires)
params[:headers]['Date'] = expires.to_i
query = [params[:query]].compact
query << "AWSAccessKeyId=#{@aws_access_key_id}"
query << "Signature=#{CGI.escape(signature(params))}"
query << "Expires=#{params[:headers]['Date']}"
"http://#{params[:host]}/#{params[:path]}?#{query.join('&')}"
end
end
class Mock
include Utils
def self.data
@data ||= Hash.new do |hash, key|
@ -82,9 +94,13 @@ module Fog
@data = self.class.data[@aws_access_key_id]
end
def signature(params)
"foo"
end
end
class Real
include Utils
# Initialize connection to S3
#
@ -174,16 +190,6 @@ DATA
hmac = @hmac.update(string_to_sign)
signature = Base64.encode64(hmac.digest).chomp!
end
def url(params, expires)
params[:headers]['Date'] = expires.to_i
query = [params[:query]].compact
query << "AWSAccessKeyId=#{@aws_access_key_id}"
query << "Signature=#{CGI.escape(signature(params))}"
query << "Expires=#{params[:headers]['Date']}"
"http://#{params[:host]}/#{params[:path]}?#{query.join('&')}"
end
end
end
end

View file

@ -83,6 +83,7 @@ describe 'Fog::AWS::S3::Files' do
data = File.open(File.dirname(__FILE__) + '/../../../lorem.txt', 'r')
file = @directory.files.create(:key => 'fogfilename', :body => data)
url = @directory.files.get_url('fogfilename', Time.now + 60 * 10)
url.should include("fogfilename", "Expires")
unless Fog.mocking?
open(url).read.should == File.open(File.dirname(__FILE__) + '/../../../lorem.txt', 'r').read
end