Skip to content

Commit

Permalink
Utilize HTTP::FormData streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
janko committed May 19, 2017
1 parent 2690a24 commit bb4479f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion http.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Gem::Specification.new do |gem|
gem.required_ruby_version = ">= 2.0"

gem.add_runtime_dependency "http_parser.rb", "~> 0.6.0"
gem.add_runtime_dependency "http-form_data", "~> 1.0.1"
gem.add_runtime_dependency "http-form_data", ">= 2.0.0-pre2", "< 3"
gem.add_runtime_dependency "http-cookie", "~> 1.0"
gem.add_runtime_dependency "addressable", "~> 2.3"

Expand Down
5 changes: 2 additions & 3 deletions lib/http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,8 @@ def make_request_body(opts, headers)
opts.body
when opts.form
form = HTTP::FormData.create opts.form
headers[Headers::CONTENT_TYPE] ||= form.content_type
headers[Headers::CONTENT_LENGTH] ||= form.content_length
form.to_s
headers[Headers::CONTENT_TYPE] ||= form.content_type
form
when opts.json
body = MimeType[:json].encode opts.json
headers[Headers::CONTENT_TYPE] ||= "application/json; charset=#{body.encoding.name}"
Expand Down
26 changes: 26 additions & 0 deletions spec/lib/http/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,32 @@ def simple_response(body, status = 200)
end
end

describe "passing multipart form data" 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 => {:foo => "bar"})
end

it "creates multipart 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::Multipart)
expect(opts[:body].to_s).to include("content")
end

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

describe "passing json" do
it "encodes given object" do
client = HTTP::Client.new
Expand Down

0 comments on commit bb4479f

Please sign in to comment.