Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow empty array to be used as a params in get requests. #477

Merged
merged 1 commit into from
Jun 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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