Skip to content

Commit

Permalink
Re-add register middleware w/ Symbol, String, Proc
Browse files Browse the repository at this point in the history
Reintroduce the possibility to register middleware with symbols, strings or procs.

Fixes #1389
  • Loading branch information
iMacTia authored and olleolleolle committed Feb 2, 2022
1 parent 33563e3 commit be93a4c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
6 changes: 5 additions & 1 deletion UPGRADING.md
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
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
30 changes: 30 additions & 0 deletions spec/faraday/middleware_registry_spec.rb
@@ -0,0 +1,30 @@
# frozen_string_literal: true

class CustomMiddleware < Faraday::Middleware
end

RSpec.describe Faraday::MiddlewareRegistry do
let(:dummy) { Class.new { extend Faraday::MiddlewareRegistry } }

after { dummy.unregister_middleware(:custom) }

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

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

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

it 'allows to register with Proc' do
dummy.register_middleware(custom: -> { CustomMiddleware })
expect(dummy.lookup_middleware(:custom)).to eq(CustomMiddleware)
end
end
12 changes: 0 additions & 12 deletions spec/faraday/rack_builder_spec.rb
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

0 comments on commit be93a4c

Please sign in to comment.