diff --git a/README.md b/README.md index b93443b18..87d96e04c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/UPGRADING.md b/UPGRADING.md index 3912aff45..1dd727482 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -31,6 +31,19 @@ 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`. You can do so simply by using the setter: + + ```ruby + # For example, to use net_http (previous default value, will now require `gem 'faraday-net_http'` in your gemfile) + Faraday.default_adapter = :net_http + ``` + ### Autoloading and dependencies Faraday has until now provided and relied on a complex dynamic dependencies system. diff --git a/faraday.gemspec b/faraday.gemspec index c343c2d90..11ce82812 100644 --- a/faraday.gemspec +++ b/faraday.gemspec @@ -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' diff --git a/lib/faraday.rb b/lib/faraday.rb index 9eb7778e7..92cb32301 100644 --- a/lib/faraday.rb +++ b/lib/faraday.rb @@ -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}. @@ -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 diff --git a/spec/faraday/adapter/net_http_spec.rb b/spec/faraday/adapter/net_http_spec.rb deleted file mode 100644 index 8e8f88d52..000000000 --- a/spec/faraday/adapter/net_http_spec.rb +++ /dev/null @@ -1,11 +0,0 @@ -# frozen_string_literal: true - -require 'faraday/net_http' - -# Even though Faraday::Adapter::NetHttp is not shipped with Faraday anymore, -# this is still useful to test `it_behaves_like 'an adapter'` shared examples. -RSpec.describe Faraday::Adapter::NetHttp do - features :request_body_on_query_methods, :reason_phrase_parse, :compression, :streaming, :trace_method - - it_behaves_like 'an adapter' -end diff --git a/spec/faraday/connection_spec.rb b/spec/faraday/connection_spec.rb index 3b88035f4..3008ef4c0 100644 --- a/spec/faraday/connection_spec.rb +++ b/spec/faraday/connection_spec.rb @@ -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' } @@ -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 @@ -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| @@ -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 } + + 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) @@ -652,15 +675,22 @@ 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) @@ -668,47 +698,61 @@ 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('/')