Skip to content

Commit

Permalink
Allow passing HTTP::FormData object directly
Browse files Browse the repository at this point in the history
  • Loading branch information
summera committed Mar 10, 2020
1 parent 2fed1f9 commit 08d19d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def make_request_body(opts, headers)
when opts.body
opts.body
when opts.form
form = HTTP::FormData.create opts.form
form = form_data(opts.form)
headers[Headers::CONTENT_TYPE] ||= form.content_type
form
when opts.json
Expand All @@ -184,5 +184,9 @@ def make_request_body(opts, headers)
body
end
end

def form_data(form)
(form || {}).respond_to?(:to_h) ? HTTP::FormData.create(form) : form
end
end
end
14 changes: 14 additions & 0 deletions spec/lib/http/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,20 @@ def simple_response(body, status = 200)

client.get("http://example.com/", :form => {:foo => HTTP::FormData::Part.new("content")})
end

context "when passing an HTTP::FormData object directly" do
it "creates url encoded form data object" do
client = HTTP::Client.new
allow(client).to receive(:perform)

expect(HTTP::Request).to receive(:new) do |opts|
expect(opts[:body]).to be_a(HTTP::FormData::Urlencoded)
expect(opts[:body].to_s).to eq "foo=bar"
end

client.get("http://example.com/", :form => HTTP::FormData.create({ :foo => "bar" }))
end
end
end

describe "passing json" do
Expand Down

0 comments on commit 08d19d8

Please sign in to comment.