Skip to content

Commit

Permalink
Allow empty array to be used as a params in get requests.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryrck committed Jun 9, 2016
1 parent 0c1ac2f commit 92be470
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/httparty/hash_conversions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/httparty/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,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

Expand Down
8 changes: 8 additions & 0 deletions spec/httparty/hash_conversions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
22 changes: 20 additions & 2 deletions spec/httparty/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 92be470

Please sign in to comment.