minato_ruby_api_client 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +23 -0
- data/Dockerfile +1 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +398 -0
- data/README.md +54 -0
- data/Rakefile +10 -0
- data/compose.yml +11 -0
- data/coverage/.last_run.json +5 -0
- data/coverage/.resultset.json +1248 -0
- data/coverage/.resultset.json.lock +0 -0
- data/coverage/assets/0.12.3/DataTables-1.10.20/images/sort_asc.png +0 -0
- data/coverage/assets/0.12.3/DataTables-1.10.20/images/sort_asc_disabled.png +0 -0
- data/coverage/assets/0.12.3/DataTables-1.10.20/images/sort_both.png +0 -0
- data/coverage/assets/0.12.3/DataTables-1.10.20/images/sort_desc.png +0 -0
- data/coverage/assets/0.12.3/DataTables-1.10.20/images/sort_desc_disabled.png +0 -0
- data/coverage/assets/0.12.3/application.css +1 -0
- data/coverage/assets/0.12.3/application.js +7 -0
- data/coverage/assets/0.12.3/colorbox/border.png +0 -0
- data/coverage/assets/0.12.3/colorbox/controls.png +0 -0
- data/coverage/assets/0.12.3/colorbox/loading.gif +0 -0
- data/coverage/assets/0.12.3/colorbox/loading_background.png +0 -0
- data/coverage/assets/0.12.3/favicon_green.png +0 -0
- data/coverage/assets/0.12.3/favicon_red.png +0 -0
- data/coverage/assets/0.12.3/favicon_yellow.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_glass_75_dadada_1x400.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
- data/coverage/assets/0.12.3/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
- data/coverage/assets/0.12.3/images/ui-icons_222222_256x240.png +0 -0
- data/coverage/assets/0.12.3/images/ui-icons_2e83ff_256x240.png +0 -0
- data/coverage/assets/0.12.3/images/ui-icons_454545_256x240.png +0 -0
- data/coverage/assets/0.12.3/images/ui-icons_888888_256x240.png +0 -0
- data/coverage/assets/0.12.3/images/ui-icons_cd0a0a_256x240.png +0 -0
- data/coverage/assets/0.12.3/loading.gif +0 -0
- data/coverage/assets/0.12.3/magnify.png +0 -0
- data/coverage/index.html +13728 -0
- data/lib/minato_ruby_api_client/api_client.rb +389 -0
- data/lib/minato_ruby_api_client/api_error.rb +16 -0
- data/lib/minato_ruby_api_client/configuration.rb +247 -0
- data/lib/minato_ruby_api_client/version.rb +3 -0
- data/lib/minato_ruby_api_client.rb +24 -0
- data/minato_ruby_api_client.gemspec +35 -0
- data/spec/api_client_spec.rb +309 -0
- data/spec/configuration_spec.rb +204 -0
- data/spec/minato_ruby_api_client_spec.rb +17 -0
- data/spec/spec_helper.rb +105 -0
- metadata +165 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
=begin
|
4
|
+
# Minato Ruby Api Client
|
5
|
+
|
6
|
+
The Minato ruby HTTP Client
|
7
|
+
|
8
|
+
=end
|
9
|
+
|
10
|
+
$:.push File.expand_path("../lib", __FILE__)
|
11
|
+
require "minato_ruby_api_client/version"
|
12
|
+
|
13
|
+
Gem::Specification.new do |s|
|
14
|
+
s.name = "minato_ruby_api_client"
|
15
|
+
s.version = MinatoRubyApiClient::VERSION
|
16
|
+
s.platform = Gem::Platform::RUBY
|
17
|
+
s.authors = ["Ferreri"]
|
18
|
+
s.email = ["support@ferreri.co"]
|
19
|
+
s.homepage = "http://ferreri.co"
|
20
|
+
s.summary = "A Minato ruby HTTP Client"
|
21
|
+
s.description = "Minato HTTP Client"
|
22
|
+
s.license = "unlicense"
|
23
|
+
s.required_ruby_version = ">= 3.0"
|
24
|
+
|
25
|
+
s.add_runtime_dependency 'faraday', '~> 1.0', '>= 1.0.1'
|
26
|
+
s.add_runtime_dependency 'minato_error_handler', '~> 0.1.9-1'
|
27
|
+
s.add_runtime_dependency 'minato-trace', '~> 0.1.6-8'
|
28
|
+
|
29
|
+
s.add_development_dependency 'rspec', '~> 3.6', '>= 3.6.0'
|
30
|
+
|
31
|
+
s.files = `find *`.split("\n").uniq.sort.select { |f| !f.empty? }
|
32
|
+
s.test_files = `find spec/*`.split("\n")
|
33
|
+
s.executables = []
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
end
|
@@ -0,0 +1,309 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MinatoRubyApiClient::ApiClient do
|
4
|
+
|
5
|
+
[:config, :default_headers].each do |method|
|
6
|
+
it { should respond_to method }
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:url) { 'https://example.com/v1' }
|
10
|
+
|
11
|
+
let(:api_client) do
|
12
|
+
uri = URI.parse(url)
|
13
|
+
described_class.default.tap do |api_client|
|
14
|
+
api_client.config.host = uri.host
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.default' do
|
19
|
+
it 'memoizes the instance of MinatoRubyApiClient::ApiClient in subsequent calls' do
|
20
|
+
expect(described_class).to receive(:new).once.and_call_original
|
21
|
+
2.times { described_class.default }
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'returns a instance of MinatoRubyApiClient::ApiClient' do
|
25
|
+
expect(described_class.default).to be_a MinatoRubyApiClient::ApiClient
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#build_request_body' do
|
30
|
+
let(:form_params) { { foo: 'bar', bar: 'foobar' } }
|
31
|
+
let(:body) { { foo: 'bar', bar: 'foobar' } }
|
32
|
+
|
33
|
+
before do
|
34
|
+
@data = api_client.build_request_body(header_params, form_params, body)
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'when content type is form-url-encoded' do
|
38
|
+
let(:header_params) { { 'Content-Type' => 'application/x-www-form-urlencoded' } }
|
39
|
+
|
40
|
+
it 'returns data in url encoded format' do
|
41
|
+
expect(@data).to eq('foo=bar&bar=foobar')
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context 'when content type is multipar/form-data' do
|
46
|
+
let(:header_params) { { 'Content-Type' => 'multipart/form-data' } }
|
47
|
+
|
48
|
+
it 'returns data in hash format' do
|
49
|
+
expect(@data).to eq(form_params)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'when body is a string' do
|
54
|
+
let(:header_params) { { 'Content-Type' => 'text/plain' } }
|
55
|
+
let(:body) { 'body is a string' }
|
56
|
+
|
57
|
+
it 'returns data in string format' do
|
58
|
+
expect(@data).to eq(body)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'when body is a hash' do
|
63
|
+
let(:header_params) { { 'Content-Type' => 'application/json' } }
|
64
|
+
|
65
|
+
it 'returns data in json format' do
|
66
|
+
expect(@data).to eq(body.to_json)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context 'when no body and form content types is passed' do
|
71
|
+
let(:header_params) { { 'Content-Type' => 'application/json' } }
|
72
|
+
let(:body) { nil }
|
73
|
+
|
74
|
+
it 'returns a null data' do
|
75
|
+
expect(@data).to be_nil
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe '#json_mime?' do
|
81
|
+
context 'when content type is json' do
|
82
|
+
it 'returns true' do
|
83
|
+
expect(api_client.json_mime?('application/json')).to be true
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'when content type is not json' do
|
88
|
+
it 'returns false' do
|
89
|
+
expect(api_client.json_mime?('application/xml')).to be false
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe '#call_api' do
|
95
|
+
let(:opts) { { body: { name: 'Foo Bar' }, auth_names: 'apikey', return_type: 'Object' } }
|
96
|
+
let(:http_response) do
|
97
|
+
{ status: 200, headers: { 'Context-type': 'application/json' },
|
98
|
+
body: { message: 'success' }.to_json }
|
99
|
+
end
|
100
|
+
|
101
|
+
before do
|
102
|
+
stub_request(:post, /test/)
|
103
|
+
.to_return(http_response)
|
104
|
+
allow(api_client.config).to receive(:logger).and_return(Logger.new(STDOUT))
|
105
|
+
api_client.config.logger.level = Logger::WARN
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'when request is successful' do
|
109
|
+
it 'returns data, status and headers' do
|
110
|
+
data, status_code, headers = api_client.call_api(:post, '/test', opts)
|
111
|
+
expect(data).to eq({ message: 'success' })
|
112
|
+
expect(status_code).to eq(200)
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'returns nil when return_type is not defined' do
|
116
|
+
opts[:return_type] = nil
|
117
|
+
data, status_code, headers = api_client.call_api(:post, '/test', opts)
|
118
|
+
expect(data).to be_nil
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context 'when request failed' do
|
123
|
+
let(:http_response) do
|
124
|
+
{ status: [400, 'Bad Request'], headers: { 'Context-type': 'application/json' },
|
125
|
+
body: { message: 'Dados inválidos' }.to_json }
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'raise an error MinatoRubyApiClient::ApiError' do
|
129
|
+
expect { api_client.call_api(:post, '/test', opts) }.to raise_error(
|
130
|
+
an_instance_of(MinatoRubyApiClient::ApiError)
|
131
|
+
.and having_attributes(message: 'An error occurred while communicating with the API.',
|
132
|
+
caused_by: 'Bad Request', status_code: 400))
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context 'when request timeout' do
|
137
|
+
before { stub_request(:get, /test/).to_raise(Faraday::TimeoutError) }
|
138
|
+
|
139
|
+
it 'raise an error caused by timeout' do
|
140
|
+
expect { api_client.call_api(:get, '/test', opts) }.to raise_error { |error|
|
141
|
+
expect(error).to be_a(MinatoRubyApiClient::ApiError)
|
142
|
+
expect(error.caused_by).to be_a(Faraday::TimeoutError)
|
143
|
+
}
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
describe 'trace' do
|
149
|
+
let(:config) { MinatoRubyApiClient::Configuration.new }
|
150
|
+
let(:api_client) { described_class.new(config) }
|
151
|
+
|
152
|
+
context 'when trace is enabled and Rails is defined' do
|
153
|
+
it 'add distributed trace middleware to middleware stack' do
|
154
|
+
allow(Object).to receive(:const_defined?).with(:Rails).and_return(true)
|
155
|
+
allow(Rails.env).to receive(:production?).and_return(true)
|
156
|
+
allow(Minato::Trace).to receive(:enabled?).and_return(true)
|
157
|
+
expect(api_client.config.instance_variable_get('@middlewares')).to include([Minato::Trace::Middleware::DistributedTraceContext])
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
context 'when trace is disabled' do
|
162
|
+
it 'should not add ditributed trace middleware' do
|
163
|
+
allow(Object).to receive(:const_defined?).with(:Rails).and_return(true)
|
164
|
+
allow(Rails.env).to receive(:production?).and_return(true)
|
165
|
+
allow(Minato::Trace).to receive(:enabled?).and_return(false)
|
166
|
+
expect(api_client.config.instance_variable_get('@middlewares')).to eq []
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
context 'when Rails is not defined' do
|
171
|
+
it 'should not add ditributed trace middleware' do
|
172
|
+
allow(Object).to receive(:const_defined?).with(:Rails).and_return(false)
|
173
|
+
allow(Minato::Trace).to receive(:enabled?).and_return(true)
|
174
|
+
expect(api_client.config.instance_variable_get('@middlewares')).to eq []
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
context 'when Rails is defined but is not in productio mode' do
|
179
|
+
it 'should not add ditributed trace middleware' do
|
180
|
+
allow(Object).to receive(:const_defined?).with(:Rails).and_return(true)
|
181
|
+
allow(Rails.env).to receive(:production?).and_return(false)
|
182
|
+
allow(Minato::Trace).to receive(:enabled?).and_return(true)
|
183
|
+
expect(api_client.config.instance_variable_get('@middlewares')).to eq []
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
describe '#convert_to_type' do
|
189
|
+
context 'when data is nil' do
|
190
|
+
it 'returns nil' do
|
191
|
+
expect(api_client.convert_to_type(nil, 'String')).to be_nil
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
context 'when return type is String' do
|
196
|
+
it 'returns string' do
|
197
|
+
expect(api_client.convert_to_type(1, 'String')).to be_a(String)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
context 'when return type is Integer' do
|
202
|
+
it 'returns integer' do
|
203
|
+
expect(api_client.convert_to_type('1', 'Integer')).to be_a(Integer)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
context 'when return type is Float' do
|
208
|
+
it 'returns float' do
|
209
|
+
expect(api_client.convert_to_type('1', 'Float')).to be_a(Float)
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
context 'when return type is Boolean' do
|
214
|
+
it 'returns boolean' do
|
215
|
+
expect(api_client.convert_to_type(true, 'Boolean')).to be_truthy
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
context 'when return type is Time' do
|
220
|
+
it 'returns time' do
|
221
|
+
expect(api_client.convert_to_type('12:00:00', 'Time')).to be_a(Time)
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
context 'when return type is Date' do
|
226
|
+
it 'returns time' do
|
227
|
+
expect(api_client.convert_to_type('2020-12-25 12:00:00', 'Date')).to be_a(Date)
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
context 'when return type is Object' do
|
232
|
+
it 'returns data' do
|
233
|
+
data = { id: 1 }
|
234
|
+
expect(api_client.convert_to_type(data, 'Object')).to equal(data)
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
context 'when return type is Array<String>' do
|
239
|
+
it 'returns array list of type specified' do
|
240
|
+
data = [1, 2, 3]
|
241
|
+
expect(api_client.convert_to_type(data, 'Array<String>')).to eq(['1', '2', '3'])
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
context 'when return type is Hash<String, Integer>' do
|
246
|
+
it 'returns hash with key:value of types specifieds' do
|
247
|
+
data = { id: '22', qtd: '12' }
|
248
|
+
expect(api_client.convert_to_type(data, 'Hash<String, Integer>')).to eq({ 'id': 22, 'qtd': 12 })
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
context 'when return type is a custom type as a class' do
|
253
|
+
it 'returns a object from the class' do
|
254
|
+
klass = double('Pet', name: 'Bob')
|
255
|
+
allow(MinatoRubyApiClient).to receive(:const_get).with('Pet').and_return(klass)
|
256
|
+
allow(klass).to receive(:build_from_hash).and_return(true)
|
257
|
+
|
258
|
+
expect(api_client.convert_to_type({ name: 'Bob' }, 'Pet')).to be_truthy
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
describe '#update_params_for_auth!' do
|
264
|
+
let(:header_params) { {} }
|
265
|
+
let(:query_params) { {} }
|
266
|
+
let(:config) { MinatoRubyApiClient::Configuration.new }
|
267
|
+
let(:api_client) { described_class.new(config) }
|
268
|
+
|
269
|
+
context 'when auth method is bearer auth' do
|
270
|
+
it 'add bearer auth in header' do
|
271
|
+
api_client.config.access_token = 'access_token'
|
272
|
+
api_client.update_params_for_auth!(header_params, query_params, ['bearer_auth'])
|
273
|
+
expect(header_params).to eq({ 'Authorization' => 'Bearer access_token' })
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
context 'when auth method is basic auth' do
|
278
|
+
it 'add basic auth in header' do
|
279
|
+
api_client.config.username = 'user'
|
280
|
+
api_client.config.password = 'pass'
|
281
|
+
basic_auth = Base64.encode64("user:pass").chomp.delete("\n")
|
282
|
+
|
283
|
+
api_client.update_params_for_auth!(header_params, query_params, ['basic_auth'])
|
284
|
+
expect(header_params).to eq({ 'Authorization' => "Basic #{basic_auth}" })
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
context 'when auth method is apikey' do
|
289
|
+
context 'when api_key prefix is not defined' do
|
290
|
+
it 'add apikey auth in header without prefix' do
|
291
|
+
api_client.config.api_key['api_key'] = 'key'
|
292
|
+
|
293
|
+
api_client.update_params_for_auth!(header_params, query_params, ['apikey'])
|
294
|
+
expect(header_params).to eq({ 'api_key' => 'key' })
|
295
|
+
end
|
296
|
+
end
|
297
|
+
|
298
|
+
context 'when api_key prefix is defined' do
|
299
|
+
it 'add apikey auth in header with prefix' do
|
300
|
+
api_client.config.api_key['api_key'] = 'key'
|
301
|
+
api_client.config.api_key_prefix['api_key'] = 'prefix'
|
302
|
+
|
303
|
+
api_client.update_params_for_auth!(header_params, query_params, ['apikey'])
|
304
|
+
expect(header_params).to eq({ 'api_key' => 'prefix key' })
|
305
|
+
end
|
306
|
+
end
|
307
|
+
end
|
308
|
+
end
|
309
|
+
end
|
@@ -0,0 +1,204 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MinatoRubyApiClient::Configuration do
|
4
|
+
let(:url) { 'https://example.com/v1' }
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
require 'uri'
|
8
|
+
uri = URI.parse(url)
|
9
|
+
MinatoRubyApiClient.configure do |c|
|
10
|
+
c.host = uri.host
|
11
|
+
c.base_path = uri.path
|
12
|
+
c.api_key['api_key'] = 'test_key'
|
13
|
+
end
|
14
|
+
|
15
|
+
@config = described_class.default
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#scheme' do
|
19
|
+
it 'should have the default value https' do
|
20
|
+
expect(@config.scheme).to eq('https')
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should remove :// from scheme' do
|
24
|
+
@config.scheme = 'https://'
|
25
|
+
expect(@config.scheme).to eq('https')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '#host' do
|
30
|
+
it 'should do not have the scheme value' do
|
31
|
+
expect(@config.host).not_to match(/https/)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '#base_path' do
|
36
|
+
it 'should add leading slashes if missing' do
|
37
|
+
@config.base_path = 'v1'
|
38
|
+
expect(@config.base_path).to eq('/v1')
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'should be empty if is /' do
|
42
|
+
@config.base_path = '/'
|
43
|
+
expect(@config.base_path).to eq('')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#user_agent' do
|
48
|
+
it 'should have user agent' do
|
49
|
+
@config.user_agent = 'Minato'
|
50
|
+
expect(@config.user_agent).to eq('Minato')
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#base_url' do
|
55
|
+
it 'should have the default value' do
|
56
|
+
expect(@config.base_url).to eq(url)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'should remove trailing slashes' do
|
60
|
+
[nil, '', '/', '//'].each do |base_path|
|
61
|
+
@config.base_path = base_path
|
62
|
+
expect(@config.base_url).to eq("https://example.com")
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe '#timeout' do
|
68
|
+
it 'should have the default value' do
|
69
|
+
expect(@config.timeout).to eq(60)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe '#debugging' do
|
74
|
+
it 'should have the default value falsy' do
|
75
|
+
expect(@config.debugging).to be_falsy
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'when MINATO_RUBY_API_CLIENT_DEBUG is true' do
|
79
|
+
it 'should be truthy' do
|
80
|
+
allow(ENV).to receive(:[]).with('MINATO_RUBY_API_CLIENT_DEBUG').and_return('true')
|
81
|
+
expect(@config.debugging).to be_truthy
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'when debug is active' do
|
86
|
+
it 'should be truthy' do
|
87
|
+
config = described_class.new
|
88
|
+
config.debugging = true
|
89
|
+
expect(config.debugging).to be_truthy
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe '#auth_settings' do
|
95
|
+
it 'should have default auth types' do
|
96
|
+
expect(@config.auth_settings.keys).to eq(%w[bearer_auth basic_auth apikey])
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe '#basic_auth_token' do
|
101
|
+
let(:username) { 'test' }
|
102
|
+
let(:password) { 'test' }
|
103
|
+
let(:token) { ["#{username}:#{password}"].pack('m').delete("\r\n") }
|
104
|
+
it 'should return basic auth string' do
|
105
|
+
@config.username = username
|
106
|
+
@config.password = password
|
107
|
+
expect(@config.basic_auth_token).to eq("Basic #{token}")
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe '#logger' do
|
112
|
+
let(:logger) { Logger.new(STDOUT) }
|
113
|
+
|
114
|
+
context 'when Rails is defined' do
|
115
|
+
it 'should return Rails.logger' do
|
116
|
+
allow(Object).to receive(:const_defined?).with(:Rails).and_return(true)
|
117
|
+
allow(Rails).to receive(:logger).and_return(logger)
|
118
|
+
config = described_class.new
|
119
|
+
expect(config.logger).to eq(Rails.logger)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context 'when Rails is not defined' do
|
124
|
+
it 'should return Logger instance' do
|
125
|
+
allow(Object).to receive(:const_defined?).with(:Rails).and_return(nil)
|
126
|
+
config = described_class.new
|
127
|
+
expect(config.logger).to be_instance_of(Logger)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
describe '#api_key_with_prefix' do
|
133
|
+
context 'when api_key_prefix is not defined' do
|
134
|
+
it 'return the key without prefix' do
|
135
|
+
expect(@config.api_key_with_prefix('api_key')).to eq('test_key')
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context 'when api_key_prefix is defined' do
|
140
|
+
it 'return the key with prefix' do
|
141
|
+
@config.api_key_prefix['api_key'] = 'Prefix'
|
142
|
+
expect(@config.api_key_with_prefix('api_key')).to eq('Prefix test_key')
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe '#configure' do
|
148
|
+
context 'when block is given' do
|
149
|
+
it 'should yield the configuration object' do
|
150
|
+
expect { |b| @config.configure(&b) }.to yield_with_args
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
context 'when block is not given' do
|
155
|
+
it 'should return nil' do
|
156
|
+
expect(@config.configure).to be_nil
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
describe '#use' do
|
162
|
+
it 'add middleware to the middlewares list' do
|
163
|
+
@config.use(:test)
|
164
|
+
expect(@config.instance_variable_get('@middlewares')).to include([:test])
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
describe '#request' do
|
169
|
+
it 'add middleware to the request middlewares list' do
|
170
|
+
@config.request(:test)
|
171
|
+
expect(@config.instance_variable_get('@request_middlewares')).to include([:test])
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
describe '#response' do
|
176
|
+
it 'add middleware to the response middlewares list' do
|
177
|
+
@config.response(:test)
|
178
|
+
expect(@config.instance_variable_get('@response_middlewares')).to include([:test])
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
describe '#configure_middleware' do
|
183
|
+
let(:conn) { double('conn') }
|
184
|
+
let(:config) { described_class.new }
|
185
|
+
|
186
|
+
before do
|
187
|
+
config.use(:use)
|
188
|
+
config.request(:request)
|
189
|
+
config.response(:response)
|
190
|
+
|
191
|
+
allow(conn).to receive(:use)
|
192
|
+
allow(conn).to receive(:request)
|
193
|
+
allow(conn).to receive(:response)
|
194
|
+
|
195
|
+
config.configure_middleware(conn)
|
196
|
+
end
|
197
|
+
|
198
|
+
it 'add middlewares to connection middleware stack' do
|
199
|
+
expect(conn).to have_received(:use).once.with(:use)
|
200
|
+
expect(conn).to have_received(:request).once.with(:request)
|
201
|
+
expect(conn).to have_received(:response).once.with(:response)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MinatoRubyApiClient do
|
4
|
+
describe '.configure' do
|
5
|
+
context 'when no block is given' do
|
6
|
+
it 'returns the default configuration' do
|
7
|
+
expect(MinatoRubyApiClient.configure).to eq MinatoRubyApiClient::Configuration.default
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context 'when a block is given' do
|
12
|
+
it 'yields the configuration' do
|
13
|
+
expect { |b| MinatoRubyApiClient.configure(&b) }.to yield_control
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start
|
3
|
+
|
4
|
+
require 'webmock/rspec'
|
5
|
+
WebMock.disable_net_connect!(allow_localhost: true)
|
6
|
+
|
7
|
+
# load the gem
|
8
|
+
require 'minato_ruby_api_client'
|
9
|
+
|
10
|
+
# The following was generated by the `rspec --init` command. Conventionally, all
|
11
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
12
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
13
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
14
|
+
# files.
|
15
|
+
#
|
16
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
17
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
18
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
19
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
20
|
+
# a separate helper file that requires the additional dependencies and performs
|
21
|
+
# the additional setup, and require it from the spec files that actually need
|
22
|
+
# it.
|
23
|
+
#
|
24
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
25
|
+
# users commonly want.
|
26
|
+
#
|
27
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
28
|
+
RSpec.configure do |config|
|
29
|
+
# rspec-expectations config goes here. You can use an alternate
|
30
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
31
|
+
# assertions if you prefer.
|
32
|
+
config.expect_with :rspec do |expectations|
|
33
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
34
|
+
# and `failure_message` of custom matchers include text for helper methods
|
35
|
+
# defined using `chain`, e.g.:
|
36
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
37
|
+
# # => "be bigger than 2 and smaller than 4"
|
38
|
+
# ...rather than:
|
39
|
+
# # => "be bigger than 2"
|
40
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
44
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
45
|
+
config.mock_with :rspec do |mocks|
|
46
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
47
|
+
# a real object. This is generally recommended, and will default to
|
48
|
+
# `true` in RSpec 4.
|
49
|
+
mocks.verify_partial_doubles = true
|
50
|
+
end
|
51
|
+
|
52
|
+
# The settings below are suggested to provide a good initial experience
|
53
|
+
# with RSpec, but feel free to customize to your heart's content.
|
54
|
+
=begin
|
55
|
+
# These two settings work together to allow you to limit a spec run
|
56
|
+
# to individual examples or groups you care about by tagging them with
|
57
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
58
|
+
# get run.
|
59
|
+
config.filter_run :focus
|
60
|
+
config.run_all_when_everything_filtered = true
|
61
|
+
|
62
|
+
# Allows RSpec to persist some state between runs in order to support
|
63
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
64
|
+
# you configure your source control system to ignore this file.
|
65
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
66
|
+
|
67
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
68
|
+
# recommended. For more details, see:
|
69
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
70
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
71
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
72
|
+
config.disable_monkey_patching!
|
73
|
+
|
74
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
75
|
+
# be too noisy due to issues in dependencies.
|
76
|
+
config.warnings = true
|
77
|
+
|
78
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
79
|
+
# file, and it's useful to allow more verbose output when running an
|
80
|
+
# individual spec file.
|
81
|
+
if config.files_to_run.one?
|
82
|
+
# Use the documentation formatter for detailed output,
|
83
|
+
# unless a formatter has already been configured
|
84
|
+
# (e.g. via a command-line flag).
|
85
|
+
config.default_formatter = 'doc'
|
86
|
+
end
|
87
|
+
|
88
|
+
# Print the 10 slowest examples and example groups at the
|
89
|
+
# end of the spec run, to help surface which specs are running
|
90
|
+
# particularly slow.
|
91
|
+
config.profile_examples = 10
|
92
|
+
|
93
|
+
# Run specs in random order to surface order dependencies. If you find an
|
94
|
+
# order dependency and want to debug it, you can fix the order by providing
|
95
|
+
# the seed, which is printed after each run.
|
96
|
+
# --seed 1234
|
97
|
+
config.order = :random
|
98
|
+
|
99
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
100
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
101
|
+
# test failures related to randomization by passing the same `--seed` value
|
102
|
+
# as the one that triggered the failure.
|
103
|
+
Kernel.srand config.seed
|
104
|
+
=end
|
105
|
+
end
|