From 97ff7f4d9a009d657de65691a7af4635d8d90524 Mon Sep 17 00:00:00 2001 From: Steve Bazyl Date: Fri, 31 Mar 2017 15:26:51 -0700 Subject: [PATCH] #475, #488 -- allow raw JSON if explicitly requested, fix empty body handling & test cleanup --- lib/google/apis/core/api_command.rb | 7 +++++- lib/google/apis/core/http_command.rb | 5 +--- lib/google/apis/options.rb | 10 +++++++- spec/google/apis/core/api_command_spec.rb | 29 +++++++++++++++++++++++ spec/google/apis/core/service_spec.rb | 4 ++++ 5 files changed, 49 insertions(+), 6 deletions(-) diff --git a/lib/google/apis/core/api_command.rb b/lib/google/apis/core/api_command.rb index 27430e712..1df7d6240 100644 --- a/lib/google/apis/core/api_command.rb +++ b/lib/google/apis/core/api_command.rb @@ -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 diff --git a/lib/google/apis/core/http_command.rb b/lib/google/apis/core/http_command.rb index aff138317..4e544b719 100644 --- a/lib/google/apis/core/http_command.rb +++ b/lib/google/apis/core/http_command.rb @@ -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 diff --git a/lib/google/apis/options.rb b/lib/google/apis/options.rb index 066558a62..1287fef6d 100644 --- a/lib/google/apis/options.rb +++ b/lib/google/apis/options.rb @@ -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 '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 diff --git a/spec/google/apis/core/service_spec.rb b/spec/google/apis/core/service_spec.rb index d9cbab0ea..673b8311c 100644 --- a/spec/google/apis/core/service_spec.rb +++ b/spec/google/apis/core/service_spec.rb @@ -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