faraday 1.8.0 → 2.0.0.alpha.pre.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
 - data/CHANGELOG.md +111 -1
 - data/README.md +16 -9
 - data/examples/client_test.rb +1 -1
 - data/lib/faraday/adapter/test.rb +2 -0
 - data/lib/faraday/adapter.rb +0 -5
 - data/lib/faraday/connection.rb +3 -84
 - data/lib/faraday/encoders/nested_params_encoder.rb +2 -2
 - data/lib/faraday/error.rb +1 -0
 - data/lib/faraday/file_part.rb +0 -6
 - data/lib/faraday/logging/formatter.rb +1 -0
 - data/lib/faraday/middleware.rb +0 -1
 - data/lib/faraday/middleware_registry.rb +15 -79
 - data/lib/faraday/options.rb +3 -3
 - data/lib/faraday/rack_builder.rb +1 -1
 - data/lib/faraday/request/authorization.rb +26 -40
 - data/lib/faraday/request/instrumentation.rb +2 -0
 - data/lib/faraday/request/json.rb +55 -0
 - data/lib/faraday/request/multipart.rb +2 -0
 - data/lib/faraday/request/retry.rb +3 -1
 - data/lib/faraday/request/url_encoded.rb +2 -0
 - data/lib/faraday/request.rb +13 -31
 - data/lib/faraday/response/json.rb +54 -0
 - data/lib/faraday/response/logger.rb +4 -4
 - data/lib/faraday/response/raise_error.rb +9 -1
 - data/lib/faraday/response.rb +8 -19
 - data/lib/faraday/utils/headers.rb +1 -1
 - data/lib/faraday/utils.rb +9 -4
 - data/lib/faraday/version.rb +1 -1
 - data/lib/faraday.rb +6 -37
 - data/spec/faraday/connection_spec.rb +78 -51
 - data/spec/faraday/options/env_spec.rb +2 -2
 - data/spec/faraday/rack_builder_spec.rb +5 -43
 - data/spec/faraday/request/authorization_spec.rb +14 -36
 - data/spec/faraday/request/instrumentation_spec.rb +5 -7
 - data/spec/faraday/request/json_spec.rb +111 -0
 - data/spec/faraday/request/multipart_spec.rb +5 -5
 - data/spec/faraday/request/retry_spec.rb +13 -1
 - data/spec/faraday/request_spec.rb +0 -11
 - data/spec/faraday/response/json_spec.rb +117 -0
 - data/spec/faraday/response/raise_error_spec.rb +7 -4
 - data/spec/faraday/utils_spec.rb +1 -1
 - data/spec/support/fake_safe_buffer.rb +1 -1
 - data/spec/support/shared_examples/request_method.rb +5 -5
 - metadata +11 -134
 - data/lib/faraday/adapter/typhoeus.rb +0 -15
 - data/lib/faraday/autoload.rb +0 -87
 - data/lib/faraday/dependency_loader.rb +0 -37
 - data/lib/faraday/request/basic_authentication.rb +0 -20
 - data/lib/faraday/request/token_authentication.rb +0 -20
 - data/spec/faraday/adapter/em_http_spec.rb +0 -49
 - data/spec/faraday/adapter/em_synchrony_spec.rb +0 -18
 - data/spec/faraday/adapter/excon_spec.rb +0 -49
 - data/spec/faraday/adapter/httpclient_spec.rb +0 -73
 - data/spec/faraday/adapter/net_http_spec.rb +0 -64
 - data/spec/faraday/adapter/patron_spec.rb +0 -18
 - data/spec/faraday/adapter/rack_spec.rb +0 -8
 - data/spec/faraday/adapter/typhoeus_spec.rb +0 -7
 - data/spec/faraday/response/middleware_spec.rb +0 -68
 - data/spec/support/webmock_rack_app.rb +0 -68
 
| 
         @@ -1,68 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            # frozen_string_literal: true
         
     | 
| 
       2 
     | 
    
         
            -
             
     | 
