-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathextend_parse_json_test.rb
More file actions
53 lines (46 loc) · 1.72 KB
/
extend_parse_json_test.rb
File metadata and controls
53 lines (46 loc) · 1.72 KB
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
# encoding: utf-8
require 'helper'
module Middleware
class ExtendedParseJson < Faraday::TestCase
def conn(_retry_options = {})
Faraday.new do |b|
b.use Faraday::ExtendedParseJson
b.adapter :test do |stub|
stub.get('/invalid_json') { [200, {}, 'something'] }
stub.get('/valid_json') { [200, {}, { 'var' => 1 }.to_json] }
stub.get('/parse_error_code') { [403, {}, { 'code' => Parse::Protocol::ERROR_INTERNAL }.to_json] }
stub.get('/empty_response') { [403, {}, ''] }
stub.get('/404') { [404, {}, {}.to_json] }
stub.get('/500') { [500, {}, { 'text' => 'Internal Server Error' }.to_json] }
end
end
end
def test_invalid_json
assert_raises(Faraday::ParsingError) { conn.get('/invalid_json') }
end
def test_valid_json
resp = conn.get('/valid_json')
assert_equal 200, resp.status
assert_equal({ 'var' => 1 }, resp.body)
end
def test_empty_response
ex = assert_raises(Parse::ParseProtocolError) { conn.get('/empty_response') }
assert_match(/403/, ex.to_s)
assert_equal 'HTTP Status 403 Body ', ex.error
end
def test_parse_error_code
ex = assert_raises(Parse::ParseProtocolError) { conn.get('/parse_error_code') }
assert_match(/403/, ex.to_s)
assert_equal Parse::Protocol::ERROR_INTERNAL, ex.code
end
def test_404
ex = assert_raises(Parse::ParseProtocolError) { conn.get('/404') }
assert_match(/404/, ex.to_s)
end
def test_500
ex = assert_raises(Parse::ParseProtocolError) { conn.get('/500') }
assert_match(/500/, ex.to_s)
assert_match(/Internal Server Error/, ex.to_s)
end
end
end