Skip to content

Commit

Permalink
Tighten use of form-encoding to only cases where a JSON body is not e…
Browse files Browse the repository at this point in the history
…ver allowed
  • Loading branch information
sqrrrl committed Sep 8, 2016
1 parent 6089c86 commit e13da8e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/google/apis/core/api_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def check_status(status, header = nil, body = nil, message = nil)
end
end

def allow_form_encoding?
request_representation.nil? && super
end

private

# Attempt to parse a JSON error message, returning the first found error
Expand Down
6 changes: 5 additions & 1 deletion lib/google/apis/core/http_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def prepare!
self.url = url.expand(params) if url.is_a?(Addressable::Template)
url.query_values = query.merge(url.query_values || {})

if [:post, :put].include?(method) && body.nil?
if allow_form_encoding?
@form_encoded = true
self.body = Addressable::URI.form_encode(url.query_values(Array))
self.header['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'
Expand Down Expand Up @@ -306,6 +306,10 @@ def apply_request_options(req)
req.options.open_timeout = options.open_timeout_sec
end

def allow_form_encoding?
[:post, :put].include?(method) && body.nil?
end

private

def form_encoded?
Expand Down
14 changes: 11 additions & 3 deletions spec/google/apis/core/api_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
require 'google/apis/core/json_representation'
require 'hurley/test'

RSpec.describe Google::Apis::Core::HttpCommand do
RSpec.describe Google::Apis::Core::ApiCommand do
include TestHelpers
include_context 'HTTP client'

Expand All @@ -40,20 +40,28 @@
command = Google::Apis::Core::ApiCommand.new(:post, 'https://www.googleapis.com/zoo/animals')
command.request_representation = representer_class
command.request_object = request
command.query['a'] = 'b'
command
end

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: %({}))
end

it 'should serialize the request object' do
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)
end).to have_been_made
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

context('with a JSON response') do
Expand Down

0 comments on commit e13da8e

Please sign in to comment.