Skip to content
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: 4 additions & 2 deletions lib/async/http/faraday/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,11 @@ def call(env)
end

if body = env.body
# We need to wrap the body in a Readable object so that it can be read in chunks:
# We need to ensure the body is wrapped in a Readable object so that it can be read in chunks:
# Faraday's body only responds to `#read`.
if body.respond_to?(:read)
if body.is_a?(::Protocol::HTTP::Body::Readable)
# Good to go
elsif body.respond_to?(:read)
body = BodyReadWrapper.new(body)
else
body = ::Protocol::HTTP::Body::Buffered.wrap(body)
Expand Down
12 changes: 12 additions & 0 deletions test/async/http/faraday/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
require 'faraday'
require 'faraday/multipart'

require 'protocol/http/body/file'

describe Async::HTTP::Faraday::Adapter do
def get_response(url = bound_url, path = '/index', adapter_options: {})
connection = Faraday.new(url) do |builder|
Expand Down Expand Up @@ -125,6 +127,16 @@ def get_response(url = bound_url, path = '/index', adapter_options: {})

expect(response.body).to be == 'text=Hello+World'
end

it "can use a ::Protocol::HTTP::Body::Readable body" do
readable = ::Protocol::HTTP::Body::File.new(File.open(__FILE__, 'r'), 0...128)

response = Faraday.new do |builder|
builder.adapter :async_http
end.post(bound_url, readable)

expect(response.body).to be == File.read(__FILE__, 128)
end
end
end

Expand Down