Skip to content

Commit

Permalink
Allow the config block to return a different wrapper. Fixes #43.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 17, 2024
1 parent 2d81bb3 commit 4e52a80
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/async/http/faraday/clients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def close
# @parameter endpoint [IO::Endpoint::Generic] The endpoint to create the client for.
def make_client(endpoint)
client = Client.new(endpoint, **@options)
@block&.call(client)
return client

return @block&.call(client) || client
end

# Get a client for the given endpoint.
Expand Down
2 changes: 1 addition & 1 deletion test/async/http/faraday/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def get_response(url = bound_url, path = '/index', adapter_options: {})

adapter = Faraday.new do |builder|
builder.adapter :async_http do |client|
config_block_invoked = true
config_block_invoked = true; client
end
end

Expand Down
16 changes: 16 additions & 0 deletions test/async/http/faraday/clients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,26 @@
# Copyright, 2024, by Samuel Williams.

require 'async/http/faraday/clients'
require 'async/http/middleware/location_redirector'

describe Async::HTTP::Faraday::PersistentClients do
let(:clients) {subject.new}

with "a block" do
let(:clients) do
subject.new do |client|
Async::HTTP::Middleware::LocationRedirector.new(client)
end
end

it "can wrap the client with middleware" do
endpoint = Async::HTTP::Endpoint.parse('http://example.com')
client = clients.make_client(endpoint)

expect(client).to be_a(Async::HTTP::Middleware::LocationRedirector)
end
end

with "#make_client" do
it "caches the client" do
endpoint = Async::HTTP::Endpoint.parse('http://example.com')
Expand Down

0 comments on commit 4e52a80

Please sign in to comment.