| 
       3 
     | 
    
         
            -
            RSpec.describe Faraday::Response::Middleware do
         
     | 
| 
       4 
     | 
    
         
            -
              let(:conn) do
         
     | 
| 
       5 
     | 
    
         
            -
                Faraday.new do |b|
         
     | 
| 
       6 
     | 
    
         
            -
                  b.use custom_middleware
         
     | 
| 
       7 
     | 
    
         
            -
                  b.adapter :test do |stub|
         
     | 
| 
       8 
     | 
    
         
            -
                    stub.get('ok') { [200, { 'Content-Type' => 'text/html' }, '<body></body>'] }
         
     | 
| 
       9 
     | 
    
         
            -
                    stub.get('not_modified') { [304, nil, nil] }
         
     | 
| 
       10 
     | 
    
         
            -
                    stub.get('no_content') { [204, nil, nil] }
         
     | 
| 
       11 
     | 
    
         
            -
                  end
         
     | 
| 
       12 
     | 
    
         
            -
                end
         
     | 
| 
       13 
     | 
    
         
            -
              end
         
     | 
| 
       14 
     | 
    
         
            -
             
     | 
| 
       15 
     | 
    
         
            -
              context 'with a custom ResponseMiddleware' do
         
     | 
| 
       16 
     | 
    
         
            -
                let(:custom_middleware) do
         
     | 
| 
       17 
     | 
    
         
            -
                  Class.new(Faraday::Response::Middleware) do
         
     | 
| 
       18 
     | 
    
         
            -
                    def parse(body)
         
     | 
| 
       19 
     | 
    
         
            -
                      body.upcase
         
     | 
| 
       20 
     | 
    
         
            -
                    end
         
     | 
| 
       21 
     | 
    
         
            -
                  end
         
     | 
| 
       22 
     | 
    
         
            -
                end
         
     | 
| 
       23 
     | 
    
         
            -
             
     | 
| 
       24 
     | 
    
         
            -
                it 'parses the response' do
         
     | 
| 
       25 
     | 
    
         
            -
                  expect(conn.get('ok').body).to eq('<BODY></BODY>')
         
     | 
| 
       26 
     | 
    
         
            -
                end
         
     | 
| 
       27 
     | 
    
         
            -
              end
         
     | 
| 
       28 
     | 
    
         
            -
             
     | 
| 
       29 
     | 
    
         
            -
              context 'with a custom ResponseMiddleware and private parse' do
         
     | 
| 
       30 
     | 
    
         
            -
                let(:custom_middleware) do
         
     | 
| 
       31 
     | 
    
         
            -
                  Class.new(Faraday::Response::Middleware) do
         
     | 
| 
       32 
     | 
    
         
            -
                    private
         
     | 
| 
       33 
     | 
    
         
            -
             
     | 
| 
       34 
     | 
    
         
            -
                    def parse(body)
         
     | 
| 
       35 
     | 
    
         
            -
                      body.upcase
         
     | 
| 
       36 
     | 
    
         
            -
                    end
         
     | 
| 
       37 
     | 
    
         
            -
                  end
         
     | 
| 
       38 
     | 
    
         
            -
                end
         
     | 
| 
       39 
     | 
    
         
            -
             
     | 
| 
       40 
     | 
    
         
            -
                it 'parses the response' do
         
     | 
| 
       41 
     | 
    
         
            -
                  expect(conn.get('ok').body).to eq('<BODY></BODY>')
         
     | 
| 
       42 
     | 
    
         
            -
                end
         
     | 
| 
       43 
     | 
    
         
            -
              end
         
     | 
| 
       44 
     | 
    
         
            -
             
     | 
| 
       45 
     | 
    
         
            -
              context 'with a custom ResponseMiddleware but empty response' do
         
     | 
| 
       46 
     | 
    
         
            -
                let(:custom_middleware) do
         
     | 
| 
       47 
     | 
    
         
            -
                  Class.new(Faraday::Response::Middleware) do
         
     | 
