Sha256: e1c83118d0945edc71010c7db2e743ca687c7a2009c04fbcc72321cf7cd396a3

Contents?: true

Size: 1.77 KB

Versions: 77

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

RSpec.describe Faraday::Response::Middleware do
  let(:conn) do
    Faraday.new do |b|
      b.use custom_middleware
      b.adapter :test do |stub|
        stub.get('ok') { [200, { 'Content-Type' => 'text/html' }, '<body></body>'] }
        stub.get('not_modified') { [304, nil, nil] }
        stub.get('no_content') { [204, nil, nil] }
      end
    end
  end

  context 'with a custom ResponseMiddleware' do
    let(:custom_middleware) do
      Class.new(Faraday::Response::Middleware) do
        def parse(body)
          body.upcase
        end
      end
    end

    it 'parses the response' do
      expect(conn.get('ok').body).to eq('<BODY></BODY>')
    end
  end

  context 'with a custom ResponseMiddleware and private parse' do
    let(:custom_middleware) do
      Class.new(Faraday::Response::Middleware) do
        private

        def parse(body)
          body.upcase
        end
      end
    end

    it 'parses the response' do
      expect(conn.get('ok').body).to eq('<BODY></BODY>')
    end
  end

  context 'with a custom ResponseMiddleware but empty response' do
    let(:custom_middleware) do
      Class.new(Faraday::Response::Middleware) do
        def parse(_body)
          raise 'this should not be called'
        end
      end
    end

    it 'raises exception for 200 responses' do
      expect { conn.get('ok') }.to raise_error(StandardError)
    end

    it 'doesn\'t call the middleware for 204 responses' do
      expect_any_instance_of(custom_middleware).not_to receive(:parse)
      expect(conn.get('no_content').body).to be_nil
    end

    it 'doesn\'t call the middleware for 304 responses' do
      expect_any_instance_of(custom_middleware).not_to receive(:parse)
      expect(conn.get('not_modified').body).to be_nil
    end
  end
end

Version data entries

77 entries across 64 versions & 15 rubygems

Version Path
faraday-1.10.4 spec/faraday/response/middleware_spec.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/faraday-1.2.0/spec/faraday/response/middleware_spec.rb
avalara_sdk-24.2.29 vendor/bundle/ruby/2.7.0/gems/faraday-1.10.3/spec/faraday/response/middleware_spec.rb
faraday-1.10.3 spec/faraday/response/middleware_spec.rb
faraday-1.10.2 spec/faraday/response/middleware_spec.rb
faraday-1.10.1 spec/faraday/response/middleware_spec.rb
alloy_sdk-0.1.0 vendor/bundle/ruby/3.0.0/gems/faraday-1.0.1/spec/faraday/response/middleware_spec.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/faraday-1.10.0/spec/faraday/response/middleware_spec.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/faraday-1.10.0/spec/faraday/response/middleware_spec.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/faraday-1.10.0/spec/faraday/response/middleware_spec.rb
faraday-1.10.0 spec/faraday/response/middleware_spec.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/faraday-1.9.3/spec/faraday/response/middleware_spec.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/faraday-1.8.0/spec/faraday/response/middleware_spec.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/faraday-1.9.3/spec/faraday/response/middleware_spec.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/faraday-1.9.3/spec/faraday/response/middleware_spec.rb
faraday-1.9.3 spec/faraday/response/middleware_spec.rb
faraday-1.9.2 spec/faraday/response/middleware_spec.rb
faraday-1.9.1 spec/faraday/response/middleware_spec.rb
faraday-1.9.0 spec/faraday/response/middleware_spec.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/faraday-1.8.0/spec/faraday/response/middleware_spec.rb