Tighten use of form-encoding to only cases where a JSON body is not ever allowed
This commit is contained in:
parent
6089c866ac
commit
e13da8e05e
|
@ -105,6 +105,10 @@ module Google
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def allow_form_encoding?
|
||||||
|
request_representation.nil? && super
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Attempt to parse a JSON error message, returning the first found error
|
# Attempt to parse a JSON error message, returning the first found error
|
||||||
|
|
|
@ -146,7 +146,7 @@ module Google
|
||||||
self.url = url.expand(params) if url.is_a?(Addressable::Template)
|
self.url = url.expand(params) if url.is_a?(Addressable::Template)
|
||||||
url.query_values = query.merge(url.query_values || {})
|
url.query_values = query.merge(url.query_values || {})
|
||||||
|
|
||||||
if [:post, :put].include?(method) && body.nil?
|
if allow_form_encoding?
|
||||||
@form_encoded = true
|
@form_encoded = true
|
||||||
self.body = Addressable::URI.form_encode(url.query_values(Array))
|
self.body = Addressable::URI.form_encode(url.query_values(Array))
|
||||||
self.header['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
|
self.header['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
|
||||||
|
@ -306,6 +306,10 @@ module Google
|
||||||
req.options.open_timeout = options.open_timeout_sec
|
req.options.open_timeout = options.open_timeout_sec
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def allow_form_encoding?
|
||||||
|
[:post, :put].include?(method) && body.nil?
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def form_encoded?
|
def form_encoded?
|
||||||
|
|
|
@ -17,7 +17,7 @@ require 'google/apis/core/api_command'
|
||||||
require 'google/apis/core/json_representation'
|
require 'google/apis/core/json_representation'
|
||||||
require 'hurley/test'
|
require 'hurley/test'
|
||||||
|
|
||||||
RSpec.describe Google::Apis::Core::HttpCommand do
|
RSpec.describe Google::Apis::Core::ApiCommand do
|
||||||
include TestHelpers
|
include TestHelpers
|
||||||
include_context 'HTTP client'
|
include_context 'HTTP client'
|
||||||
|
|
||||||
|
@ -40,20 +40,28 @@ RSpec.describe Google::Apis::Core::HttpCommand do
|
||||||
command = Google::Apis::Core::ApiCommand.new(:post, 'https://www.googleapis.com/zoo/animals')
|
command = Google::Apis::Core::ApiCommand.new(:post, 'https://www.googleapis.com/zoo/animals')
|
||||||
command.request_representation = representer_class
|
command.request_representation = representer_class
|
||||||
command.request_object = request
|
command.request_object = request
|
||||||
|
command.query['a'] = 'b'
|
||||||
command
|
command
|
||||||
end
|
end
|
||||||
|
|
||||||
before(:example) do
|
before(:example) do
|
||||||
stub_request(:post, 'https://www.googleapis.com/zoo/animals')
|
stub_request(:post, 'https://www.googleapis.com/zoo/animals?a=b')
|
||||||
.to_return(headers: { 'Content-Type' => 'application/json' }, body: %({}))
|
.to_return(headers: { 'Content-Type' => 'application/json' }, body: %({}))
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should serialize the request object' do
|
it 'should serialize the request object' do
|
||||||
command.execute(client)
|
command.execute(client)
|
||||||
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals').with do |req|
|
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals?a=b').with do |req|
|
||||||
be_json_eql(%({"value":"hello"})).matches?(req.body)
|
be_json_eql(%({"value":"hello"})).matches?(req.body)
|
||||||
end).to have_been_made
|
end).to have_been_made
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'should not form encode query parameters when body expected but nil' do
|
||||||
|
command.query['a'] = 'b'
|
||||||
|
command.request_object = nil
|
||||||
|
command.execute(client)
|
||||||
|
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals?a=b').with(body: nil)).to have_been_made
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context('with a JSON response') do
|
context('with a JSON response') do
|
||||||
|
|
Loading…
Reference in New Issue