| 
       48 
     | 
    
         
            -
                    def parse(_body)
         
     | 
| 
       49 
     | 
    
         
            -
                      raise 'this should not be called'
         
     | 
| 
       50 
     | 
    
         
            -
                    end
         
     | 
| 
       51 
     | 
    
         
            -
                  end
         
     | 
| 
       52 
     | 
    
         
            -
                end
         
     | 
| 
       53 
     | 
    
         
            -
             
     | 
| 
       54 
     | 
    
         
            -
                it 'raises exception for 200 responses' do
         
     | 
| 
       55 
     | 
    
         
            -
                  expect { conn.get('ok') }.to raise_error(StandardError)
         
     | 
| 
       56 
     | 
    
         
            -
                end
         
     | 
| 
       57 
     | 
    
         
            -
             
     | 
| 
       58 
     | 
    
         
            -
                it 'doesn\'t call the middleware for 204 responses' do
         
     | 
| 
       59 
     | 
    
         
            -
                  expect_any_instance_of(custom_middleware).not_to receive(:parse)
         
     | 
| 
       60 
     | 
    
         
            -
                  expect(conn.get('no_content').body).to be_nil
         
     | 
| 
       61 
     | 
    
         
            -
                end
         
     | 
| 
       62 
     | 
    
         
            -
             
     | 
| 
       63 
     | 
    
         
            -
                it 'doesn\'t call the middleware for 304 responses' do
         
     | 
| 
       64 
     | 
    
         
            -
                  expect_any_instance_of(custom_middleware).not_to receive(:parse)
         
     | 
| 
       65 
     | 
    
         
            -
                  expect(conn.get('not_modified').body).to be_nil
         
     | 
| 
       66 
     | 
    
         
            -
                end
         
     | 
| 
       67 
     | 
    
         
            -
              end
         
     | 
| 
       68 
     | 
    
         
            -
            end
         
     | 
| 
         @@ -1,68 +0,0 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            # frozen_string_literal: true
         
     | 
| 
       2 
     | 
    
         
            -
             
     | 
| 
       3 
     | 
    
         
            -
            # Rack app used to test the Rack adapter.
         
     | 
| 
       4 
     | 
    
         
            -
            # Uses Webmock to check if requests are registered, in which case it returns
         
     | 
| 
       5 
     | 
    
         
            -
            # the registered response.
         
     | 
| 
       6 
     | 
    
         
            -
            class WebmockRackApp
         
     | 
| 
       7 
     | 
    
         
            -
              def call(env)
         
     | 
| 
       8 
     | 
    
         
            -
                req_signature = WebMock::RequestSignature.new(
         
     | 
| 
       9 
     | 
    
         
            -
                  req_method(env),
         
     | 
| 
       10 
     | 
    
         
            -
                  req_uri(env),
         
     | 
| 
       11 
     | 
    
         
            -
                  body: req_body(env),
         
     | 
| 
       12 
     | 
    
         
            -
                  headers: req_headers(env)
         
     | 
| 
       13 
     | 
    
         
            -
                )
         
     | 
| 
       14 
     | 
    
         
            -
             
     | 
| 
       15 
     | 
    
         
            -
                WebMock::RequestRegistry
         
     | 
| 
       16 
     | 
    
         
            -
                  .instance
         
     | 
| 
       17 
     | 
    
         
            -
                  .requested_signatures
         
     | 
| 
       18 
     | 
    
         
            -
                  .put(req_signature)
         
     | 
| 
       19 
     | 
    
         
            -
             
     | 
| 
       20 
     | 
    
         
            -
                process_response(req_signature)
         
     | 
| 
       21 
     | 
    
         
            -
              end
         
     | 
| 
       22 
     | 
    
         
            -
             
     | 
| 
       23 
     | 
    
         
            -
              def req_method(env)
         
     | 
| 
       24 
     | 
    
         
            -
                env['REQUEST_METHOD'].downcase.to_sym
         
     | 
| 
       25 
     | 
    
         
            -
              end
         
     | 
