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

change s3 mocks data hash to more workable format

This commit is contained in:
Wesley Beary 2009-08-16 11:45:52 -07:00
parent 1f6df7083f
commit 12a570fa50
12 changed files with 56 additions and 81 deletions

View file

@ -49,31 +49,24 @@ else
def copy_object(source_bucket_name, source_object_name, target_bucket_name, target_object_name, options = {})
response = Fog::Response.new
source_bucket_status = get_bucket(source_bucket_name).status
target_bucket_status = get_bucket(target_bucket_name).status
source_object_status = get_object(source_bucket_name, source_object_name).status
source_bucket = @data[:buckets][source_bucket_name]
source_object = source_bucket && source_bucket[:objects][source_object_name]
target_bucket = @data[:buckets][target_bucket_name]
if source_bucket_status != 200
response.status = source_bucket_status
elsif target_bucket_status != 200
response.status = target_bucket_status
elsif source_bucket_status != 200
response.status = source_bucket_status
else
if source_object && target_bucket
response.status = 200
end
if response.status == 200
source_bucket = @data['Buckets'].select {|bucket| bucket['Name'] == source_bucket_name}.first
source_object = source_bucket['Contents'].select {|object| object['Key'] == source_object_name}.first
target_bucket = @data['Buckets'].select {|bucket| bucket['Name'] == target_bucket_name}.first
target_object = source_object.dup
target_object['Name'] = target_object_name
target_bucket['Contents'] << target_object
target_object.merge!({
'ETag' => Fog::AWS::Mock.etag,
'Name' => target_object_name
})
target_bucket[:objects][target_object_name] = target_object
response.body = {
'ETag' => target_object['ETag'],
'LastModified' => Time.parse(target_object['LastModified'])
}
else
response.status = 404
end
response

View file

@ -33,12 +33,10 @@ else
def delete_bucket(bucket_name)
response = Fog::Response.new
bucket_status = get_bucket(bucket_name).status
if bucket_status == 200
if @data[:buckets].delete(bucket_name)
response.status = 204
@data['Buckets'].delete_if {|bucket| bucket['Name'] == bucket_name}
else
response.status = bucket_status
response.status = 404
end
response
end

View file

@ -35,13 +35,11 @@ else
def delete_object(bucket_name, object_name)
response = Fog::Response.new
object_status = get_object(bucket_name, object_name).status
if object_status == 200
if (bucket = @data[:buckets][bucket_name]) && @data[:buckets][bucket_name][:objects][object_name]
response.status = 204
bucket = @data['Buckets'].select {|bucket| bucket['Name'] == bucket_name}.first
bucket['Contents'].delete_if {|object| object['Key'] == object_name}
bucket[:objects].delete(object_name)
else
response.status = object_status
response.status = 404
end
response
end

View file

