Skip to content

Commit e13da8e

Browse files
committed
Tighten use of form-encoding to only cases where a JSON body is not ever allowed
1 parent 6089c86 commit e13da8e

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

lib/google/apis/core/api_command.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ def check_status(status, header = nil, body = nil, message = nil)
105105
end
106106
end
107107

108+
def allow_form_encoding?
109+
request_representation.nil? && super
110+
end
111+
108112
private
109113

110114
# Attempt to parse a JSON error message, returning the first found error

lib/google/apis/core/http_command.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ def prepare!
146146
self.url = url.expand(params) if url.is_a?(Addressable::Template)
147147
url.query_values = query.merge(url.query_values || {})
148148

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

309+
def allow_form_encoding?
310+
[:post, :put].include?(method) && body.nil?
311+
end
312+
309313
private
310314

311315
def form_encoded?

spec/google/apis/core/api_command_spec.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
require 'google/apis/core/json_representation'
1818
require 'hurley/test'
1919

20-
RSpec.describe Google::Apis::Core::HttpCommand do
20+
RSpec.describe Google::Apis::Core::ApiCommand do
2121
include TestHelpers
2222
include_context 'HTTP client'
2323

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

4647
before(:example) do
47-
stub_request(:post, 'https://www.googleapis.com/zoo/animals')
48+
stub_request(:post, 'https://www.googleapis.com/zoo/animals?a=b')
4849
.to_return(headers: { 'Content-Type' => 'application/json' }, body: %({}))
4950
end
5051

5152
it 'should serialize the request object' do
5253
command.execute(client)
53-
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals').with do |req|
54+
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals?a=b').with do |req|
5455
be_json_eql(%({"value":"hello"})).matches?(req.body)
5556
end).to have_been_made
5657
end
58+
59+
it 'should not form encode query parameters when body expected but nil' do
60+
command.query['a'] = 'b'
61+
command.request_object = nil
62+
command.execute(client)
63+
expect(a_request(:post, 'https://www.googleapis.com/zoo/animals?a=b').with(body: nil)).to have_been_made
64+
end
5765
end
5866

5967
context('with a JSON response') do

0 commit comments

Comments
 (0)