Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reintroduce the possibility to register middleware with symbols, strings or procs #1391

Merged
merged 2 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ We did our best to make this transition as painless as possible for you, so here
# Gemfile
gem 'faraday'
gem 'faraday-net_http_persistent'

# Code
require 'faraday'
require 'faraday/net_http_persistent'
Expand Down Expand Up @@ -102,6 +102,10 @@ This change should not affect you directly, but if you're registering middleware
Faraday::Middleware.register_middleware(name: klass)
```

The `register_middleware` method also previously allowed to provide a symbol, string, proc, or array
but this has been removed from the v2.0 release to simplify the interface.
(EDIT: symbol/string/proc have subsequently been reintroduced in v2.2, but not the array).

### Authentication helper methods in Connection have been removed

You were previously able to call `authorization`, `basic_auth` and `token_auth` against the `Connection` object, but this helper methods have now been dropped.
Expand Down
20 changes: 19 additions & 1 deletion lib/faraday/middleware_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,31 @@ def unregister_middleware(key)
# Faraday::Middleware.lookup_middleware(:foo)
# # => Faraday::Whatever
def lookup_middleware(key)
registered_middleware[key] ||
load_middleware(key) ||
raise(Faraday::Error, "#{key.inspect} is not registered on #{self}")
end

private

def middleware_mutex(&block)
@middleware_mutex ||= Monitor.new
@middleware_mutex.synchronize(&block)
end

def load_middleware(key)
value = registered_middleware[key]
case value
when Module
value
when Symbol, String
middleware_mutex do
@registered_middleware[key] = const_get(value)
end
when Proc
middleware_mutex do
@registered_middleware[key] = value.call
end
end
end
end
end
31 changes: 31 additions & 0 deletions spec/faraday/middleware_registry_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

RSpec.describe Faraday::MiddlewareRegistry do
before do
stub_const('CustomMiddleware', custom_middleware_klass)
end
let(:custom_middleware_klass) { Class.new(Faraday::Middleware) }
let(:dummy) { Class.new { extend Faraday::MiddlewareRegistry } }

after { dummy.unregister_middleware(:custom) }

it 'allows to register with constant' do
dummy.register_middleware(custom: custom_middleware_klass)
expect(dummy.lookup_middleware(:custom)).to eq(custom_middleware_klass)
end

it 'allows to register with symbol' do
dummy.register_middleware(custom: :CustomMiddleware)
expect(dummy.lookup_middleware(:custom)).to eq(custom_middleware_klass)
end

it 'allows to register with string' do
dummy.register_middleware(custom: 'CustomMiddleware')
expect(dummy.lookup_middleware(:custom)).to eq(custom_middleware_klass)
end

it 'allows to register with Proc' do
dummy.register_middleware(custom: -> { custom_middleware_klass })
expect(dummy.lookup_middleware(:custom)).to eq(custom_middleware_klass)
end
end
12 changes: 0 additions & 12 deletions spec/faraday/rack_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,6 @@ class Banana < Handler
end
end

context 'with custom registered middleware' do
let(:conn) { Faraday::Connection.new {} }

after { Faraday::Middleware.unregister_middleware(:apple) }

it 'allows to register with constant' do
Faraday::Middleware.register_middleware(apple: Apple)
subject.use(:apple)
expect(subject.handlers).to eq([Apple])
end
end

context 'when having two handlers' do
let(:conn) { Faraday::Connection.new {} }

Expand Down