Sha256: 9bda6ac3bf575a9c85728c1ba2ad72c304a2469647ac365a1060a5a170b9a88f

Contents?: true

Size: 1.82 KB

Versions: 9

Compression:

Stored size: 1.82 KB

Contents

require File.expand_path('../helper', __FILE__)

class ResponseMiddlewareTest < Faraday::TestCase
  def setup
    @conn = Faraday.new do |b|
      b.response :raise_error
      b.adapter :test do |stub|
        stub.get('ok')        { [200, {'Content-Type' => 'text/html'}, '<body></body>'] }
        stub.get('not-found') { [404, {'X-Reason' => 'because'}, 'keep looking'] }
        stub.get('error')     { [500, {'X-Error' => 'bailout'}, 'fail'] }
      end
    end
  end

  class ResponseUpcaser < Faraday::Response::Middleware
    def parse(body)
      body.upcase
    end
  end

  def test_success
    assert @conn.get('ok')
  end

  def test_raises_not_found
    error = assert_raises Faraday::ResourceNotFound do
      @conn.get('not-found')
    end
    assert_equal 'the server responded with status 404', error.message
    assert_equal 'because', error.response[:headers]['X-Reason']
  end

  def test_raises_error
    error = assert_raises Faraday::ClientError do
      @conn.get('error')
    end
    assert_equal 'the server responded with status 500', error.message
    assert_equal 'bailout', error.response[:headers]['X-Error']
  end

  def test_upcase
    @conn.builder.insert(0, ResponseUpcaser)
    assert_equal '<BODY></BODY>', @conn.get('ok').body
  end
end

class ResponseNoBodyMiddleWareTest < Faraday::TestCase
  def setup
    @conn = Faraday.new do |b|
      b.response :raise_error
      b.adapter :test do |stub|
        stub.get('not_modified') { [304, nil, nil] }
        stub.get('no_content') { [204, nil, nil] }
      end
    end
    @conn.builder.insert(0, NotCalled)
  end

  class NotCalled < Faraday::Response::Middleware
    def parse(body)
      raise "this should not be called"
    end
  end

  def test_204
    assert_nil @conn.get('no_content').body
  end

  def test_304
    assert_nil @conn.get('not_modified').body
  end
end

Version data entries

9 entries across 8 versions & 2 rubygems

Version Path
faraday-0.17.6 test/response_middleware_test.rb
faraday-0.17.5 test/response_middleware_test.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/tdiary-5.1.5/vendor/bundle/ruby/3.0.0/gems/faraday-0.17.3/test/response_middleware_test.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/faraday-0.17.4/test/response_middleware_test.rb
tdiary-5.1.5 vendor/bundle/ruby/3.0.0/gems/faraday-0.17.3/test/response_middleware_test.rb
faraday-0.17.4 test/response_middleware_test.rb
faraday-0.17.3 test/response_middleware_test.rb
tdiary-5.1.0 vendor/bundle/gems/faraday-0.17.1/test/response_middleware_test.rb
faraday-0.17.1 test/response_middleware_test.rb