From 99a233863a1a56b802463bf910dc13b67fa2dae9 Mon Sep 17 00:00:00 2001 From: Jacob Frautschi Date: Fri, 12 Jul 2024 17:17:31 -0700 Subject: [PATCH 1/3] Don't wrap request bodies if they are already Readables 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`. --- lib/async/http/faraday/adapter.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/async/http/faraday/adapter.rb b/lib/async/http/faraday/adapter.rb index 06f017f..ebae7dd 100644 --- a/lib/async/http/faraday/adapter.rb +++ b/lib/async/http/faraday/adapter.rb @@ -118,7 +118,9 @@ def call(env) if body = env.body # We need to wrap the body 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) From 03bdb14223273c997a814a528fcae6f2859d6b66 Mon Sep 17 00:00:00 2001 From: Jake Frautschi Date: Mon, 15 Jul 2024 14:04:51 -0700 Subject: [PATCH 2/3] add test --- test/async/http/faraday/adapter.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/async/http/faraday/adapter.rb b/test/async/http/faraday/adapter.rb index 992dffd..eb1a93f 100644 --- a/test/async/http/faraday/adapter.rb +++ b/test/async/http/faraday/adapter.rb @@ -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| @@ -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__, 129) + end end end From 0d1093204fd13a3c76abb5fd36a76a4672670d16 Mon Sep 17 00:00:00 2001 From: Jake Frautschi Date: Mon, 15 Jul 2024 16:02:37 -0700 Subject: [PATCH 3/3] :broom: --- lib/async/http/faraday/adapter.rb | 2 +- test/async/http/faraday/adapter.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/async/http/faraday/adapter.rb b/lib/async/http/faraday/adapter.rb index 8098cc7..23becf1 100644 --- a/lib/async/http/faraday/adapter.rb +++ b/lib/async/http/faraday/adapter.rb @@ -153,7 +153,7 @@ 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.is_a?(::Protocol::HTTP::Body::Readable) # Good to go diff --git a/test/async/http/faraday/adapter.rb b/test/async/http/faraday/adapter.rb index eb1a93f..09a423c 100644 --- a/test/async/http/faraday/adapter.rb +++ b/test/async/http/faraday/adapter.rb @@ -129,13 +129,13 @@ def get_response(url = bound_url, path = '/index', adapter_options: {}) end it "can use a ::Protocol::HTTP::Body::Readable body" do - readable = ::Protocol::HTTP::Body::File.new(File.open(__FILE__, 'r'), 0..128) + 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__, 129) + expect(response.body).to be == File.read(__FILE__, 128) end end end