forked from mislav/faraday-stack
/
response_middleware_test.rb
72 lines (62 loc) · 2.14 KB
/
response_middleware_test.rb
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 'test_helper'
class ResponseMiddlewareTest < Test::Unit::TestCase
def setup
@json_handler = FaradayStack::ResponseJSON
@conn = Faraday.new do |b|
b.use @json_handler
b.adapter :test do |stub|
stub.get('json') { [200, {'Content-Type' => 'application/json; charset=utf-8'}, "[1,2,3]"] }
stub.get('blank') { [200, {'Content-Type' => 'application/json'}, ''] }
stub.get('nil') { [200, {'Content-Type' => 'application/json'}, nil] }
stub.get('bad_json') { [200, {'Content-Type' => 'application/json'}, '<body></body>']}
stub.get('non_json') { [200, {'Content-Type' => 'text/html'}, '<body></body>']}
end
end
end
def process_only(*types)
@conn.builder.swap @json_handler, @json_handler, :content_type => types
end
def test_uses_json_to_parse_json_content
response = @conn.get('json')
assert response.success?
assert_equal [1,2,3], response.body
end
def test_uses_json_to_parse_json_content_conditional
process_only('application/json')
response = @conn.get('json')
assert response.success?
assert_equal [1,2,3], response.body
end
def test_uses_json_to_parse_json_content_conditional_with_regexp
process_only(%r{/(x-)?json$})
response = @conn.get('json')
assert response.success?
assert_equal [1,2,3], response.body
end
def test_uses_json_to_skip_blank_content
response = @conn.get('blank')
assert response.success?
assert_nil response.body
end
def test_uses_json_to_skip_nil_content
response = @conn.get('nil')
assert response.success?
assert_nil response.body
end
def test_uses_json_to_raise_Faraday_Error_Parsing_with_no_json_content
assert_raises Faraday::Error::ParsingError do
@conn.get('bad_json')
end
end
def test_non_json_response
assert_raises Faraday::Error::ParsingError do
@conn.get('non_json')
end
end
def test_non_json_response_conditional
process_only('application/json')
response = @conn.get('non_json')
assert_equal 'text/html', response.headers['Content-Type']
assert_equal '<body></body>', response.body
end
end