diff --git a/lib/http/client.rb b/lib/http/client.rb index ece1140a..b9efb46c 100644 --- a/lib/http/client.rb +++ b/lib/http/client.rb @@ -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 @@ -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 diff --git a/spec/lib/http/client_spec.rb b/spec/lib/http/client_spec.rb index eb4eb1e2..ad74bdef 100644 --- a/spec/lib/http/client_spec.rb +++ b/spec/lib/http/client_spec.rb @@ -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