Skip to content

Commit

Permalink
Merge pull request #351 from vimutter/patch-2
Browse files Browse the repository at this point in the history
Added test on put_object with `nil` as object body
  • Loading branch information
Temikus authored Jun 5, 2018
2 parents cb42ede + 6fa4813 commit 2c0dece
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
34 changes: 22 additions & 12 deletions lib/fog/storage/google_json/requests/put_object.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Fog
module Storage
class GoogleJSON
Expand Down Expand Up @@ -46,18 +48,7 @@ def put_object(bucket_name,
kms_key_name: nil,
predefined_acl: nil,
**options)
if data.is_a?(String)
data = StringIO.new(data)
options[:content_type] ||= "text/plain"
elsif data.is_a?(::File)
options[:content_type] ||= Fog::Storage.parse_data(data)[:headers]["Content-Type"]
end

# Paperclip::AbstractAdapter
if data.respond_to?(:content_type) && data.respond_to?(:path)
options[:content_type] ||= data.content_type
data = data.path
end
data, options = normalize_data(data, options)

object_config = ::Google::Apis::StorageV1::Object.new(
options.merge(:name => object_name)
Expand All @@ -78,6 +69,25 @@ def put_object(bucket_name,
:upload_source => data
)
end

protected

def normalize_data(data, options)
raise ArgumentError.new("data is required") unless data
if data.is_a?(String)
data = StringIO.new(data)
options[:content_type] ||= "text/plain"
elsif data.is_a?(::File)
options[:content_type] ||= Fog::Storage.parse_data(data)[:headers]["Content-Type"]
end

# Paperclip::AbstractAdapter
if data.respond_to?(:content_type) && data.respond_to?(:path)
options[:content_type] ||= data.content_type
data = data.path
end
[data, options]
end
end

class Mock
Expand Down
6 changes: 6 additions & 0 deletions test/integration/storage/test_objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ def test_put_object_string
assert_equal(temp_file_content, object[:body])
end

def test_put_object_nil
assert_raises(ArgumentError) do
@client.put_object(some_bucket_name, new_object_name, nil)
end
end

def test_put_object_file
object_name = new_object_name
expected_body = "A file body"
Expand Down

0 comments on commit 2c0dece

Please sign in to comment.