Skip to content

Commit 7ffe926

Browse files
authored
Don't wrap request bodies if they are already Readables (#38)
We don't need to wrap request bodies if they already conform to the `::Protocol::HTTP::Body::Readable` interface. This way developers can use other kinds of `Readable`s directly when making calls with `Faraday`.
1 parent dd1e4e4 commit 7ffe926

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/async/http/faraday/adapter.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,11 @@ def call(env)
153153
end
154154

155155
if body = env.body
156-
# We need to wrap the body in a Readable object so that it can be read in chunks:
156+
# We need to ensure the body is wrapped in a Readable object so that it can be read in chunks:
157157
# Faraday's body only responds to `#read`.
158-
if body.respond_to?(:read)
158+
if body.is_a?(::Protocol::HTTP::Body::Readable)
159+
# Good to go
160+
elsif body.respond_to?(:read)
159161
body = BodyReadWrapper.new(body)
160162
else
161163
body = ::Protocol::HTTP::Body::Buffered.wrap(body)

test/async/http/faraday/adapter.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
require 'faraday'
1616
require 'faraday/multipart'
1717

18+
require 'protocol/http/body/file'
19+
1820
describe Async::HTTP::Faraday::Adapter do
1921
def get_response(url = bound_url, path = '/index', adapter_options: {})
2022
connection = Faraday.new(url) do |builder|
@@ -125,6 +127,16 @@ def get_response(url = bound_url, path = '/index', adapter_options: {})
125127

126128
expect(response.body).to be == 'text=Hello+World'
127129
end
130+
131+
it "can use a ::Protocol::HTTP::Body::Readable body" do
132+
readable = ::Protocol::HTTP::Body::File.new(File.open(__FILE__, 'r'), 0...128)
133+
134+
response = Faraday.new do |builder|
135+
builder.adapter :async_http
136+
end.post(bound_url, readable)
137+
138+
expect(response.body).to be == File.read(__FILE__, 128)
139+
end
128140
end
129141
end
130142

0 commit comments

Comments
 (0)