Skip to content

Commit d75a893

Browse files
iMacTiajrochkind
authored andcommitted
Remove net-http adapter and update docs (lostisland#1336)
1 parent 7d15a00 commit d75a893

File tree

6 files changed

+94
-47
lines changed

6 files changed

+94
-47
lines changed

README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
[![GitHub Discussions](https://img.shields.io/github/discussions/lostisland/faraday?logo=github)](https://github.com/lostisland/faraday/discussions)
66

77

8-
Faraday is an HTTP client library that provides a common interface over many
9-
adapters (such as Net::HTTP) and embraces the concept of Rack middleware when
10-
processing the request/response cycle.
8+
Faraday is an HTTP client library abstraction layer that provides a common interface over many
9+
adapters (such as Net::HTTP) and embraces the concept of Rack middleware when processing the request/response cycle.
10+
You probably don't want to use Faraday directly in your project, as it will lack an actual client library to perform
11+
requests. Instead, you probably want to have a look at [Awesome Faraday][awesome] for a list of available adapters.
1112

12-
## ATTENTION
13+
## FARADAY 2.0
1314

14-
You're reading the README and looking at the code of our upcoming v2.0 release (the `main` branch).
15+
You're reading the README and looking at the code of our upcoming v2.0 release (the `main` branch, currently in alpha).
1516
If you're here to read about our latest v1.x release, then please head over to the [1.x branch](https://github.com/lostisland/faraday/tree/1.x).
1617

1718
## Getting Started
@@ -48,6 +49,7 @@ But before you start coding, please read our [Contributing Guide][contributing]
4849
## Copyright
4950
© 2009 - 2021, the [Faraday Team][faraday_team]. Website and branding design by [Elena Lo Piccolo](https://elelopic.design).
5051

52+
[awesome]: https://github.com/lostisland/awesome-faraday/#adapters
5153
[website]: https://lostisland.github.io/faraday
5254
[faraday_team]: https://lostisland.github.io/faraday/team
5355
[contributing]: https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md

UPGRADING.md

+13
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ We did our best to make this transition as painless as possible for you, so here
3131
* We've setup an [Awesome Faraday](https://github.com/lostisland/awesome-faraday) repository, where you can find and discover adapters.
3232
We also highlighted their unique features and level of compliance with Faraday's features.
3333

34+
#### That's great! What should I change in my code immediately after upgrading?
35+
36+
* Add the corresponding adapter gem to your Gemfile (e.g. `faraday-net_http`). Ideally, this should replace
37+
`faraday` altogether as these gems usually have Faraday already in their dependencies.
38+
* If you're relying on `Faraday.default_adapter` (e.g. if you use `Faraday.get` or other verb class methods, or not
39+
specifying an adapter in your connection initializer), then you'll now need to set it yourself. It previously
40+
defaulted to `:net_http`, but it now defaults to `:test`. You can do so simply by using the setter:
41+
42+
```ruby
43+
# For example, to use net_http (previous default value, will now require `gem 'faraday-net_http'` in your gemfile)
44+
Faraday.default_adapter = :net_http
45+
```
46+
3447
### Autoloading and dependencies
3548

3649
Faraday has until now provided and relied on a complex dynamic dependencies system.

faraday.gemspec

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Gem::Specification.new do |spec|
1515

1616
spec.required_ruby_version = '>= 2.6'
1717

18-
spec.add_dependency 'faraday-net_http', '~> 1.0'
1918
spec.add_dependency 'multipart-post', '>= 1.2', '< 3'
2019
spec.add_dependency 'ruby2_keywords', '>= 0.0.4'
2120

lib/faraday.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class << self
4949

5050
# @overload default_adapter
5151
# Gets the Symbol key identifying a default Adapter to use
52-
# for the default {Faraday::Connection}. Defaults to `:net_http`.
52+
# for the default {Faraday::Connection}. Defaults to `:test`.
5353
# @return [Symbol] the default adapter
5454
# @overload default_adapter=(adapter)
5555
# Updates default adapter while resetting {.default_connection}.
@@ -150,5 +150,5 @@ def method_missing(name, *args, &block)
150150
self.ignore_env_proxy = false
151151
self.root_path = File.expand_path __dir__
152152
self.lib_path = File.expand_path 'faraday', __dir__
153-
self.default_adapter = :net_http
153+
self.default_adapter = :test
154154
end

spec/faraday/adapter/net_http_spec.rb

-11
This file was deleted.

spec/faraday/connection_spec.rb

+72-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# frozen_string_literal: true
22

3+
class CustomEncoder
4+
def encode(params)
5+
params.map { |k, v| "#{k.upcase}-#{v.to_s.upcase}" }.join(',')
6+
end
7+
8+
def decode(params)
9+
params.split(',').map { |pair| pair.split('-') }.to_h
10+
end
11+
end
12+
313
shared_examples 'initializer with url' do
414
context 'with simple url' do
515
let(:address) { 'http://sushi.com' }
@@ -130,7 +140,7 @@
130140
context 'with block' do
131141
let(:conn) do
132142
Faraday::Connection.new(params: { 'a' => '1' }) do |faraday|
133-
faraday.adapter :net_http
143+
faraday.adapter :test
134144
faraday.url_prefix = 'http://sushi.com/omnom'
135145
end
136146
end
@@ -540,26 +550,32 @@
540550
end
541551

542552
context 'performing a request' do
543-
before { stub_request(:get, 'http://example.com') }
553+
let(:url) { 'http://example.com' }
554+
let(:conn) do
555+
Faraday.new do |f|
556+
f.adapter :test do |stubs|
557+
stubs.get(url) do
558+
[200, {}, 'ok']
559+
end
560+
end
561+
end
562+
end
544563

545564
it 'dynamically checks proxy' do
546565
with_env 'http_proxy' => 'http://proxy.com:80' do
547-
conn = Faraday.new
548566
expect(conn.proxy.uri.host).to eq('proxy.com')
549567

550-
conn.get('http://example.com') do |req|
568+
conn.get(url) do |req|
551569
expect(req.options.proxy.uri.host).to eq('proxy.com')
552570
end
553571
end
554572

555-
conn.get('http://example.com')
573+
conn.get(url)
556574
expect(conn.instance_variable_get('@temp_proxy')).to be_nil
557575
end
558576

559577
it 'dynamically check no proxy' do
560578
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
561-
conn = Faraday.new
562-
563579
expect(conn.proxy.uri.host).to eq('proxy.com')
564580

565581
conn.get('http://example.com') do |req|
@@ -628,9 +644,16 @@
628644
describe 'request params' do
629645
context 'with simple url' do
630646
let(:url) { 'http://example.com' }
631-
let!(:stubbed) { stub_request(:get, 'http://example.com?a=a&p=3') }
647+
let(:stubs) { Faraday::Adapter::Test::Stubs.new }
648+
649+
before do
650+
conn.adapter(:test, stubs)
651+
stubs.get('http://example.com?a=a&p=3') do
652+
[200, {}, 'ok']
653+
end
654+
end
632655

633-
after { expect(stubbed).to have_been_made.once }
656+
after { stubs.verify_stubbed_calls }
634657

635658
it 'test_overrides_request_params' do
636659
conn.get('?p=2&a=a', p: 3)
@@ -652,63 +675,84 @@
652675
context 'with url and extra params' do
653676
let(:url) { 'http://example.com?a=1&b=2' }
654677
let(:options) { { params: { c: 3 } } }
678+
let(:stubs) { Faraday::Adapter::Test::Stubs.new }
679+
680+
before do
681+
conn.adapter(:test, stubs)
682+
end
655683

656684
it 'merges connection and request params' do
657-
stubbed = stub_request(:get, 'http://example.com?a=1&b=2&c=3&limit=5&page=1')
685+
expected = 'http://example.com?a=1&b=2&c=3&limit=5&page=1'
686+
stubs.get(expected) { [200, {}, 'ok'] }
658687
conn.get('?page=1', limit: 5)
659-
expect(stubbed).to have_been_made.once
688+
stubs.verify_stubbed_calls
660689
end
661690

662691
it 'allows to override all params' do
663-
stubbed = stub_request(:get, 'http://example.com?b=b')
692+
expected = 'http://example.com?b=b'
693+
stubs.get(expected) { [200, {}, 'ok'] }
664694
conn.get('?p=1&a=a', p: 2) do |req|
665695
expect(req.params[:a]).to eq('a')
666696
expect(req.params['c']).to eq(3)
667697
expect(req.params['p']).to eq(2)
668698
req.params = { b: 'b' }
669699
expect(req.params['b']).to eq('b')
670700
end
671-
expect(stubbed).to have_been_made.once
701+
stubs.verify_stubbed_calls
672702
end
673703

674704
it 'allows to set params_encoder for single request' do
675-
encoder = Object.new
676-
def encoder.encode(params)
677-
params.map { |k, v| "#{k.upcase}-#{v.to_s.upcase}" }.join(',')
678-
end
679-
stubbed = stub_request(:get, 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE')
705+
encoder = CustomEncoder.new
706+
expected = 'http://example.com/?A-1,B-2,C-3,FEELING-BLUE'
707+
stubs.get(expected) { [200, {}, 'ok'] }
680708

681-
conn.get('/', feeling: 'blue') do |req|
709+
conn.get('/', a: 1, b: 2, c: 3, feeling: 'blue') do |req|
682710
req.options.params_encoder = encoder
683711
end
684-
expect(stubbed).to have_been_made.once
712+
stubs.verify_stubbed_calls
685713
end
686714
end
687715

688716
context 'with default params encoder' do
689-
let!(:stubbed) { stub_request(:get, 'http://example.com?color%5B%5D=red&color%5B%5D=blue') }
690-
after { expect(stubbed).to have_been_made.once }
717+
let(:stubs) { Faraday::Adapter::Test::Stubs.new }
718+
719+
before do
720+
conn.adapter(:test, stubs)
721+
stubs.get('http://example.com?color%5B%5D=blue&color%5B%5D=red') do
722+
[200, {}, 'ok']
723+
end
724+
end
725+
726+
after { stubs.verify_stubbed_calls }
691727

692728
it 'supports array params in url' do
693-
conn.get('http://example.com?color[]=red&color[]=blue')
729+
conn.get('http://example.com?color[]=blue&color[]=red')
694730
end
695731

696732
it 'supports array params in params' do
697-
conn.get('http://example.com', color: %w[red blue])
733+
conn.get('http://example.com', color: %w[blue red])
698734
end
699735
end
700736

701737
context 'with flat params encoder' do
702738
let(:options) { { request: { params_encoder: Faraday::FlatParamsEncoder } } }
703-
let!(:stubbed) { stub_request(:get, 'http://example.com?color=blue') }
704-
after { expect(stubbed).to have_been_made.once }
739+
let(:stubs) { Faraday::Adapter::Test::Stubs.new }
740+
741+
before do
742+
conn.adapter(:test, stubs)
743+
stubs.get('http://example.com?color=blue&color=red') do
744+
[200, {}, 'ok']
745+
end
746+
end
747+
748+
after { stubs.verify_stubbed_calls }
705749

706750
it 'supports array params in params' do
707-
conn.get('http://example.com', color: %w[red blue])
751+
conn.get('http://example.com', color: %w[blue red])
708752
end
709753

710754
context 'with array param in url' do
711-
let(:url) { 'http://example.com?color[]=red&color[]=blue' }
755+
let(:url) { 'http://example.com?color[]=blue&color[]=red' }
712756

713757
it do
714758
conn.get('/')

0 commit comments

Comments
 (0)