#475, #488 -- allow raw JSON if explicitly requested, fix empty body handling & test cleanup

This commit is contained in:
Steve Bazyl 2017-03-31 15:26:51 -07:00
parent 489378db58
commit 97ff7f4d9a
5 changed files with 49 additions and 6 deletions

View File

@ -55,7 +55,11 @@ module Google
query[FIELDS_PARAM] = normalize_fields_param(query[FIELDS_PARAM]) if query.key?(FIELDS_PARAM)
if request_representation && request_object
header['Content-Type'] ||= JSON_CONTENT_TYPE
self.body = request_representation.new(request_object).to_json(user_options: { skip_undefined: true })
if options && options.skip_serialization
self.body = request_object
else
self.body = request_representation.new(request_object).to_json(user_options: { skip_undefined: true })
end
end
super
end
@ -71,6 +75,7 @@ module Google
# noinspection RubyUnusedLocalVariable
def decode_response_body(content_type, body)
return super unless response_representation
return super if options && options.skip_deserialization
return super if content_type.nil?
return nil unless content_type.start_with?(JSON_CONTENT_TYPE)
instance = response_class.new

View File

@ -156,10 +156,7 @@ module Google
@form_encoded = false
end
unless body
self.header['Content-Type'] = 'application/json'
self.header['Content-Length'] = 0
end
self.body = '' unless self.body
end

View File

@ -27,7 +27,9 @@ module Google
:header,
:timeout_sec,
:open_timeout_sec,
:normalize_unicode)
:normalize_unicode,
:skip_serialization,
:skip_deserialization)
# General client options
class ClientOptions
@ -59,6 +61,10 @@ module Google
# @return [Hash<String,String] Additional HTTP headers to include in requests
# @!attribute [rw] normalize_unicode
# @return [Boolean] True if unicode strings should be normalized in path parameters
# @!attribute [rw] skip_serialization
# @return [Boolean] True if body object should be treated as raw text instead of an object.
# @!attribute [rw] skip_deserialization
# @return [Boolean] True if response should be returned in raw form instead of deserialized.
# Get the default options
# @return [Google::Apis::RequestOptions]
@ -84,5 +90,7 @@ module Google
RequestOptions.default.retries = 0
RequestOptions.default.open_timeout_sec = 20
RequestOptions.default.normalize_unicode = false
RequestOptions.default.skip_serialization = false
RequestOptions.default.skip_deserialization = false
end
end

View File

@ -63,6 +63,29 @@ RSpec.describe Google::Apis::Core::ApiCommand do
end
end
context('with a raw request body') do
let(:command) do
request = model_class.new
command = Google::Apis::Core::ApiCommand.new(:post, 'https://www.googleapis.com/zoo/animals')
command.request_representation = representer_class
command.request_object = %({"value": "hello"})
command.options.skip_serialization = true
command
end
before(:example) do
stub_request(:post, 'https://www.googleapis.com/zoo/animals')
.to_return(headers: { 'Content-Type' => 'application/json' }, body: %({}))
end
it 'should allow raw JSON if skip_serialization = true' do
command.execute(client)
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals').with do |req|
be_json_eql(%({"value":"hello"})).matches?(req.body)
end).to have_been_made
end
end
context('with a JSON response') do
let(:command) do
command = Google::Apis::Core::ApiCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
@ -85,6 +108,12 @@ RSpec.describe Google::Apis::Core::ApiCommand do
result = command.execute(client)
expect(result.value).to eql 'hello'
end
it 'should return a raw JSON if skip_deserialization true' do
command.options.skip_deserialization = true
result = command.execute(client)
expect(result).to eql %({"value" : "hello"})
end
end
context('with an invalid content-type response') do

View File

@ -95,6 +95,10 @@ RSpec.describe Google::Apis::Core::BaseService do
context 'with proxy' do
after(:example) do
Google::Apis::ClientOptions.default.proxy_url = nil
end
it 'should allow proxy URLs as strings' do
Google::Apis::ClientOptions.default.proxy_url = 'http://gateway.example.com:1234'
service.client