diff --git a/lib/async/http/faraday/adapter.rb b/lib/async/http/faraday/adapter.rb index bd83a46..06f017f 100644 --- a/lib/async/http/faraday/adapter.rb +++ b/lib/async/http/faraday/adapter.rb @@ -18,7 +18,7 @@ module Async module HTTP module Faraday - class BodyWrapper < ::Protocol::HTTP::Body::Readable + class BodyReadWrapper < ::Protocol::HTTP::Body::Readable def initialize(body, block_size: 4096) @body = body @block_size = block_size @@ -118,8 +118,11 @@ 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`. - # body = ::Protocol::HTTP::Body::Buffered.wrap(body) - body = BodyWrapper.new(body) + if body.respond_to?(:read) + body = BodyReadWrapper.new(body) + else + body = ::Protocol::HTTP::Body::Buffered.wrap(body) + end end if headers = env.request_headers diff --git a/readme.md b/readme.md index 2dba17e..ba380b7 100644 --- a/readme.md +++ b/readme.md @@ -31,8 +31,8 @@ require 'async/http/faraday' Faraday.default_adapter = :async_http # Per connection: -conn = Faraday.new(...) do |faraday| - faraday.adapter :async_http +connection = Faraday.new(...) do |builder| + builder.adapter :async_http end ``` @@ -40,7 +40,7 @@ Here is how you make a request: ``` ruby Async do - response = conn.get("/index") + response = connection.get("/index") end ``` diff --git a/spec/async/http/faraday/adapter/proxy_spec.rb b/spec/async/http/faraday/adapter/proxy_spec.rb index 75d5b70..396a0ee 100644 --- a/spec/async/http/faraday/adapter/proxy_spec.rb +++ b/spec/async/http/faraday/adapter/proxy_spec.rb @@ -13,9 +13,9 @@ include_context Async::RSpec::Reactor def get_response(url = endpoint.url, path = '/index', adapter_options: {}) - connection = Faraday.new(url, proxy: ENV['PROXY_URL']) do |faraday| - faraday.response :logger - faraday.adapter :async_http, **adapter_options + connection = Faraday.new(url, proxy: ENV['PROXY_URL']) do |builder| + builder.response :logger + builder.adapter :async_http, **adapter_options end connection.get(path) diff --git a/spec/async/http/faraday/adapter_spec.rb b/spec/async/http/faraday/adapter_spec.rb index 8db70c6..22730b5 100644 --- a/spec/async/http/faraday/adapter_spec.rb +++ b/spec/async/http/faraday/adapter_spec.rb @@ -46,13 +46,11 @@ def run_server(response = Protocol::HTTP::Response[204], response_delay: nil) end def get_response(url = endpoint.url, path = '/index', adapter_options: {}) - connection = Faraday.new(url) do |faraday| - faraday.response :logger - faraday.adapter :async_http, **adapter_options + connection = Faraday.new(url) do |builder| + builder.adapter :async_http, **adapter_options end connection.get(path) - ensure connection&.close end @@ -86,8 +84,8 @@ def get_response(url = endpoint.url, path = '/index', adapter_options: {}) end it "works without initial url and trailing slash (compatiblisity to the original behaviour)" do - response = Faraday.new do |faraday| - faraday.adapter :async_http + response = Faraday.new do |builder| + builder.adapter :async_http end.get 'https://www.google.com' expect(response).to be_success end @@ -124,6 +122,31 @@ def get_response(url = endpoint.url, path = '/index', adapter_options: {}) expect { get_response(endpoint.url, '/index') }.to raise_error(Faraday::ConnectionFailed) end + def post_response(body, url = endpoint.url, path = '/index', adapter_options: {}) + connection = Faraday.new(url) do |builder| + builder.request :url_encoded + builder.adapter :async_http, **adapter_options + end + + connection.post(path) do |request| + request.body = body + end + ensure + connection&.close + end + + it 'can use string body' do + run_server(Protocol::HTTP::Response[200, {}, ['Hello World']]) do + expect(post_response('Hello World').body).to be == 'Hello World' + end + end + + it 'can use url-encoded body' do + run_server(Protocol::HTTP::Response[200, {}, ['Hello World']]) do + expect(post_response({text: 'Hello World'}).body).to be == 'Hello World' + end + end + it 'can use multi-part post body' do connection = Faraday.new do |builder| builder.request :multipart