@ -61,16 +61,17 @@ else
def get_bucket(bucket_name, options = {})
response = Fog::Response.new
bucket = @data['Buckets'].select {|bucket| bucket['Name'] == bucket_name}.first
unless bucket
unless bucket = @data[:buckets][bucket_name]
response.status = 404
else
response.status = 200
response.body = {
'Contents' => bucket['Contents'].map do |object|
'Contents' => bucket[:objects].values.map do |object|
data = object.reject {|key, value| !['ETag', 'Key', 'LastModified', 'Owner', 'Size', 'StorageClass'].include?(key)}
data['LastModified'] = Time.parse(data['LastModified'])
data['Size'] = data['Size'].to_i
data.merge!({
'LastModified' => Time.parse(data['LastModified']),
'Size' => data['Size'].to_i
})
data
end,
'IsTruncated' => false,

View file

@ -36,13 +36,11 @@ else
def get_bucket_location(bucket_name)
response = Fog::Response.new
bucket_status = get_bucket(bucket_name).status
if bucket_status == 200
if bucket = @data[:buckets][bucket_name]
response.status = 200
bucket = @data['Buckets'].select {|bucket| bucket['Name'] == bucket_name}.first
response.body = {'LocationConstraint' => bucket['LocationConstraint'] }
else
response.status = bucket_status
response.status = 404
end
response
end

View file

@ -49,33 +49,26 @@ else
def get_object(bucket_name, object_name, options = {})
response = Fog::Response.new
bucket_status = get_bucket(bucket_name).status
if bucket_status == 200
bucket = @data['Buckets'].select {|bucket| bucket['Name'] == bucket_name}.first
object = bucket['Contents'].select {|object| object['Key'] == object_name}.first
if object
if options['If-Match'] && options['If-Match'] != object['ETag']
response.status = 412
elsif options['If-Modified-Since'] && options['If-Modified-Since'] > Time.parse(object['LastModified'])
response.status = 304
elsif options['If-None-Match'] && options['If-None-Match'] == object['ETag']
response.status = 304
elsif options['If-Unmodified-Since'] && options['If-Unmodified-Since'] < Time.parse(object['LastModified'])
response.status = 412
else
response.status = 200
response.headers = {
'Content-Length' => object['Size'],
'ETag' => object['ETag'],
'Last-Modified' => object['LastModified']
}
response.body = object[:body]
end
if (bucket = @data[:buckets][bucket_name]) && (object = bucket[:objects][object_name])
if options['If-Match'] && options['If-Match'] != object['ETag']
response.status = 412
elsif options['If-Modified-Since'] && options['If-Modified-Since'] > Time.parse(object['LastModified'])
response.status = 304
elsif options['If-None-Match'] && options['If-None-Match'] == object['ETag']
response.status = 304
elsif options['If-Unmodified-Since'] && options['If-Unmodified-Since'] < Time.parse(object['LastModified'])
response.status = 412
else
response.status = 404
response.status = 200
response.headers = {
'Content-Length' => object['Size'],
'ETag' => object['ETag'],
'Last-Modified' => object['LastModified']
}
response.body = object[:body]
end
else
response.status = bucket_status
response.status = 404
end
response
end

View file

@ -36,13 +36,11 @@ else
def get_request_payment(bucket_name)
response = Fog::Response.new
bucket_status = get_bucket(bucket_name).status
if bucket_status == 200
if bucket = @data[:buckets][bucket_name]
response.status = 200
bucket = @data['Buckets'].select {|bucket| bucket['Name'] == bucket_name}.first
response.body = { 'Payer' => bucket['Payer'] }
else
response.status = bucket_status
response.status = 404
end
response
end

View file

@ -39,14 +39,14 @@ else
def get_service
response = Fog::Response.new
response.headers['Status'] = 200
buckets = @data['Buckets'].map do |bucket|
buckets = @data[:buckets].values.map do |bucket|
bucket.reject do |key, value|
!['CreationDate', 'Name'].include?(key)
end
end
response.body = {
'Buckets' => buckets,
'Owner' => { 'DisplayName' => 'owner', 'ID' => 'some_id'}
'Owner' => { 'DisplayName' => 'owner', 'ID' => 'some_id'}
}
response
end

View file

@ -48,17 +48,17 @@ else
response = Fog::Response.new
response.status = 200
bucket = {
'Name' => bucket_name,
'Contents' => [],
'CreationDate' => Time.now,
'Payer' => 'BucketOwner'
:objects => {},
'Name' => bucket_name,
'CreationDate' => Time.now,
'Payer' => 'BucketOwner'
}
if options['LocationConstraint']
bucket['LocationConstraint'] = options['LocationConstraint']
else
bucket['LocationConstraint'] = ''
end
@data['Buckets'] << bucket
@data[:buckets][bucket_name] = bucket
response
end

View file

@ -50,11 +50,9 @@ else
def put_object(bucket_name, object_name, object, options = {})
file = parse_file(object)
response = Fog::Response.new
bucket_status = get_bucket(bucket_name).status
if bucket_status == 200
if (bucket = @data[:buckets][bucket_name])
response.status = 200
bucket = @data['Buckets'].select {|bucket| bucket['Name'] == bucket_name}.first
bucket['Contents'] << {
bucket[:objects][object_name] = {
:body => file[:body],
'ETag' => Fog::AWS::Mock.etag,
'Key' => object_name,
@ -64,7 +62,7 @@ else
'StorageClass' => 'STANDARD'
}
else
response.status = bucket_status
response.status = 404
end
response
end

View file

@ -38,13 +38,11 @@ else
def put_request_payment(bucket_name, payer)
response = Fog::Response.new
bucket_status = get_bucket(bucket_name).status
if bucket_status == 200
if bucket = @data[:buckets][bucket_name]
response.status = 200
bucket = @data['Buckets'].select {|bucket| bucket['Name'] == bucket_name}.first
bucket['Payer'] = payer
else
response.status = bucket_status
response.status = 404
end
response
end

View file

@ -87,7 +87,7 @@ module Fog
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}")
if Fog.mocking?
@data = { 'Buckets' => [] }
@data = { :buckets => {} }
end
end