From 08d19d8f34c56ac012928057c2c1c7a9b65eef2d Mon Sep 17 00:00:00 2001 From: Ari Summer Date: Mon, 9 Mar 2020 18:31:22 -0600 Subject: [PATCH 1/2] Allow passing HTTP::FormData object directly --- lib/http/client.rb | 6 +++++- spec/lib/http/client_spec.rb | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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..4fa3b26e 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 From 8bcb47250815f9af8ceefdc780ecb812ce08e428 Mon Sep 17 00:00:00 2001 From: Ari Summer Date: Mon, 9 Mar 2020 21:28:29 -0600 Subject: [PATCH 2/2] Remove space inside hash --- spec/lib/http/client_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/lib/http/client_spec.rb b/spec/lib/http/client_spec.rb index 4fa3b26e..ad74bdef 100644 --- a/spec/lib/http/client_spec.rb +++ b/spec/lib/http/client_spec.rb @@ -208,7 +208,7 @@ def simple_response(body, status = 200) expect(opts[:body].to_s).to eq "foo=bar" end - client.get("http://example.com/", :form => HTTP::FormData.create({ :foo => "bar" })) + client.get("http://example.com/", :form => HTTP::FormData.create(:foo => "bar")) end end end