From 6fa4813dd0ae67f3ceecd392b73787a48ed53097 Mon Sep 17 00:00:00 2001 From: Mark Huk Date: Mon, 4 Jun 2018 16:01:43 +0200 Subject: [PATCH] Added test on put_object with `nil` as object body Fix trailing spaces Added data presence check to put_object `Fog::Storage::GoogleJSON.put_object` to be precise --- .../google_json/requests/put_object.rb | 34 ++++++++++++------- test/integration/storage/test_objects.rb | 6 ++++ 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/lib/fog/storage/google_json/requests/put_object.rb b/lib/fog/storage/google_json/requests/put_object.rb index 165893f26f..0ff3bf2ae3 100644 --- a/lib/fog/storage/google_json/requests/put_object.rb +++ b/lib/fog/storage/google_json/requests/put_object.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Fog module Storage class GoogleJSON @@ -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) @@ -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 diff --git a/test/integration/storage/test_objects.rb b/test/integration/storage/test_objects.rb index c01716a954..baf948137f 100644 --- a/test/integration/storage/test_objects.rb +++ b/test/integration/storage/test_objects.rb @@ -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"