diff --git a/lib/httparty/hash_conversions.rb b/lib/httparty/hash_conversions.rb index cecaa1f2..9eb03de1 100644 --- a/lib/httparty/hash_conversions.rb +++ b/lib/httparty/hash_conversions.rb @@ -26,7 +26,11 @@ def self.normalize_param(key, value) stack = [] if value.respond_to?(:to_ary) - param << value.to_ary.map { |element| normalize_param("#{key}[]", element) }.join + param << if value.empty? + "#{key}[]=&" + else + value.to_ary.map { |element| normalize_param("#{key}[]", element) }.join + end elsif value.respond_to?(:to_hash) stack << [key, value.to_hash] else diff --git a/lib/httparty/request.rb b/lib/httparty/request.rb index 65a4a12d..7274a8ab 100644 --- a/lib/httparty/request.rb +++ b/lib/httparty/request.rb @@ -78,7 +78,11 @@ def uri # avoid double query string on redirects [#12] unless redirect - new_uri.query = query_string(new_uri) + possible_query_string = query_string(new_uri) + + if possible_query_string != nil && possible_query_string != "" + new_uri.query = query_string(new_uri) + end end unless SupportedURISchemes.include? new_uri.scheme @@ -201,7 +205,9 @@ def query_string(uri) query_string_parts << options[:query] unless options[:query].nil? end - query_string_parts.reject!(&:empty?) unless query_string_parts == [""] + query_string_parts.reject! { |part| + part.is_a?(Array) || part.empty? + } unless query_string_parts == [""] query_string_parts.size > 0 ? query_string_parts.join('&') : nil end diff --git a/spec/httparty/hash_conversions_spec.rb b/spec/httparty/hash_conversions_spec.rb index 2ff57fe7..a4033912 100644 --- a/spec/httparty/hash_conversions_spec.rb +++ b/spec/httparty/hash_conversions_spec.rb @@ -22,6 +22,14 @@ end end + context "value is an empty array" do + it "creates a params string" do + expect( + HTTParty::HashConversions.normalize_param(:people, []) + ).to eq("people[]=&") + end + end + context "value is hash" do it "creates a params string" do expect( diff --git a/spec/httparty/request_spec.rb b/spec/httparty/request_spec.rb index 1ee70f36..acf37161 100644 --- a/spec/httparty/request_spec.rb +++ b/spec/httparty/request_spec.rb @@ -56,6 +56,24 @@ expect(request.connection_adapter).to eq(my_adapter) end + context "when using a query string" do + context "and it has an empty array" do + it "sets correct query string" do + request = HTTParty::Request.new(Net::HTTP::Get, 'http://google.com', query: { fake_array: [] }) + + expect(request.uri).to eq(URI.parse("http://google.com/?fake_array[]=")) + end + end + + context "when sending an array with only one element" do + it "sets correct query" do + request = HTTParty::Request.new(Net::HTTP::Get, 'http://google.com', query: { fake_array: [1] }) + + expect(request.uri).to eq(URI.parse("http://google.com/?fake_array[]=1")) + end + end + end + context "when basic authentication credentials provided in uri" do context "when basic auth options wasn't set explicitly" do it "sets basic auth from uri" do @@ -221,10 +239,10 @@ end it "respects the query string normalization proc" do - empty_proc = lambda {|qs| ""} + empty_proc = lambda {|qs| "I"} @request.options[:query_string_normalizer] = empty_proc @request.options[:query] = {foo: :bar} - expect(CGI.unescape(@request.uri.query)).to eq("") + expect(CGI.unescape(@request.uri.query)).to eq("I") end it "does not append an ampersand when queries are embedded in paths" do