Module: Fog::Storage::AWS::SharedMockMethods

Included in:
Mock
Defined in:
lib/fog/aws/requests/storage/shared_mock_methods.rb

Instance Method Summary collapse

Instance Method Details

#define_mock_acl(bucket_name, object_name, options) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/fog/aws/requests/storage/shared_mock_methods.rb', line 5

def define_mock_acl(bucket_name, object_name, options)
  acl = options['x-amz-acl'] || 'private'
  if !['private', 'public-read', 'public-read-write', 'authenticated-read'].include?(acl)
    raise Excon::Errors::BadRequest.new('invalid x-amz-acl')
  else
    self.data[:acls][:object][bucket_name] ||= {}
    self.data[:acls][:object][bucket_name][object_name] = self.class.acls(acl)
  end
end

#get_upload_info(bucket_name, upload_id, delete = false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fog/aws/requests/storage/shared_mock_methods.rb', line 33

def get_upload_info(bucket_name, upload_id, delete = false)
  if delete
    upload_info = self.data[:multipart_uploads][bucket_name].delete(upload_id)
  else
    upload_info = self.data[:multipart_uploads][bucket_name][upload_id]
  end

  if !upload_info
    response = Excon::Response.new
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end

  upload_info
end

#parse_mock_data(data) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/fog/aws/requests/storage/shared_mock_methods.rb', line 15

def parse_mock_data(data)
  data = Fog::Storage.parse_data(data)
  unless data[:body].is_a?(String)
    data[:body] = data[:body].read
  end
  data
end

#store_mock_object(bucket, object_name, body, options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/fog/aws/requests/storage/shared_mock_methods.rb', line 49

def store_mock_object(bucket, object_name, body, options)
  object = {
    :body             => body,
    'Content-Type'    => options['Content-Type'],
    'ETag'            => OpenSSL::Digest::MD5.hexdigest(body),
    'Key'             => object_name,
    'Last-Modified'   => Fog::Time.now.to_date_header,
    'Content-Length'  => options['Content-Length'],
    'StorageClass'    => options['x-amz-storage-class'] || 'STANDARD',
    'VersionId'       => bucket[:versioning] == 'Enabled' ? Fog::Mock.random_base64(32) : 'null'
  }

  for key, value in options
    case key
    when 'Cache-Control', 'Content-Disposition', 'Content-Encoding', 'Content-MD5', 'Expires', /^x-amz-meta-/
      object[key] = value
    end
  end

  if bucket[:versioning]
    bucket[:objects][object_name] ||= []

    # When versioning is suspended, putting an object will create a new 'null' version if the latest version
    # is a value other than 'null', otherwise it will replace the latest version.
    if bucket[:versioning] == 'Suspended' && bucket[:objects][object_name].first['VersionId'] == 'null'
      bucket[:objects][object_name].shift
    end

    bucket[:objects][object_name].unshift(object)
  else
    bucket[:objects][object_name] = [object]
  end

  object
end

#verify_mock_bucket_exists(bucket_name) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/fog/aws/requests/storage/shared_mock_methods.rb', line 23

def verify_mock_bucket_exists(bucket_name)
  if (bucket = self.data[:buckets][bucket_name])
    return bucket
  end

  response = Excon::Response.new
  response.status = 404
  raise(Excon::Errors.status_error({:expects => 200}, response))
end