restful_resource 2.3.0 → 2.4.0
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 +4 -4
- data/.circleci/config.yml +20 -0
- data/.codeclimate.yml +3 -0
- data/.rubocop.yml +5 -0
- data/.rubocop_todo.yml +203 -0
- data/CHANGELOG.md +5 -0
- data/README.md +1 -1
- data/Rakefile +5 -5
- data/lib/restful_resource/associations.rb +10 -8
- data/lib/restful_resource/base.rb +36 -33
- data/lib/restful_resource/http_client.rb +50 -48
- data/lib/restful_resource/instrumentation.rb +17 -20
- data/lib/restful_resource/null_logger.rb +1 -2
- data/lib/restful_resource/open_object.rb +2 -2
- data/lib/restful_resource/paginated_array.rb +3 -1
- data/lib/restful_resource/rails_validations.rb +12 -12
- data/lib/restful_resource/redirections.rb +5 -6
- data/lib/restful_resource/request.rb +0 -1
- data/lib/restful_resource/resource_id_missing_error.rb +1 -1
- data/lib/restful_resource/response.rb +4 -2
- data/lib/restful_resource/version.rb +1 -1
- data/restful_resource.gemspec +26 -25
- data/spec/fixtures.rb +7 -7
- data/spec/restful_resource/associations_spec.rb +23 -21
- data/spec/restful_resource/base_authorization_spec.rb +6 -7
- data/spec/restful_resource/base_spec.rb +134 -117
- data/spec/restful_resource/http_client_configuration_spec.rb +20 -19
- data/spec/restful_resource/http_client_spec.rb +38 -38
- data/spec/restful_resource/open_object_spec.rb +8 -8
- data/spec/restful_resource/rails_validations_spec.rb +68 -68
- data/spec/restful_resource/redirections_spec.rb +26 -26
- data/spec/spec_helper.rb +3 -4
- metadata +44 -13
- data/circle.yml +0 -3
@@ -2,13 +2,13 @@ require_relative '../spec_helper'
|
|
2
2
|
|
3
3
|
RSpec.describe RestfulResource::Base do
|
4
4
|
before do
|
5
|
-
@mock_http = double(
|
6
|
-
allow(
|
7
|
-
|
5
|
+
@mock_http = double('mock_http')
|
6
|
+
allow(described_class).to receive(:http).and_return(@mock_http)
|
7
|
+
described_class.configure(base_url: 'http://api.carwow.co.uk/')
|
8
8
|
end
|
9
9
|
|
10
|
-
it
|
11
|
-
object =
|
10
|
+
it 'acts as an openobject' do
|
11
|
+
object = described_class.new(name: 'David', surname: 'Santoro')
|
12
12
|
|
13
13
|
expect(object.name).to eq 'David'
|
14
14
|
expect(object.surname).to eq 'Santoro'
|
@@ -16,15 +16,15 @@ RSpec.describe RestfulResource::Base do
|
|
16
16
|
end
|
17
17
|
|
18
18
|
describe '#parse_json' do
|
19
|
-
it '
|
20
|
-
expect {
|
19
|
+
it 'does not fail on empty string' do
|
20
|
+
expect { described_class.send(:parse_json, ' ') }.not_to raise_error
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
describe
|
25
|
-
it
|
26
|
-
expected_response = RestfulResource::Response.new(body: {id: 12}.to_json)
|
27
|
-
expect_get(
|
24
|
+
describe '#find' do
|
25
|
+
it 'returns an object instance for the correct id' do
|
26
|
+
expected_response = RestfulResource::Response.new(body: { id: 12 }.to_json)
|
27
|
+
expect_get('http://api.carwow.co.uk/makes/12', expected_response)
|
28
28
|
|
29
29
|
object = Make.find(12)
|
30
30
|
|
@@ -32,56 +32,58 @@ RSpec.describe RestfulResource::Base do
|
|
32
32
|
expect(object.id).to be 12
|
33
33
|
end
|
34
34
|
|
35
|
-
it
|
36
|
-
expected_response = RestfulResource::Response.new(body: {name: 'Golf', price:
|
37
|
-
expect_get(
|
35
|
+
it 'returns an object instance for nested routes' do
|
36
|
+
expected_response = RestfulResource::Response.new(body: { name: 'Golf', price: 15_000 }.to_json)
|
37
|
+
expect_get('http://api.carwow.co.uk/groups/15/makes/Volkswagen/models/Golf', expected_response)
|
38
38
|
|
39
39
|
object = Model.find('Golf', make_slug: 'Volkswagen', group_id: 15)
|
40
40
|
|
41
41
|
expect(object).not_to be_nil
|
42
42
|
expect(object.name).to eq 'Golf'
|
43
|
-
expect(object.price).to eq
|
43
|
+
expect(object.price).to eq 15_000
|
44
44
|
end
|
45
45
|
|
46
|
-
it
|
47
|
-
expected_response = RestfulResource::Response.new(body: {name: 'Golf', price:
|
48
|
-
expect_get(
|
46
|
+
it 'encodes parameters correctly in the url' do
|
47
|
+
expected_response = RestfulResource::Response.new(body: { name: 'Golf', price: 15_000 }.to_json)
|
48
|
+
expect_get('http://api.carwow.co.uk/groups/xxx+yyy%3Fl%3D7/makes/Land+Rover%3Fx%3D0.123/models/Golf+Cabriolet%3Ftest', expected_response)
|
49
49
|
|
50
50
|
object = Model.find('Golf Cabriolet?test', make_slug: 'Land Rover?x=0.123', group_id: 'xxx yyy?l=7')
|
51
51
|
end
|
52
52
|
|
53
53
|
it 'accepts custom headers' do
|
54
|
-
expect_get(
|
54
|
+
expect_get('http://api.carwow.co.uk/makes/12',
|
55
55
|
RestfulResource::Response.new,
|
56
|
-
headers: { cache_control: 'no-cache' }
|
56
|
+
headers: { cache_control: 'no-cache' }
|
57
|
+
)
|
57
58
|
|
58
59
|
Make.find(12, headers: { cache_control: 'no-cache' })
|
59
60
|
end
|
60
61
|
|
61
62
|
it 'accepts no_cache option' do
|
62
|
-
expect_get(
|
63
|
+
expect_get('http://api.carwow.co.uk/makes/12',
|
63
64
|
RestfulResource::Response.new,
|
64
|
-
headers: { cache_control: 'no-cache' }
|
65
|
+
headers: { cache_control: 'no-cache' }
|
66
|
+
)
|
65
67
|
|
66
68
|
Make.find(12, no_cache: true)
|
67
69
|
end
|
68
70
|
end
|
69
71
|
|
70
|
-
describe
|
71
|
-
it
|
72
|
-
expected_response = RestfulResource::Response.new(body: [{name: 'Golf', price:
|
73
|
-
expect_get(
|
72
|
+
describe '#where' do
|
73
|
+
it 'returns an array of objects' do
|
74
|
+
expected_response = RestfulResource::Response.new(body: [{ name: 'Golf', price: 15_000 }, { name: 'Polo', price: 11_000 }].to_json)
|
75
|
+
expect_get('http://api.carwow.co.uk/groups/15/makes/Volkswagen/models?on_sale=true', expected_response)
|
74
76
|
object = Model.where(make_slug: 'Volkswagen', on_sale: true, group_id: 15)
|
75
77
|
|
76
78
|
expect(object).not_to be_nil
|
77
79
|
expect(object.length).to eq 2
|
78
80
|
expect(object.first.name).to eq 'Golf'
|
79
|
-
expect(object.first.price).to eq
|
81
|
+
expect(object.first.price).to eq 15_000
|
80
82
|
end
|
81
83
|
|
82
|
-
it
|
83
|
-
expected_response = response_with_page_information
|
84
|
-
expect_get(
|
84
|
+
it 'provides a paginated result if response contains rest pagination headers' do
|
85
|
+
expected_response = response_with_page_information
|
86
|
+
expect_get('http://api.carwow.co.uk/groups/15/makes/Volkswagen/models', expected_response)
|
85
87
|
|
86
88
|
models = Model.where(group_id: 15, make_slug: 'Volkswagen')
|
87
89
|
|
@@ -92,26 +94,28 @@ RSpec.describe RestfulResource::Base do
|
|
92
94
|
end
|
93
95
|
|
94
96
|
it 'accepts custom headers' do
|
95
|
-
expect_get(
|
97
|
+
expect_get('http://api.carwow.co.uk/groups/15/makes/Volkswagen/models?on_sale=true',
|
96
98
|
RestfulResource::Response.new,
|
97
|
-
headers: { cache_control: 'no-cache' }
|
99
|
+
headers: { cache_control: 'no-cache' }
|
100
|
+
)
|
98
101
|
|
99
102
|
Model.where(make_slug: 'Volkswagen', on_sale: true, group_id: 15, headers: { cache_control: 'no-cache' })
|
100
103
|
end
|
101
104
|
|
102
105
|
it 'accepts no_cache option' do
|
103
|
-
expect_get(
|
106
|
+
expect_get('http://api.carwow.co.uk/groups/15/makes/Volkswagen/models?on_sale=true',
|
104
107
|
RestfulResource::Response.new,
|
105
|
-
headers: { cache_control: 'no-cache' }
|
108
|
+
headers: { cache_control: 'no-cache' }
|
109
|
+
)
|
106
110
|
|
107
111
|
Model.where(make_slug: 'Volkswagen', on_sale: true, group_id: 15, no_cache: true)
|
108
112
|
end
|
109
113
|
end
|
110
114
|
|
111
|
-
describe
|
112
|
-
it
|
113
|
-
expected_response = RestfulResource::Response.new(body: [{name: 'Volkswagen'}, {name: 'Audi'}].to_json)
|
114
|
-
expect_get(
|
115
|
+
describe '#all' do
|
116
|
+
it 'returns all items' do
|
117
|
+
expected_response = RestfulResource::Response.new(body: [{ name: 'Volkswagen' }, { name: 'Audi' }].to_json)
|
118
|
+
expect_get('http://api.carwow.co.uk/makes', expected_response)
|
115
119
|
makes = Make.all
|
116
120
|
|
117
121
|
expect(makes).not_to be_nil
|
@@ -120,47 +124,49 @@ RSpec.describe RestfulResource::Base do
|
|
120
124
|
end
|
121
125
|
|
122
126
|
it 'accepts custom headers' do
|
123
|
-
expect_get(
|
127
|
+
expect_get('http://api.carwow.co.uk/makes',
|
124
128
|
RestfulResource::Response.new,
|
125
|
-
headers: { cache_control: 'no-cache' }
|
129
|
+
headers: { cache_control: 'no-cache' }
|
130
|
+
)
|
126
131
|
|
127
132
|
Make.all(headers: { cache_control: 'no-cache' })
|
128
133
|
end
|
129
134
|
|
130
135
|
it 'accepts no_cache option' do
|
131
|
-
expect_get(
|
136
|
+
expect_get('http://api.carwow.co.uk/makes',
|
132
137
|
RestfulResource::Response.new,
|
133
|
-
headers: { cache_control: 'no-cache' }
|
138
|
+
headers: { cache_control: 'no-cache' }
|
139
|
+
)
|
134
140
|
|
135
141
|
Make.all(no_cache: true)
|
136
142
|
end
|
137
143
|
end
|
138
144
|
|
139
|
-
describe
|
140
|
-
it
|
141
|
-
BaseA.configure(base_url:
|
145
|
+
describe '#base_url' do
|
146
|
+
it 'is different for each subclass of Base' do
|
147
|
+
BaseA.configure(base_url: 'http://a.carwow.co.uk')
|
142
148
|
|
143
|
-
BaseB.configure(base_url:
|
149
|
+
BaseB.configure(base_url: 'http://b.carwow.co.uk')
|
144
150
|
|
145
|
-
expect_get('http://a.carwow.co.uk/testa/1', RestfulResource::Response.new
|
146
|
-
expect_get('http://b.carwow.co.uk/testb/2', RestfulResource::Response.new
|
151
|
+
expect_get('http://a.carwow.co.uk/testa/1', RestfulResource::Response.new)
|
152
|
+
expect_get('http://b.carwow.co.uk/testb/2', RestfulResource::Response.new)
|
147
153
|
|
148
154
|
TestA.find(1)
|
149
155
|
TestB.find(2)
|
150
156
|
end
|
151
157
|
end
|
152
158
|
|
153
|
-
describe
|
154
|
-
it
|
155
|
-
expect_get('http://api.carwow.co.uk/makes/15/lazy', RestfulResource::Response.new(body: {name: 'Volk.'}.to_json))
|
159
|
+
describe '#action' do
|
160
|
+
it 'retrieves a resource using a custom action' do
|
161
|
+
expect_get('http://api.carwow.co.uk/makes/15/lazy', RestfulResource::Response.new(body: { name: 'Volk.' }.to_json))
|
156
162
|
|
157
163
|
make = Make.action(:lazy).find(15)
|
158
164
|
|
159
165
|
expect(make.name).to eq 'Volk.'
|
160
166
|
end
|
161
167
|
|
162
|
-
it '
|
163
|
-
expect_get('http://api.carwow.co.uk/makes/available', RestfulResource::Response.new(body: [{name: 'Audi'}, {name: 'Fiat'}].to_json))
|
168
|
+
it 'retrieves many resources using a custom action' do
|
169
|
+
expect_get('http://api.carwow.co.uk/makes/available', RestfulResource::Response.new(body: [{ name: 'Audi' }, { name: 'Fiat' }].to_json))
|
164
170
|
|
165
171
|
make = Make.action(:available).all
|
166
172
|
|
@@ -169,36 +175,38 @@ RSpec.describe RestfulResource::Base do
|
|
169
175
|
end
|
170
176
|
end
|
171
177
|
|
172
|
-
describe
|
173
|
-
it
|
174
|
-
expected_response = RestfulResource::Response.new(body: {average_score: 4.3}.to_json, status: 200)
|
178
|
+
describe '#get' do
|
179
|
+
it 'returns an open_object' do
|
180
|
+
expected_response = RestfulResource::Response.new(body: { average_score: 4.3 }.to_json, status: 200)
|
175
181
|
expect_get('http://api.carwow.co.uk/makes/average_score?make_slug%5B%5D=Volkswagen&make_slug%5B%5D=Audi', expected_response)
|
176
182
|
|
177
|
-
object = Make.action(:average_score).get(make_slug: [
|
183
|
+
object = Make.action(:average_score).get(make_slug: %w[Volkswagen Audi])
|
178
184
|
|
179
185
|
expect(object.average_score).to eq 4.3
|
180
186
|
end
|
181
187
|
|
182
188
|
it 'accepts custom headers' do
|
183
|
-
expect_get(
|
189
|
+
expect_get('http://api.carwow.co.uk/makes/average_score',
|
184
190
|
RestfulResource::Response.new,
|
185
|
-
headers: { cache_control: 'no-cache' }
|
191
|
+
headers: { cache_control: 'no-cache' }
|
192
|
+
)
|
186
193
|
|
187
194
|
Make.action(:average_score).get(headers: { cache_control: 'no-cache' })
|
188
195
|
end
|
189
196
|
|
190
197
|
it 'accepts no_cache option' do
|
191
|
-
expect_get(
|
198
|
+
expect_get('http://api.carwow.co.uk/makes/average_score',
|
192
199
|
RestfulResource::Response.new,
|
193
|
-
headers: { cache_control: 'no-cache' }
|
200
|
+
headers: { cache_control: 'no-cache' }
|
201
|
+
)
|
194
202
|
|
195
203
|
Make.action(:average_score).get(no_cache: true)
|
196
204
|
end
|
197
205
|
end
|
198
206
|
|
199
|
-
describe
|
200
|
-
it '
|
201
|
-
expected_response = RestfulResource::Response.new(body: {name: 'Audi'}.to_json, status: 200)
|
207
|
+
describe '#put' do
|
208
|
+
it 'puts no data with no params' do
|
209
|
+
expected_response = RestfulResource::Response.new(body: { name: 'Audi' }.to_json, status: 200)
|
202
210
|
expect_put('http://api.carwow.co.uk/makes/1', expected_response)
|
203
211
|
|
204
212
|
object = Make.put(1)
|
@@ -206,8 +214,8 @@ RSpec.describe RestfulResource::Base do
|
|
206
214
|
expect(object.name).to eq 'Audi'
|
207
215
|
end
|
208
216
|
|
209
|
-
it '
|
210
|
-
expected_response = RestfulResource::Response.new(body: {name: 'Audi'}.to_json, status: 200)
|
217
|
+
it 'puts no data with no params passed' do
|
218
|
+
expected_response = RestfulResource::Response.new(body: { name: 'Audi' }.to_json, status: 200)
|
211
219
|
expect_put('http://api.carwow.co.uk/makes/1?make_slug=Volkswagen', expected_response)
|
212
220
|
|
213
221
|
object = Make.put(1, make_slug: 'Volkswagen')
|
@@ -215,21 +223,21 @@ RSpec.describe RestfulResource::Base do
|
|
215
223
|
expect(object.name).to eq 'Audi'
|
216
224
|
end
|
217
225
|
|
218
|
-
it '
|
219
|
-
data = {make_slug: 'Audi'}
|
226
|
+
it 'puts data with params passed' do
|
227
|
+
data = { make_slug: 'Audi' }
|
220
228
|
|
221
|
-
expected_response = RestfulResource::Response.new(body: {name: 'Audi'}.to_json, status: 200)
|
229
|
+
expected_response = RestfulResource::Response.new(body: { name: 'Audi' }.to_json, status: 200)
|
222
230
|
expect_put('http://api.carwow.co.uk/makes/1', expected_response, data: data)
|
223
231
|
|
224
|
-
object = Make.put(1, data: {make_slug: 'Audi'})
|
232
|
+
object = Make.put(1, data: { make_slug: 'Audi' })
|
225
233
|
|
226
234
|
expect(object.name).to eq 'Audi'
|
227
235
|
end
|
228
236
|
|
229
|
-
it '
|
230
|
-
data = {make_slug: 'Audi'}
|
237
|
+
it 'puts data with params passed' do
|
238
|
+
data = { make_slug: 'Audi' }
|
231
239
|
|
232
|
-
expected_response = RestfulResource::Response.new(body: {name: 'Audi'}.to_json, status: 200)
|
240
|
+
expected_response = RestfulResource::Response.new(body: { name: 'Audi' }.to_json, status: 200)
|
233
241
|
expect_put('http://api.carwow.co.uk/makes/1?make_slug=Volkswagen', expected_response, data: data)
|
234
242
|
|
235
243
|
object = Make.put(1, data: data, make_slug: 'Volkswagen')
|
@@ -238,17 +246,18 @@ RSpec.describe RestfulResource::Base do
|
|
238
246
|
end
|
239
247
|
|
240
248
|
it 'accepts custom headers' do
|
241
|
-
expect_put(
|
249
|
+
expect_put('http://api.carwow.co.uk/makes/1',
|
242
250
|
RestfulResource::Response.new,
|
243
|
-
headers: { accept: 'application/json' }
|
251
|
+
headers: { accept: 'application/json' }
|
252
|
+
)
|
244
253
|
|
245
254
|
Make.put(1, data: {}, headers: { accept: 'application/json' })
|
246
255
|
end
|
247
256
|
end
|
248
257
|
|
249
|
-
describe
|
250
|
-
it
|
251
|
-
data = {slug: 'audi-make', name: 'Audi', num_of_cars: 3}
|
258
|
+
describe '#post' do
|
259
|
+
it 'posts parameters to the collection url' do
|
260
|
+
data = { slug: 'audi-make', name: 'Audi', num_of_cars: 3 }
|
252
261
|
|
253
262
|
expected_response = RestfulResource::Response.new(body: data.to_json, status: 200)
|
254
263
|
expect_post('http://api.carwow.co.uk/makes', expected_response, data: data)
|
@@ -260,17 +269,18 @@ RSpec.describe RestfulResource::Base do
|
|
260
269
|
end
|
261
270
|
|
262
271
|
it 'accepts custom headers' do
|
263
|
-
expect_post(
|
272
|
+
expect_post('http://api.carwow.co.uk/makes',
|
264
273
|
RestfulResource::Response.new,
|
265
|
-
headers: { accept: 'application/json' }
|
274
|
+
headers: { accept: 'application/json' }
|
275
|
+
)
|
266
276
|
|
267
277
|
Make.post(data: {}, headers: { accept: 'application/json' })
|
268
278
|
end
|
269
279
|
end
|
270
280
|
|
271
|
-
describe
|
272
|
-
it
|
273
|
-
expected_response = RestfulResource::Response.new(body: {deleted: true}.to_json, status: 200)
|
281
|
+
describe '#delete' do
|
282
|
+
it 'deletes to the member url' do
|
283
|
+
expected_response = RestfulResource::Response.new(body: { deleted: true }.to_json, status: 200)
|
274
284
|
expect_delete('http://api.carwow.co.uk/makes/1', expected_response)
|
275
285
|
|
276
286
|
object = Make.delete(1)
|
@@ -279,68 +289,75 @@ RSpec.describe RestfulResource::Base do
|
|
279
289
|
end
|
280
290
|
|
281
291
|
it 'accepts custom headers' do
|
282
|
-
expect_delete(
|
292
|
+
expect_delete('http://api.carwow.co.uk/makes/1',
|
283
293
|
RestfulResource::Response.new,
|
284
|
-
headers: { accept: 'application/json' }
|
294
|
+
headers: { accept: 'application/json' }
|
295
|
+
)
|
285
296
|
|
286
297
|
Make.delete(1, headers: { accept: 'application/json' })
|
287
298
|
end
|
288
299
|
end
|
289
300
|
|
290
|
-
describe
|
291
|
-
before
|
292
|
-
expected_response = RestfulResource::Response.new(body: [{name: 'Audi', slug: 'Audi-Slug'}, {name: 'Fiat', slug: 'Fiat-Slug'}].to_json)
|
301
|
+
describe '.as_json' do
|
302
|
+
before do
|
303
|
+
expected_response = RestfulResource::Response.new(body: [{ name: 'Audi', slug: 'Audi-Slug' }, { name: 'Fiat', slug: 'Fiat-Slug' }].to_json)
|
293
304
|
expect_get('http://api.carwow.co.uk/makes', expected_response)
|
294
305
|
|
295
306
|
@makes = Make.all
|
296
307
|
end
|
297
308
|
|
298
|
-
it '
|
299
|
-
expect(@makes.first.as_json).to eq ({'name' => 'Audi', 'slug' => 'Audi-Slug'})
|
309
|
+
it 'does not return inner object table' do
|
310
|
+
expect(@makes.first.as_json).to eq ({ 'name' => 'Audi', 'slug' => 'Audi-Slug' })
|
300
311
|
end
|
301
312
|
|
302
|
-
it '
|
303
|
-
expect(@makes.last.as_json(only: [:name])).to eq ({'name' => 'Fiat'})
|
313
|
+
it 'returns inner object table on selected fields' do
|
314
|
+
expect(@makes.last.as_json(only: [:name])).to eq ({ 'name' => 'Fiat' })
|
304
315
|
end
|
305
316
|
end
|
306
317
|
|
307
|
-
describe
|
308
|
-
it
|
309
|
-
expect{
|
318
|
+
describe '.member_url' do
|
319
|
+
it 'requires a member ID' do
|
320
|
+
expect { described_class.member_url('', {}) }.to raise_error(RestfulResource::ResourceIdMissingError)
|
310
321
|
end
|
311
322
|
end
|
312
323
|
|
313
|
-
describe
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
324
|
+
describe '.configure' do
|
325
|
+
let(:username) { double }
|
326
|
+
let(:password) { double }
|
327
|
+
let(:auth_token) { double }
|
328
|
+
let(:logger) { double }
|
329
|
+
let(:cache_store) { double }
|
330
|
+
let(:instrumentation) { double }
|
331
|
+
let(:faraday_config) { double }
|
332
|
+
let(:faraday_options) { double }
|
333
|
+
|
334
|
+
it 'passes arguments to HttpClient' do
|
323
335
|
expect(RestfulResource::HttpClient).to receive(:new).with(username: username,
|
324
336
|
password: password,
|
325
337
|
auth_token: auth_token,
|
326
338
|
logger: logger,
|
327
339
|
cache_store: cache_store,
|
328
340
|
instrumentation: instrumentation,
|
329
|
-
faraday_config: faraday_config
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
341
|
+
faraday_config: faraday_config,
|
342
|
+
faraday_options: faraday_options
|
343
|
+
)
|
344
|
+
|
345
|
+
described_class.configure(base_url: 'http://foo.bar',
|
346
|
+
username: username,
|
347
|
+
password: password,
|
348
|
+
auth_token: auth_token,
|
349
|
+
logger: logger,
|
350
|
+
cache_store: cache_store,
|
351
|
+
instrumentation: instrumentation,
|
352
|
+
faraday_config: faraday_config,
|
353
|
+
faraday_options: faraday_options
|
354
|
+
)
|
339
355
|
end
|
340
356
|
end
|
341
357
|
|
342
358
|
def response_with_page_information
|
343
|
-
RestfulResource::Response.new(body: [{ id: 1, name: 'Golf'}, { id: 2, name: 'Polo' }].to_json,
|
344
|
-
|
359
|
+
RestfulResource::Response.new(body: [{ id: 1, name: 'Golf' }, { id: 2, name: 'Polo' }].to_json,
|
360
|
+
headers: { links: '<http://api.carwow.co.uk/makes/Volkswagen/models.json?page=6>;rel="last",<http://api.carwow.co.uk/makes/Volkswagen/models.json?page=2>;rel="next"' }
|
361
|
+
)
|
345
362
|
end
|
346
363
|
end
|
@@ -2,21 +2,19 @@ require_relative '../spec_helper'
|
|
2
2
|
|
3
3
|
describe RestfulResource::HttpClient do
|
4
4
|
def find_middleware(adapter, name)
|
5
|
-
adapter.builder.handlers.find {|m| m.name == name }
|
6
|
-
|
7
|
-
|
8
|
-
raise "Could not find Faraday middleware: #{name}"
|
5
|
+
adapter.builder.handlers.find { |m| m.name == name }
|
6
|
+
rescue StandardError
|
7
|
+
raise "Could not find Faraday middleware: #{name}"
|
9
8
|
end
|
10
9
|
|
11
10
|
def find_middleware_args(adapter, name)
|
12
|
-
find_middleware(adapter, name).instance_variable_get(
|
13
|
-
|
14
|
-
|
15
|
-
raise "Could not find args for Faraday middleware: #{name}"
|
11
|
+
find_middleware(adapter, name).instance_variable_get('@args').first
|
12
|
+
rescue StandardError
|
13
|
+
raise "Could not find args for Faraday middleware: #{name}"
|
16
14
|
end
|
17
15
|
|
18
16
|
describe 'Configuration' do
|
19
|
-
let(:connection) { described_class.new.instance_variable_get(
|
17
|
+
let(:connection) { described_class.new.instance_variable_get('@connection') }
|
20
18
|
let(:middleware) { connection.builder.handlers }
|
21
19
|
|
22
20
|
describe 'Builder configuration' do
|
@@ -48,7 +46,7 @@ describe RestfulResource::HttpClient do
|
|
48
46
|
end
|
49
47
|
|
50
48
|
context 'with an api_name' do
|
51
|
-
let(:connection) { described_class.new(instrumentation: { api_name: 'my_api_name'}).instance_variable_get(
|
49
|
+
let(:connection) { described_class.new(instrumentation: { api_name: 'my_api_name' }).instance_variable_get('@connection') }
|
52
50
|
|
53
51
|
it 'uses default instrumenter with the api_name' do
|
54
52
|
expect(find_middleware_args(connection, 'FaradayMiddleware::Instrumentation')).to include(name: 'http.my_api_name')
|
@@ -56,7 +54,7 @@ describe RestfulResource::HttpClient do
|
|
56
54
|
end
|
57
55
|
|
58
56
|
context 'with a custom instrumentation key' do
|
59
|
-
let(:connection) { described_class.new(instrumentation: { request_instrument_name: 'foo.bar'}).instance_variable_get(
|
57
|
+
let(:connection) { described_class.new(instrumentation: { request_instrument_name: 'foo.bar' }).instance_variable_get('@connection') }
|
60
58
|
|
61
59
|
it 'uses default instrumenter with the custom key' do
|
62
60
|
expect(find_middleware_args(connection, 'FaradayMiddleware::Instrumentation')).to include(name: 'foo.bar')
|
@@ -66,7 +64,9 @@ describe RestfulResource::HttpClient do
|
|
66
64
|
context 'with a given Metrics class' do
|
67
65
|
class FakeMetrics
|
68
66
|
def count(name, value); end
|
67
|
+
|
69
68
|
def sample(name, value); end
|
69
|
+
|
70
70
|
def measure(name, value); end
|
71
71
|
end
|
72
72
|
|
@@ -78,7 +78,7 @@ describe RestfulResource::HttpClient do
|
|
78
78
|
end
|
79
79
|
|
80
80
|
it 'initializes the Instrumentation' do
|
81
|
-
described_class.new(instrumentation: { app_name: 'rails', api_name: 'api', metric_class: FakeMetrics})
|
81
|
+
described_class.new(instrumentation: { app_name: 'rails', api_name: 'api', metric_class: FakeMetrics })
|
82
82
|
|
83
83
|
expect(RestfulResource::Instrumentation).to have_received(:new)
|
84
84
|
.with(app_name: 'rails',
|
@@ -86,11 +86,12 @@ describe RestfulResource::HttpClient do
|
|
86
86
|
request_instrument_name: 'http.api',
|
87
87
|
cache_instrument_name: 'http_cache.api',
|
88
88
|
server_cache_instrument_name: 'cdn_metrics.api',
|
89
|
-
metric_class: FakeMetrics
|
89
|
+
metric_class: FakeMetrics
|
90
|
+
)
|
90
91
|
end
|
91
92
|
|
92
93
|
it 'subscribes to the notifications' do
|
93
|
-
described_class.new(instrumentation: { app_name: 'rails', api_name: 'api', metric_class: FakeMetrics})
|
94
|
+
described_class.new(instrumentation: { app_name: 'rails', api_name: 'api', metric_class: FakeMetrics })
|
94
95
|
|
95
96
|
expect(mock_instrumention).to have_received(:subscribe_to_notifications)
|
96
97
|
end
|
@@ -98,7 +99,7 @@ describe RestfulResource::HttpClient do
|
|
98
99
|
end
|
99
100
|
|
100
101
|
describe 'when provided a logger' do
|
101
|
-
let(:connection) { described_class.new(logger: logger).instance_variable_get(
|
102
|
+
let(:connection) { described_class.new(logger: logger).instance_variable_get('@connection') }
|
102
103
|
let(:logger) { Logger.new('/dev/null') }
|
103
104
|
|
104
105
|
it 'uses the logger middleware' do
|
@@ -111,7 +112,7 @@ describe RestfulResource::HttpClient do
|
|
111
112
|
end
|
112
113
|
|
113
114
|
describe 'when provided a cache store' do
|
114
|
-
let(:connection) { described_class.new(cache_store: 'redis').instance_variable_get(
|
115
|
+
let(:connection) { described_class.new(cache_store: 'redis').instance_variable_get('@connection') }
|
115
116
|
|
116
117
|
it 'uses the cache_store middleware' do
|
117
118
|
expect(middleware).to include Faraday::HttpCache
|
@@ -122,7 +123,7 @@ describe RestfulResource::HttpClient do
|
|
122
123
|
end
|
123
124
|
|
124
125
|
context 'and an api_name is provided' do
|
125
|
-
let(:connection) { described_class.new(cache_store: 'redis', instrumentation: { api_name: 'my_api_name'}).instance_variable_get(
|
126
|
+
let(:connection) { described_class.new(cache_store: 'redis', instrumentation: { api_name: 'my_api_name' }).instance_variable_get('@connection') }
|
126
127
|
|
127
128
|
it 'passes the instrumenter and the api_name' do
|
128
129
|
expect(find_middleware_args(connection, 'Faraday::HttpCache')).to include(instrumenter: ActiveSupport::Notifications, instrument_name: 'http_cache.my_api_name')
|
@@ -130,7 +131,7 @@ describe RestfulResource::HttpClient do
|
|
130
131
|
end
|
131
132
|
|
132
133
|
context 'and a custom instrument name is provided' do
|
133
|
-
let(:connection) { described_class.new(cache_store: 'redis', instrumentation: { cache_instrument_name: 'foo.bar'}).instance_variable_get(
|
134
|
+
let(:connection) { described_class.new(cache_store: 'redis', instrumentation: { cache_instrument_name: 'foo.bar' }).instance_variable_get('@connection') }
|
134
135
|
|
135
136
|
it 'passes the instrumenter to the http cache middleware' do
|
136
137
|
expect(find_middleware_args(connection, 'Faraday::HttpCache')).to include(instrumenter: ActiveSupport::Notifications, instrument_name: 'foo.bar')
|
@@ -142,7 +143,7 @@ describe RestfulResource::HttpClient do
|
|
142
143
|
let(:faraday_config_block) do
|
143
144
|
proc { |conn| @block_arg = conn }
|
144
145
|
end
|
145
|
-
let(:connection) { described_class.new(faraday_config: faraday_config_block).instance_variable_get(
|
146
|
+
let(:connection) { described_class.new(faraday_config: faraday_config_block).instance_variable_get('@connection') }
|
146
147
|
|
147
148
|
it 'passes faraday connection instance and calls it' do
|
148
149
|
connection
|