1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
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::Error::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::Error::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_equal nil, @conn.get('no_content').body
end
def test_304
assert_equal nil, @conn.get('not_modified').body
end
end
|