Skip to content

Commit

Permalink
Fix handling of string bodies.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Feb 8, 2024
1 parent 0584022 commit 47c4649
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
9 changes: 6 additions & 3 deletions lib/async/http/faraday/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ 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
```

Here is how you make a request:

``` ruby
Async do
response = conn.get("/index")
response = connection.get("/index")
end
```

Expand Down
6 changes: 3 additions & 3 deletions spec/async/http/faraday/adapter/proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 29 additions & 6 deletions spec/async/http/faraday/adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 47c4649

Please sign in to comment.