forked from lostisland/faraday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_test.rb
144 lines (121 loc) · 3.51 KB
/
client_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# frozen_string_literal: true
# Requires Ruby with test-unit and faraday gems.
# ruby client_test.rb
require 'faraday'
require 'json'
require 'test/unit'
# Example API client
class Client
def initialize(conn)
@conn = conn
end
def httpbingo(jname, params: {})
res = @conn.get("/#{jname}", params)
data = JSON.parse(res.body)
data['origin']
end
def foo(params)
res = @conn.post('/foo', JSON.dump(params))
res.status
end
end
# Example API client test
class ClientTest < Test::Unit::TestCase
def test_httpbingo_name
stubs = Faraday::Adapter::Test::Stubs.new
stubs.get('/api') do |env|
# optional: you can inspect the Faraday::Env
assert_equal '/api', env.url.path
[
200,
{ 'Content-Type': 'application/javascript' },
'{"origin": "127.0.0.1"}'
]
end
# uncomment to trigger stubs.verify_stubbed_calls failure
# stubs.get('/unused') { [404, {}, ''] }
cli = client(stubs)
assert_equal '127.0.0.1', cli.httpbingo('api')
stubs.verify_stubbed_calls
end
def test_httpbingo_not_found
stubs = Faraday::Adapter::Test::Stubs.new
stubs.get('/api') do
[
404,
{ 'Content-Type': 'application/javascript' },
'{}'
]
end
cli = client(stubs)
assert_nil cli.httpbingo('api')
stubs.verify_stubbed_calls
end
def test_httpbingo_exception
stubs = Faraday::Adapter::Test::Stubs.new
stubs.get('/api') do
raise Faraday::ConnectionFailed
end
cli = client(stubs)
assert_raise Faraday::ConnectionFailed do
cli.httpbingo('api')
end
stubs.verify_stubbed_calls
end
def test_strict_mode
stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
stubs.get('/api?abc=123') do
[
200,
{ 'Content-Type': 'application/javascript' },
'{"origin": "127.0.0.1"}'
]
end
cli = client(stubs)
assert_equal '127.0.0.1', cli.httpbingo('api', params: { abc: 123 })
# uncomment to raise Stubs::NotFound
# assert_equal '127.0.0.1', cli.httpbingo('api', params: { abc: 123, foo: 'Kappa' })
stubs.verify_stubbed_calls
end
def test_non_default_params_encoder
stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
stubs.get('/api?a=x&a=y&a=z') do
[
200,
{ 'Content-Type': 'application/javascript' },
'{"origin": "127.0.0.1"}'
]
end
conn = Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) do |builder|
builder.adapter :test, stubs
end
cli = Client.new(conn)
assert_equal '127.0.0.1', cli.httpbingo('api', params: { a: %w[x y z] })
# uncomment to raise Stubs::NotFound
# assert_equal '127.0.0.1', cli.httpbingo('api', params: { a: %w[x y] })
stubs.verify_stubbed_calls
end
def test_with_string_body
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
stub.post('/foo', '{"name":"YK"}') { [200, {}, ''] }
end
cli = client(stubs)
assert_equal 200, cli.foo(name: 'YK')
stubs.verify_stubbed_calls
end
def test_with_proc_body
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
check = ->(request_body) { JSON.parse(request_body).slice('name') == { 'name' => 'YK' } }
stub.post('/foo', check) { [200, {}, ''] }
end
cli = client(stubs)
assert_equal 200, cli.foo(name: 'YK', created_at: Time.now)
stubs.verify_stubbed_calls
end
def client(stubs)
conn = Faraday.new do |builder|
builder.adapter :test, stubs
end
Client.new(conn)
end
end