mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
support options in s3.get_bucket mocks
This commit is contained in:
parent
962169950a
commit
bb7c79295e
2 changed files with 68 additions and 15 deletions
|
@ -59,12 +59,16 @@ else
|
||||||
module AWS
|
module AWS
|
||||||
class S3
|
class S3
|
||||||
|
|
||||||
|
# FIXME: implement delimiter
|
||||||
def get_bucket(bucket_name, options = {})
|
def get_bucket(bucket_name, options = {})
|
||||||
response = Fog::Response.new
|
response = Fog::Response.new
|
||||||
if bucket = Fog::AWS::S3.data[:buckets][bucket_name]
|
if bucket = Fog::AWS::S3.data[:buckets][bucket_name]
|
||||||
response.status = 200
|
response.status = 200
|
||||||
response.body = {
|
response.body = {
|
||||||
'Contents' => bucket[:objects].values.map do |object|
|
'Contents' => bucket[:objects].values.sort {|x,y| x['Key'] <=> y['Key']}.reject do |object|
|
||||||
|
(options['prefix'] && object['Key'][0...options['prefix'].length] != options['prefix']) ||
|
||||||
|
(options['marker'] && object['Key'] <= options['marker'])
|
||||||
|
end.map do |object|
|
||||||
data = object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Owner', 'Size', 'StorageClass'].include?(key)}
|
data = object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Owner', 'Size', 'StorageClass'].include?(key)}
|
||||||
data.merge!({
|
data.merge!({
|
||||||
'LastModified' => Time.parse(data['LastModified']),
|
'LastModified' => Time.parse(data['LastModified']),
|
||||||
|
@ -73,11 +77,15 @@ else
|
||||||
data
|
data
|
||||||
end,
|
end,
|
||||||
'IsTruncated' => false,
|
'IsTruncated' => false,
|
||||||
'Marker' => options['Marker'] || '',
|
'Marker' => options['marker'] || '',
|
||||||
'MaxKeys' => options['MaxKeys'] || 1000,
|
'MaxKeys' => options['max-keys'] || 1000,
|
||||||
'Name' => bucket['Name'],
|
'Name' => bucket['Name'],
|
||||||
'Prefix' => options['Prefix'] || ''
|
'Prefix' => options['prefix'] || ''
|
||||||
}
|
}
|
||||||
|
if options['max-keys'] && options['max-keys'] < response.body['Contents'].length
|
||||||
|
response.body['IsTruncated'] = true
|
||||||
|
response.body['Contents'] = response.body['Contents'][0...options['max-keys']]
|
||||||
|
end
|
||||||
else
|
else
|
||||||
response.status = 404
|
response.status = 404
|
||||||
raise(Fog::Errors.status_error(200, 404, response))
|
raise(Fog::Errors.status_error(200, 404, response))
|
||||||
|
|
|
@ -5,11 +5,14 @@ describe 'S3.get_bucket' do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
s3.put_bucket('foggetbucket')
|
s3.put_bucket('foggetbucket')
|
||||||
file = File.open(File.dirname(__FILE__) + '/../../../lorem.txt', 'r')
|
file = File.open(File.dirname(__FILE__) + '/../../../lorem.txt', 'r')
|
||||||
s3.put_object('foggetbucket', 'fog_get_bucket', file)
|
s3.put_object('foggetbucket', 'fog_object', file)
|
||||||
|
file = File.open(File.dirname(__FILE__) + '/../../../lorem.txt', 'r')
|
||||||
|
s3.put_object('foggetbucket', 'fog_other_object', file)
|
||||||
end
|
end
|
||||||
|
|
||||||
after(:all) do
|
after(:all) do
|
||||||
s3.delete_object('foggetbucket', 'fog_get_bucket')
|
s3.delete_object('foggetbucket', 'fog_object')
|
||||||
|
s3.delete_object('foggetbucket', 'fog_other_object')
|
||||||
s3.delete_bucket('foggetbucket')
|
s3.delete_bucket('foggetbucket')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -21,9 +24,10 @@ describe 'S3.get_bucket' do
|
||||||
actual.body['Name'].should be_a(String)
|
actual.body['Name'].should be_a(String)
|
||||||
actual.body['Prefix'].should be_a(String)
|
actual.body['Prefix'].should be_a(String)
|
||||||
actual.body['Contents'].should be_an(Array)
|
actual.body['Contents'].should be_an(Array)
|
||||||
|
actual.body['Contents'].length.should == 2
|
||||||
object = actual.body['Contents'].first
|
object = actual.body['Contents'].first
|
||||||
object['ETag'].should be_a(String)
|
object['ETag'].should be_a(String)
|
||||||
object['Key'].should == 'fog_get_bucket'
|
object['Key'].should == 'fog_object'
|
||||||
object['LastModified'].should be_a(Time)
|
object['LastModified'].should be_a(Time)
|
||||||
owner = object['Owner']
|
owner = object['Owner']
|
||||||
owner['DisplayName'].should be_a(String)
|
owner['DisplayName'].should be_a(String)
|
||||||
|
@ -32,17 +36,58 @@ describe 'S3.get_bucket' do
|
||||||
object['StorageClass'].should be_a(String)
|
object['StorageClass'].should be_a(String)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should accept options' do
|
it 'should accept marker option' do
|
||||||
actual = s3.get_bucket('foggetbucket', 'prefix' => 'fog_')
|
actual = s3.get_bucket('foggetbucket', 'marker' => 'fog_object')
|
||||||
actual.body['IsTruncated'].should == false
|
actual.body['IsTruncated'].should == false
|
||||||
actual.body['Marker'].should be_a(String)
|
actual.body['Marker'].should be_a(String)
|
||||||
actual.body['MaxKeys'].should be_an(Integer)
|
actual.body['MaxKeys'].should be_an(Integer)
|
||||||
actual.body['Name'].should be_a(String)
|
actual.body['Name'].should be_a(String)
|
||||||
actual.body['Prefix'].should be_a(String)
|
actual.body['Prefix'].should be_a(String)
|
||||||
actual.body['Contents'].should be_an(Array)
|
actual.body['Contents'].should be_an(Array)
|
||||||
|
actual.body['Contents'].length.should == 1
|
||||||
object = actual.body['Contents'].first
|
object = actual.body['Contents'].first
|
||||||
object['ETag'].should be_a(String)
|
object['ETag'].should be_a(String)
|
||||||
object['Key'].should == 'fog_get_bucket'
|
object['Key'].should == 'fog_other_object'
|
||||||
|
object['LastModified'].should be_a(Time)
|
||||||
|
owner = object['Owner']
|
||||||
|
owner['DisplayName'].should be_a(String)
|
||||||
|
owner['ID'].should be_a(String)
|
||||||
|
object['Size'].should be_an(Integer)
|
||||||
|
object['StorageClass'].should be_a(String)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should respect max-keys option' do
|
||||||
|
actual = s3.get_bucket('foggetbucket', 'max-keys' => 1)
|
||||||
|
actual.body['IsTruncated'].should == true
|
||||||
|
actual.body['Marker'].should be_a(String)
|
||||||
|
actual.body['MaxKeys'].should be_an(Integer)
|
||||||
|
actual.body['Name'].should be_a(String)
|
||||||
|
actual.body['Prefix'].should be_a(String)
|
||||||
|
actual.body['Contents'].should be_an(Array)
|
||||||
|
actual.body['Contents'].length.should == 1
|
||||||
|
object = actual.body['Contents'].first
|
||||||
|
object['ETag'].should be_a(String)
|
||||||
|
object['Key'].should == 'fog_object'
|
||||||
|
object['LastModified'].should be_a(Time)
|
||||||
|
owner = object['Owner']
|
||||||
|
owner['DisplayName'].should be_a(String)
|
||||||
|
owner['ID'].should be_a(String)
|
||||||
|
object['Size'].should be_an(Integer)
|
||||||
|
object['StorageClass'].should be_a(String)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should accept prefix option' do
|
||||||
|
actual = s3.get_bucket('foggetbucket', 'prefix' => 'fog_ob')
|
||||||
|
actual.body['IsTruncated'].should == false
|
||||||
|
actual.body['Marker'].should be_a(String)
|
||||||
|
actual.body['MaxKeys'].should be_an(Integer)
|
||||||
|
actual.body['Name'].should be_a(String)
|
||||||
|
actual.body['Prefix'].should be_a(String)
|
||||||
|
actual.body['Contents'].should be_an(Array)
|
||||||
|
actual.body['Contents'].length.should == 1
|
||||||
|
object = actual.body['Contents'].first
|
||||||
|
object['ETag'].should be_a(String)
|
||||||
|
object['Key'].should == 'fog_object'
|
||||||
object['LastModified'].should be_a(Time)
|
object['LastModified'].should be_a(Time)
|
||||||
owner = object['Owner']
|
owner = object['Owner']
|
||||||
owner['DisplayName'].should be_a(String)
|
owner['DisplayName'].should be_a(String)
|
||||||
|
|
Loading…
Reference in a new issue