| 
       26 
     | 
    
         
            -
             
     | 
| 
       27 
     | 
    
         
            -
              def req_uri(env)
         
     | 
| 
       28 
     | 
    
         
            -
                scheme = env['rack.url_scheme']
         
     | 
| 
       29 
     | 
    
         
            -
                host = env['SERVER_NAME']
         
     | 
| 
       30 
     | 
    
         
            -
                port = env['SERVER_PORT']
         
     | 
| 
       31 
     | 
    
         
            -
                path = env['PATH_INFO']
         
     | 
| 
       32 
     | 
    
         
            -
                query = env['QUERY_STRING']
         
     | 
| 
       33 
     | 
    
         
            -
             
     | 
| 
       34 
     | 
    
         
            -
                url = +"#{scheme}://#{host}:#{port}#{path}"
         
     | 
| 
       35 
     | 
    
         
            -
                url += "?#{query}" if query
         
     | 
| 
       36 
     | 
    
         
            -
             
     | 
| 
       37 
     | 
    
         
            -
                uri = WebMock::Util::URI.heuristic_parse(url)
         
     | 
| 
       38 
     | 
    
         
            -
                uri.path = uri.normalized_path.gsub('[^:]//', '/')
         
     | 
| 
       39 
     | 
    
         
            -
                uri
         
     | 
| 
       40 
     | 
    
         
            -
              end
         
     | 
| 
       41 
     | 
    
         
            -
             
     | 
| 
       42 
     | 
    
         
            -
              def req_headers(env)
         
     | 
| 
       43 
     | 
    
         
            -
                http_headers = env.select { |k, _| k.start_with?('HTTP_') }
         
     | 
| 
       44 
     | 
    
         
            -
                                  .map { |k, v| [k[5..-1], v] }
         
     | 
| 
       45 
     | 
    
         
            -
                                  .to_h
         
     | 
| 
       46 
     | 
    
         
            -
             
     | 
| 
       47 
     | 
    
         
            -
                special_headers = Faraday::Adapter::Rack::SPECIAL_HEADERS
         
     | 
| 
       48 
     | 
    
         
            -
                http_headers.merge(env.select { |k, _| special_headers.include?(k) })
         
     | 
| 
       49 
     | 
    
         
            -
              end
         
     | 
| 
       50 
     | 
    
         
            -
             
     | 
| 
       51 
     | 
    
         
            -
              def req_body(env)
         
     | 
| 
       52 
     | 
    
         
            -
                env['rack.input'].read
         
     | 
| 
       53 
     | 
    
         
            -
              end
         
     | 
| 
       54 
     | 
    
         
            -
             
     | 
| 
       55 
     | 
    
         
            -
              def process_response(req_signature)
         
     | 
| 
       56 
     | 
    
         
            -
                res = WebMock::StubRegistry.instance.response_for_request(req_signature)
         
     | 
| 
       57 
     | 
    
         
            -
             
     | 
| 
       58 
     | 
    
         
            -
                if res.nil? && req_signature.uri.host == 'localhost'
         
     | 
| 
       59 
     | 
    
         
            -
                  raise Faraday::ConnectionFailed, 'Trying to connect to localhost'
         
     | 
| 
       60 
     | 
    
         
            -
                end
         
     | 
| 
       61 
     | 
    
         
            -
             
     | 
| 
       62 
     | 
    
         
            -
                raise WebMock::NetConnectNotAllowedError, req_signature unless res
         
     | 
| 
       63 
     | 
    
         
            -
             
     | 
| 
       64 
     | 
    
         
            -
                raise Faraday::TimeoutError if res.should_timeout
         
     | 
| 
       65 
     | 
    
         
            -
             
     | 
| 
       66 
     | 
    
         
            -
                [res.status[0], res.headers || {}, [res.body || '']]
         
     | 
| 
       67 
     | 
    
         
            -
              end
         
     | 
| 
       68 
     | 
    
         
            -
            end
         
     |