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

Remove net-http adapter and update docs #1336

Merged
merged 2 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
[![GitHub Discussions](https://img.shields.io/github/discussions/lostisland/faraday?logo=github)](https://github.com/lostisland/faraday/discussions)


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

## ATTENTION
## FARADAY 2.0

You're reading the README and looking at the code of our upcoming v2.0 release (the `main` branch).
You're reading the README and looking at the code of our upcoming v2.0 release (the `main` branch, currently in alpha).
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).

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

[awesome]: https://github.com/lostisland/awesome-faraday/#adapters
[website]: https://lostisland.github.io/faraday
[faraday_team]: https://lostisland.github.io/faraday/team
[contributing]: https://github.com/lostisland/faraday/blob/master/.github/CONTRIBUTING.md
Expand Down
8 changes: 8 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ We did our best to make this transition as painless as possible for you, so here
* We've setup an [Awesome Faraday](https://github.com/lostisland/awesome-faraday) repository, where you can find and discover adapters.
We also highlighted their unique features and level of compliance with Faraday's features.

#### That's great! What should I change in my code immediately after upgrading?

* Add the corresponding adapter gem to your Gemfile (e.g. `faraday-net_http`). Ideally, this should replace
`faraday` altogether as these gems usually have Faraday already in their dependencies.
* If you're relying on `Faraday.default_adapter` (e.g. if you use `Faraday.get` or other verb class methods, or not
specifying an adapter in your connection initializer), then you'll now need to set it yourself. It previously
defaulted to `:net_http`, but it now defaults to `:test`.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this section could have a pasteable example of doing what the text says?

Suggested change
Faraday.default_adapter = :net_http

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Well, and some "you could do this" text.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know you could have multi-line code snippets inside list items!!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every day is a school day!

### Autoloading and dependencies

Faraday has until now provided and relied on a complex dynamic dependencies system.
Expand Down
1 change: 0 additions & 1 deletion faraday.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = '>= 2.6'

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

Expand Down
4 changes: 2 additions & 2 deletions lib/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class << self

# @overload default_adapter
# Gets the Symbol key identifying a default Adapter to use
# for the default {Faraday::Connection}. Defaults to `:net_http`.
# for the default {Faraday::Connection}. Defaults to `:test`.
# @return [Symbol] the default adapter
# @overload default_adapter=(adapter)
# Updates default adapter while resetting {.default_connection}.
Expand Down Expand Up @@ -150,5 +150,5 @@ def method_missing(name, *args, &block)
self.ignore_env_proxy = false
self.root_path = File.expand_path __dir__
self.lib_path = File.expand_path 'faraday', __dir__
self.default_adapter = :net_http
self.default_adapter = :test
end
11 changes: 0 additions & 11 deletions spec/faraday/adapter/net_http_spec.rb

This file was deleted.

100 changes: 72 additions & 28 deletions spec/faraday/connection_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# frozen_string_literal: true

class CustomEncoder
def encode(params)
params.map { |k, v| "#{k.upcase}-#{v.to_s.upcase}" }.join(',')
end

def decode(params)
params.split(',').map { |pair| pair.split('-') }.to_h
end
end

shared_examples 'initializer with url' do
context 'with simple url' do
let(:address) { 'http://sushi.com' }
Expand Down Expand Up @@ -130,7 +140,7 @@
context 'with block' do
let(:conn) do
Faraday::Connection.new(params: { 'a' => '1' }) do |faraday|
faraday.adapter :net_http
faraday.adapter :test
faraday.url_prefix = 'http://sushi.com/omnom'
end
end
Expand Down Expand Up @@ -540,26 +550,32 @@
end

context 'performing a request' do
before { stub_request(:get, 'http://example.com') }
let(:url) { 'http://example.com' }
let(:conn) do
Faraday.new do |f|
f.adapter :test do |stubs|
stubs.get(url) do
[200, {}, 'ok']
end
end
end
end

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

conn.get('http://example.com') do |req|
conn.get(url) do |req|
expect(req.options.proxy.uri.host).to eq('proxy.com')
end
end

conn.get('http://example.com')
conn.get(url)
expect(conn.instance_variable_get('@temp_proxy')).to be_nil
end

it 'dynamically check no proxy' do
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
conn = Faraday.new

expect(conn.proxy.uri.host).to eq('proxy.com')

conn.get('http://example.com') do |req|
Expand Down Expand Up @@ -628,9 +644,16 @@
describe 'request params' do
context 'with simple url' do
let(:url) { 'http://example.com' }
let!(:stubbed) { stub_request(:get, 'http://example.com?a=a&p=3') }
let(:stubs) { Faraday::Adapter::Test::Stubs.new }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sparkling improvement!


before do
conn.adapter(:test, stubs)
stubs.get('http://example.com?a=a&p=3') do
[200, {}, 'ok']
end
end

after { expect(stubbed).to have_been_made.once }
after { stubs.verify_stubbed_calls }

it 'test_overrides_request_params' do
conn.get('?p=2&a=a', p: 3)
Expand All @@ -652,63 +675,84 @@
context 'with url and extra params' do
let(:url) { 'http://example.com?a=1&b=2' }
let(:options) { { params: { c: 3 } } }
let(:stubs) { Faraday::Adapter::Test::Stubs.new }

before do
conn.adapter(:test, stubs)
end

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

it 'allows to override all params' do
stubbed = stub_request(:get, 'http://example.com?b=b')
expected = 'http://example.com?b=b'
stubs.get(expected) { [200, {}, 'ok'] }
conn.get('?p=1&a=a', p: 2) do |req|
expect(req.params[:a]).to eq('a')
expect(req.params['c']).to eq(3)
expect(req.params['p']).to eq(2)
req.params = { b: 'b' }
expect(req.params['b']).to eq('b')
end
expect(stubbed).to have_been_made.once
stubs.verify_stubbed_calls
end

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

conn.get('/', feeling: 'blue') do |req|
conn.get('/', a: 1, b: 2, c: 3, feeling: 'blue') do |req|
req.options.params_encoder = encoder
end
expect(stubbed).to have_been_made.once
stubs.verify_stubbed_calls
end
end

context 'with default params encoder' do
let!(:stubbed) { stub_request(:get, 'http://example.com?color%5B%5D=red&color%5B%5D=blue') }
after { expect(stubbed).to have_been_made.once }
let(:stubs) { Faraday::Adapter::Test::Stubs.new }

before do
conn.adapter(:test, stubs)
stubs.get('http://example.com?color%5B%5D=blue&color%5B%5D=red') do
[200, {}, 'ok']
end
end

after { stubs.verify_stubbed_calls }

it 'supports array params in url' do
conn.get('http://example.com?color[]=red&color[]=blue')
conn.get('http://example.com?color[]=blue&color[]=red')
end

it 'supports array params in params' do
conn.get('http://example.com', color: %w[red blue])
conn.get('http://example.com', color: %w[blue red])
end
end

context 'with flat params encoder' do
let(:options) { { request: { params_encoder: Faraday::FlatParamsEncoder } } }
let!(:stubbed) { stub_request(:get, 'http://example.com?color=blue') }
after { expect(stubbed).to have_been_made.once }
let(:stubs) { Faraday::Adapter::Test::Stubs.new }

before do
conn.adapter(:test, stubs)
stubs.get('http://example.com?color=blue&color=red') do
[200, {}, 'ok']
end
end

after { stubs.verify_stubbed_calls }

it 'supports array params in params' do
conn.get('http://example.com', color: %w[red blue])
conn.get('http://example.com', color: %w[blue red])
end

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

it do
conn.get('/')
Expand Down