Skip to content

Commit

Permalink
* Reintroduce the possibility to register middleware with symbols, st…
Browse files Browse the repository at this point in the history
…rings or procs

* Update UPGRADING.md to explain the original removal from v2.0
* Update CHANGELOG.md to point to the `releases` page

Fixes #1389
  • Loading branch information
iMacTia committed Feb 2, 2022
1 parent 26d47ca commit ae5a31c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 17 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Faraday Changelog

## The changelog has moved!
This file is not being updated anymore. Instead, please check
the [releases](https://github.com/lostisland/faraday/releases) page.

## [1.8.0](https://github.com/lostisland/faraday/releases/tag/v1.8.0) (2021-09-18)

### Features
Expand Down Expand Up @@ -65,7 +69,7 @@

### Highlights

With this release, we continue the work of gradually moving out adapters into their own gems 🎉
With this release, we continue the work of gradually moving out adapters into their own gems 🎉
Thanks to @MikeRogers0 for helping the Faraday team in progressing with this quest 👏

And thanks to @olleolleolle efforts, Faraday is becoming more inclusive than ever 🤗
Expand Down Expand Up @@ -112,7 +116,7 @@ Checkout the "Misc" section below for more details 🙌 !
## [v1.3.0](https://github.com/lostisland/faraday/releases/tag/v1.3.0) (2020-12-31)

### Highlights
Faraday v1.3.0 is the first release to officially support Ruby 3.0 in the CI pipeline 🎉 🍾!
Faraday v1.3.0 is the first release to officially support Ruby 3.0 in the CI pipeline 🎉 🍾!

This is also the first release with a previously "included" adapter (Net::HTTP) being isolated into a [separate gem](https://github.com/lostisland/faraday-net_http) 🎊!
The new adapter is added to Faraday as a dependency for now, so that means full backwards-compatibility, but just to be safe be careful when upgrading!
Expand Down Expand Up @@ -203,7 +207,7 @@ Many thanks to the Faraday Team, @JanDintel and everyone who attended the [ROSS
* Allows `parse` method to be private/protected in response middleware (#1123)
* Encode Spaces in Query Strings as '%20' Instead of '+' (#1125)
* Limits rack to v2.0.x (#1127)
* Adapter Registry reads also use mutex (#1136)
* Adapter Registry reads also use mutex (#1136)

### Documentation

Expand Down
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
30 changes: 30 additions & 0 deletions spec/faraday/middleware_registry_spec.rb
Original file line number Diff line number Diff line change
@@ -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
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

0 comments on commit ae5a31c

Please sign in to comment.