active_rest_client 1.0.6 → 1.0.7
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/.travis.yml +1 -0
- data/README.md +26 -5
- data/active_rest_client.gemspec +2 -2
- data/lib/active_rest_client/base.rb +31 -0
- data/lib/active_rest_client/configuration.rb +36 -0
- data/lib/active_rest_client/request.rb +34 -1
- data/lib/active_rest_client/result_iterator.rb +18 -0
- data/lib/active_rest_client/version.rb +1 -1
- data/spec/lib/base_spec.rb +68 -20
- data/spec/lib/caching_spec.rb +23 -23
- data/spec/lib/configuration_spec.rb +38 -14
- data/spec/lib/instrumentation_spec.rb +7 -8
- data/spec/lib/lazy_association_loader_spec.rb +7 -7
- data/spec/lib/lazy_loader_spec.rb +5 -5
- data/spec/lib/logger_spec.rb +13 -13
- data/spec/lib/proxy_spec.rb +15 -15
- data/spec/lib/recording_spec.rb +2 -2
- data/spec/lib/request_spec.rb +133 -98
- data/spec/lib/result_iterator_spec.rb +34 -5
- data/spec/lib/validation_spec.rb +1 -1
- data/spec/spec_helper.rb +11 -2
- metadata +6 -6
data/spec/lib/recording_spec.rb
CHANGED
@@ -5,11 +5,11 @@ describe ActiveRestClient::Recording do
|
|
5
5
|
class MyObject1
|
6
6
|
include ActiveRestClient::Recording
|
7
7
|
end
|
8
|
-
expect(MyObject1.record_response?).to
|
8
|
+
expect(MyObject1.record_response?).to be_falsey
|
9
9
|
MyObject1.record_response do
|
10
10
|
puts "Hello world"
|
11
11
|
end
|
12
|
-
expect(MyObject1.record_response?).to
|
12
|
+
expect(MyObject1.record_response?).to be_truthy
|
13
13
|
end
|
14
14
|
|
15
15
|
it "remembers a block given to it to later be called back" do
|
data/spec/lib/request_spec.rb
CHANGED
@@ -22,9 +22,12 @@ describe ActiveRestClient::Request do
|
|
22
22
|
get :all, "/", :has_many => {:expenses => ExampleOtherClient}
|
23
23
|
get :babies, "/babies", :has_many => {:children => ExampleOtherClient}
|
24
24
|
get :headers, "/headers"
|
25
|
+
put :headers_default, "/headers_default"
|
26
|
+
put :headers_json, "/headers_json", request_body_type: :json
|
25
27
|
get :find, "/:id"
|
26
28
|
get :change, "/change"
|
27
29
|
post :create, "/create"
|
30
|
+
post :test_encoding, "/encoding", request_body_type: :json
|
28
31
|
put :update, "/put/:id"
|
29
32
|
delete :remove, "/remove/:id"
|
30
33
|
get :hal, "/hal", fake:"{\"_links\":{\"child\": {\"href\": \"/child/1\"}, \"other\": {\"href\": \"/other/1\"}, \"cars\":[{\"href\": \"/car/1\", \"name\":\"car1\"}, {\"href\": \"/car/2\", \"name\":\"car2\"}, {\"href\": \"/car/not-embed\", \"name\":\"car_not_embed\"} ], \"lazy\": {\"href\": \"/lazy/load\"}, \"invalid\": [{\"href\": \"/invalid/1\"}]}, \"_embedded\":{\"other\":{\"name\":\"Jane\"},\"child\":{\"name\":\"Billy\"}, \"cars\":[{\"_links\": {\"self\": {\"href\": \"/car/1\"} }, \"make\": \"Bugatti\", \"model\": \"Veyron\"}, {\"_links\": {\"self\": {\"href\": \"/car/2\"} }, \"make\": \"Ferrari\", \"model\": \"F458 Italia\"} ], \"invalid\": [{\"present\":true, \"_links\": {} } ] } }", has_many:{other:ExampleOtherClient}
|
@@ -33,6 +36,13 @@ describe ActiveRestClient::Request do
|
|
33
36
|
get :defaults, "/defaults", defaults:{overwrite:"no", persist:"yes"}
|
34
37
|
end
|
35
38
|
|
39
|
+
class AuthenticatedExampleClient < ActiveRestClient::Base
|
40
|
+
base_url "http://www.example.com"
|
41
|
+
username "john"
|
42
|
+
password "smith"
|
43
|
+
get :all, "/"
|
44
|
+
end
|
45
|
+
|
36
46
|
class LazyLoadedExampleClient < ExampleClient
|
37
47
|
base_url "http://www.example.com"
|
38
48
|
lazy_load!
|
@@ -56,86 +66,109 @@ describe ActiveRestClient::Request do
|
|
56
66
|
post :save, "/save"
|
57
67
|
end
|
58
68
|
|
59
|
-
ActiveRestClient::Request.
|
69
|
+
allow_any_instance_of(ActiveRestClient::Request).to receive(:read_cached_response)
|
60
70
|
end
|
61
71
|
|
62
72
|
it "should get an HTTP connection when called" do
|
63
73
|
connection = double(ActiveRestClient::Connection).as_null_object
|
64
|
-
ActiveRestClient::ConnectionManager.
|
65
|
-
connection.
|
74
|
+
expect(ActiveRestClient::ConnectionManager).to receive(:get_connection).with("http://www.example.com").and_return(connection)
|
75
|
+
expect(connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
66
76
|
ExampleClient.all
|
67
77
|
end
|
68
78
|
|
79
|
+
it "should get an HTTP connection with authentication when called" do
|
80
|
+
connection = double(ActiveRestClient::Connection).as_null_object
|
81
|
+
expect(ActiveRestClient::ConnectionManager).to receive(:get_connection).with("http://john:smith@www.example.com").and_return(connection)
|
82
|
+
expect(connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
83
|
+
AuthenticatedExampleClient.all
|
84
|
+
end
|
85
|
+
|
69
86
|
it "should get an HTTP connection when called and call get on it" do
|
70
|
-
ActiveRestClient::Connection.
|
87
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
71
88
|
ExampleClient.all
|
72
89
|
end
|
73
90
|
|
74
91
|
it "should get an HTTP connection when called and call delete on it" do
|
75
|
-
ActiveRestClient::Connection.
|
92
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:delete).with("/remove/1", an_instance_of(Hash)).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
76
93
|
ExampleClient.remove(id:1)
|
77
94
|
end
|
78
95
|
|
79
96
|
it "should work with faraday response objects" do
|
80
97
|
response = Faraday::Response.new
|
81
|
-
response.
|
82
|
-
ActiveRestClient::Connection.
|
98
|
+
allow(response).to receive(:body).and_return({}.to_json)
|
99
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).and_return(response)
|
83
100
|
expect { ExampleClient.all }.to_not raise_error
|
84
101
|
end
|
85
102
|
|
86
103
|
it "should pass through get parameters" do
|
87
|
-
ActiveRestClient::Connection.
|
104
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/?debug=true", an_instance_of(Hash)).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
88
105
|
ExampleClient.all debug:true
|
89
106
|
end
|
90
107
|
|
91
108
|
it "should pass through get parameters, using defaults specified" do
|
92
|
-
ActiveRestClient::Connection.
|
109
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/defaults?overwrite=yes&persist=yes", an_instance_of(Hash)).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
93
110
|
ExampleClient.defaults overwrite:"yes"
|
94
111
|
end
|
95
112
|
|
96
113
|
it "should pass through url parameters" do
|
97
|
-
ActiveRestClient::Connection.
|
114
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/1234", an_instance_of(Hash)).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
98
115
|
ExampleClient.find id:1234
|
99
116
|
end
|
100
117
|
|
101
118
|
it "should accept an integer as the only parameter and use it as id" do
|
102
|
-
ActiveRestClient::Connection.
|
119
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/1234", an_instance_of(Hash)).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
103
120
|
ExampleClient.find(1234)
|
104
121
|
end
|
105
122
|
|
106
123
|
it "should accept a string as the only parameter and use it as id" do
|
107
|
-
ActiveRestClient::Connection.
|
124
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/1234", an_instance_of(Hash)).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
108
125
|
ExampleClient.find("1234")
|
109
126
|
end
|
110
127
|
|
111
128
|
it "should pass through url parameters and get parameters" do
|
112
|
-
ActiveRestClient::Connection.
|
129
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/1234?debug=true", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", headers:{}))
|
113
130
|
ExampleClient.find id:1234, debug:true
|
114
131
|
end
|
115
132
|
|
116
133
|
it "should pass through url parameters and put parameters" do
|
117
|
-
ActiveRestClient::Connection.
|
134
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:put).with("/put/1234", "debug=true", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", headers:{}))
|
118
135
|
ExampleClient.update id:1234, debug:true
|
119
136
|
end
|
120
137
|
|
121
138
|
it "should encode the body in a form-encoded format by default" do
|
122
|
-
ActiveRestClient::Connection.
|
139
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:put).with("/put/1234", "debug=true&test=foo", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", headers:{}))
|
123
140
|
ExampleClient.update id:1234, debug:true, test:'foo'
|
124
141
|
end
|
125
142
|
|
126
143
|
it "should encode the body in a JSON format if specified" do
|
127
|
-
ActiveRestClient::Connection.
|
144
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:put).with("/put/1234", %q({"debug":true,"test":"foo"}), an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", headers:{}))
|
128
145
|
ExampleClient.request_body_type :json
|
129
146
|
ExampleClient.update id:1234, debug:true, test:'foo'
|
130
147
|
end
|
131
148
|
|
149
|
+
it "allows forcing a request_body_type per request" do
|
150
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:post).with("/encoding", %q({"id":1234,"test":"something"}), an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true}", headers:{}))
|
151
|
+
ExampleClient.request_body_type :form_encoded # Should be ignored and the per_method :json used
|
152
|
+
ExampleClient.test_encoding id:1234, test: "something"
|
153
|
+
end
|
154
|
+
|
132
155
|
it "should pass through custom headers" do
|
133
|
-
ActiveRestClient::Connection.
|
156
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/headers", hash_including("X-My-Header" => "myvalue")).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
134
157
|
ExampleClient.headers
|
135
158
|
end
|
136
159
|
|
160
|
+
it "should set request header with content-type for default" do
|
161
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:put).with("/headers_default", "", hash_including("Content-Type" => "application/x-www-form-urlencoded")).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
162
|
+
ExampleClient.headers_default
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should set request header with content-type for JSON" do
|
166
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:put).with("/headers_json", "{}", hash_including("Content-Type" => "application/json")).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
167
|
+
ExampleClient.headers_json
|
168
|
+
end
|
169
|
+
|
137
170
|
it "should parse JSON to give a nice object" do
|
138
|
-
ActiveRestClient::Connection.
|
171
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:put).with("/put/1234", "debug=true", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"result\":true, \"list\":[1,2,3,{\"test\":true}], \"created_at\":\"2012-03-04T01:02:03Z\", \"child\":{\"grandchild\":{\"test\":true}}}", headers:{}))
|
139
172
|
object = ExampleClient.update id:1234, debug:true
|
140
173
|
expect(object.result).to eq(true)
|
141
174
|
expect(object.list.first).to eq(1)
|
@@ -177,13 +210,13 @@ describe ActiveRestClient::Request do
|
|
177
210
|
end
|
178
211
|
|
179
212
|
it "should log faked responses" do
|
180
|
-
ActiveRestClient::Logger.
|
181
|
-
ActiveRestClient::Logger.
|
213
|
+
allow(ActiveRestClient::Logger).to receive(:debug)
|
214
|
+
expect(ActiveRestClient::Logger).to receive(:debug).with(/Faked response found/)
|
182
215
|
ExampleClient.fake id:1234, debug:true
|
183
216
|
end
|
184
217
|
|
185
218
|
it "should parse an array within JSON to be a result iterator" do
|
186
|
-
ActiveRestClient::Connection.
|
219
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:put).with("/put/1234", "debug=true", an_instance_of(Hash)).and_return(OpenStruct.new(body:"[{\"first_name\":\"Johnny\"}, {\"first_name\":\"Billy\"}]", status:200, headers:{}))
|
187
220
|
object = ExampleClient.update id:1234, debug:true
|
188
221
|
expect(object).to be_instance_of(ActiveRestClient::ResultIterator)
|
189
222
|
expect(object.first.first_name).to eq("Johnny")
|
@@ -192,21 +225,20 @@ describe ActiveRestClient::Request do
|
|
192
225
|
end
|
193
226
|
|
194
227
|
it "should instantiate other classes using has_many when required to do so" do
|
195
|
-
ActiveRestClient::Connection.
|
228
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"first_name\":\"Johnny\", \"expenses\":[{\"amount\":1}, {\"amount\":2}]}", status:200, headers:{}))
|
196
229
|
object = ExampleClient.all
|
197
230
|
expect(object.expenses.first).to be_instance_of(ExampleOtherClient)
|
198
231
|
end
|
199
232
|
|
200
233
|
it "should instantiate other classes using has_many even if nested off the root" do
|
201
|
-
ActiveRestClient::Connection.
|
234
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/babies", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"first_name\":\"Johnny\", \"children\":{\"eldest\":[{\"name\":\"Billy\"}]}}", status:200, headers:{}))
|
202
235
|
object = ExampleClient.babies
|
203
236
|
expect(object.children.eldest.first).to be_instance_of(ExampleOtherClient)
|
204
237
|
end
|
205
238
|
|
206
239
|
it "should assign new attributes to the existing object if possible" do
|
207
|
-
ActiveRestClient::Connection.
|
208
|
-
|
209
|
-
should_receive(:post).
|
240
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
241
|
+
to receive(:post).
|
210
242
|
with("/create", "first_name=John&should_disappear=true", an_instance_of(Hash)).
|
211
243
|
and_return(OpenStruct.new(body:"{\"first_name\":\"John\", \"id\":1234}", headers:{}))
|
212
244
|
object = ExampleClient.new(first_name:"John", should_disappear:true)
|
@@ -216,14 +248,27 @@ describe ActiveRestClient::Request do
|
|
216
248
|
expect(object.id).to eq(1234)
|
217
249
|
end
|
218
250
|
|
251
|
+
it "should expose etag if available" do
|
252
|
+
response = OpenStruct.new(body: "{}", headers: {"ETag" => "123456"}, status: 200)
|
253
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/123", an_instance_of(Hash)).and_return(response)
|
254
|
+
object = ExampleClient.find(123)
|
255
|
+
expect(object._etag).to eq("123456")
|
256
|
+
end
|
257
|
+
|
258
|
+
it "should expose all headers" do
|
259
|
+
response = OpenStruct.new(body: "{}", headers: {"X-Test-Header" => "true"}, status: 200)
|
260
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/123", an_instance_of(Hash)).and_return(response)
|
261
|
+
object = ExampleClient.find(123)
|
262
|
+
expect(object._headers["X-Test-Header"]).to eq("true")
|
263
|
+
end
|
264
|
+
|
219
265
|
it "should clearly pass through 200 status responses" do
|
220
|
-
ActiveRestClient::Connection.
|
221
|
-
|
222
|
-
should_receive(:post).
|
266
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
267
|
+
to receive(:post).
|
223
268
|
with("/create", "first_name=John&should_disappear=true", an_instance_of(Hash)).
|
224
269
|
and_return(OpenStruct.new(body:"{\"first_name\":\"John\", \"id\":1234}", headers:{}, status:200))
|
225
|
-
ActiveRestClient::Logger.
|
226
|
-
ActiveRestClient::Logger.
|
270
|
+
expect(ActiveRestClient::Logger).to receive(:info).with(%r'Requesting http://www.example.com/create')
|
271
|
+
expect(ActiveRestClient::Logger).to receive(:debug).at_least(1).times.with(%r'(Response received \d+ bytes|Trying to read from cache)')
|
227
272
|
|
228
273
|
object = ExampleClient.new(first_name:"John", should_disappear:true)
|
229
274
|
object.create
|
@@ -231,26 +276,24 @@ describe ActiveRestClient::Request do
|
|
231
276
|
end
|
232
277
|
|
233
278
|
it "should debug log 200 responses" do
|
234
|
-
ActiveRestClient::Connection.
|
235
|
-
|
236
|
-
should_receive(:post).
|
279
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
280
|
+
to receive(:post).
|
237
281
|
with("/create", "first_name=John&should_disappear=true", an_instance_of(Hash)).
|
238
282
|
and_return(OpenStruct.new(body:"{\"first_name\":\"John\", \"id\":1234}", headers:{}, status:200))
|
239
|
-
ActiveRestClient::Logger.
|
240
|
-
ActiveRestClient::Logger.
|
283
|
+
expect(ActiveRestClient::Logger).to receive(:info).with(%r'Requesting http://www.example.com/create')
|
284
|
+
expect(ActiveRestClient::Logger).to receive(:debug).at_least(1).times.with(%r'(Response received \d+ bytes|Trying to read from cache)')
|
241
285
|
|
242
286
|
object = ExampleClient.new(first_name:"John", should_disappear:true)
|
243
287
|
object.create
|
244
288
|
end
|
245
289
|
|
246
290
|
it "should verbose debug the with the right http verb" do
|
247
|
-
ActiveRestClient::Connection.
|
248
|
-
|
249
|
-
should_receive(:post).
|
291
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
292
|
+
to receive(:post).
|
250
293
|
with("/create", "first_name=John&should_disappear=true", an_instance_of(Hash)).
|
251
294
|
and_return(OpenStruct.new(body:"{\"first_name\":\"John\", \"id\":1234}", headers:{}, status:200))
|
252
|
-
ActiveRestClient::Logger.
|
253
|
-
ActiveRestClient::Logger.
|
295
|
+
expect(ActiveRestClient::Logger).to receive(:debug).with(/ POST /)
|
296
|
+
allow(ActiveRestClient::Logger).to receive(:debug).with(any_args)
|
254
297
|
|
255
298
|
object = VerboseExampleClient.new(first_name:"John", should_disappear:true)
|
256
299
|
object.create
|
@@ -258,19 +301,18 @@ describe ActiveRestClient::Request do
|
|
258
301
|
|
259
302
|
it "should verbose log if enabled" do
|
260
303
|
connection = double(ActiveRestClient::Connection).as_null_object
|
261
|
-
ActiveRestClient::ConnectionManager.
|
262
|
-
connection.
|
263
|
-
ActiveRestClient::Logger.
|
264
|
-
ActiveRestClient::Logger.
|
265
|
-
ActiveRestClient::Logger.
|
266
|
-
ActiveRestClient::Logger.
|
304
|
+
expect(ActiveRestClient::ConnectionManager).to receive(:get_connection).and_return(connection)
|
305
|
+
expect(connection).to receive(:get).with("/all", an_instance_of(Hash)).and_return(OpenStruct.new(body:'{"result":true}', headers:{"Content-Type" => "application/json", "Connection" => "close"}))
|
306
|
+
expect(ActiveRestClient::Logger).to receive(:debug).with("ActiveRestClient Verbose Log:")
|
307
|
+
expect(ActiveRestClient::Logger).to receive(:debug).with(/ >> /).at_least(:twice)
|
308
|
+
expect(ActiveRestClient::Logger).to receive(:debug).with(/ << /).at_least(:twice)
|
309
|
+
allow(ActiveRestClient::Logger).to receive(:debug).with(any_args)
|
267
310
|
VerboseExampleClient.all
|
268
311
|
end
|
269
312
|
|
270
313
|
it "should raise an unauthorised exception for 401 errors" do
|
271
|
-
ActiveRestClient::Connection.
|
272
|
-
|
273
|
-
should_receive(:post).
|
314
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
315
|
+
to receive(:post).
|
274
316
|
with("/create", "first_name=John&should_disappear=true", an_instance_of(Hash)).
|
275
317
|
and_return(OpenStruct.new(body:"{\"first_name\":\"John\", \"id\":1234}", headers:{}, status:401))
|
276
318
|
object = ExampleClient.new(first_name:"John", should_disappear:true)
|
@@ -285,9 +327,8 @@ describe ActiveRestClient::Request do
|
|
285
327
|
end
|
286
328
|
|
287
329
|
it "should raise a forbidden client exception for 403 errors" do
|
288
|
-
ActiveRestClient::Connection.
|
289
|
-
|
290
|
-
should_receive(:post).
|
330
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
331
|
+
to receive(:post).
|
291
332
|
with("/create", "first_name=John&should_disappear=true", an_instance_of(Hash)).
|
292
333
|
and_return(OpenStruct.new(body:"{\"first_name\":\"John\", \"id\":1234}", headers:{}, status:403))
|
293
334
|
object = ExampleClient.new(first_name:"John", should_disappear:true)
|
@@ -302,9 +343,8 @@ describe ActiveRestClient::Request do
|
|
302
343
|
end
|
303
344
|
|
304
345
|
it "should raise a not found client exception for 404 errors" do
|
305
|
-
ActiveRestClient::Connection.
|
306
|
-
|
307
|
-
should_receive(:post).
|
346
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
347
|
+
to receive(:post).
|
308
348
|
with("/create", "first_name=John&should_disappear=true", an_instance_of(Hash)).
|
309
349
|
and_return(OpenStruct.new(body:"{\"first_name\":\"John\", \"id\":1234}", headers:{}, status:404))
|
310
350
|
object = ExampleClient.new(first_name:"John", should_disappear:true)
|
@@ -319,9 +359,8 @@ describe ActiveRestClient::Request do
|
|
319
359
|
end
|
320
360
|
|
321
361
|
it "should raise a client exceptions for 4xx errors" do
|
322
|
-
ActiveRestClient::Connection.
|
323
|
-
|
324
|
-
should_receive(:post).
|
362
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
363
|
+
to receive(:post).
|
325
364
|
with("/create", "first_name=John&should_disappear=true", an_instance_of(Hash)).
|
326
365
|
and_return(OpenStruct.new(body:"{\"first_name\":\"John\", \"id\":1234}", headers:{}, status:409))
|
327
366
|
object = ExampleClient.new(first_name:"John", should_disappear:true)
|
@@ -336,9 +375,8 @@ describe ActiveRestClient::Request do
|
|
336
375
|
end
|
337
376
|
|
338
377
|
it "should raise a server exception for 5xx errors" do
|
339
|
-
ActiveRestClient::Connection.
|
340
|
-
|
341
|
-
should_receive(:post).
|
378
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
379
|
+
to receive(:post).
|
342
380
|
with("/create", "first_name=John&should_disappear=true", an_instance_of(Hash)).
|
343
381
|
and_return(OpenStruct.new(body:"{\"first_name\":\"John\", \"id\":1234}", headers:{}, status:500))
|
344
382
|
object = ExampleClient.new(first_name:"John", should_disappear:true)
|
@@ -354,9 +392,8 @@ describe ActiveRestClient::Request do
|
|
354
392
|
|
355
393
|
it "should raise a parse exception for invalid JSON returns" do
|
356
394
|
error_content = "<h1>500 Server Error</h1>"
|
357
|
-
ActiveRestClient::Connection.
|
358
|
-
|
359
|
-
should_receive(:post).
|
395
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
396
|
+
to receive(:post).
|
360
397
|
with("/create", "first_name=John&should_disappear=true", an_instance_of(Hash)).
|
361
398
|
and_return(OpenStruct.new(body:error_content, headers:{'Content-Type' => 'text/html'}, status:500))
|
362
399
|
object = ExampleClient.new(first_name:"John", should_disappear:true)
|
@@ -372,9 +409,8 @@ describe ActiveRestClient::Request do
|
|
372
409
|
|
373
410
|
it "should raise a bad request exception for 400 response status" do
|
374
411
|
error_content = "<h1>400 Bad Request</h1>"
|
375
|
-
ActiveRestClient::Connection.
|
376
|
-
|
377
|
-
should_receive(:post).
|
412
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
413
|
+
to receive(:post).
|
378
414
|
with("/create", "first_name=John&should_disappear=true", an_instance_of(Hash)).
|
379
415
|
and_return(OpenStruct.new(body:error_content, headers:{'Content-Type' => 'text/html'}, status:400))
|
380
416
|
object = ExampleClient.new(first_name:"John", should_disappear:true)
|
@@ -390,9 +426,8 @@ describe ActiveRestClient::Request do
|
|
390
426
|
|
391
427
|
it "should raise response parse exception for 200 response status and non json content type" do
|
392
428
|
error_content = "<h1>malformed json</h1>"
|
393
|
-
ActiveRestClient::Connection.
|
394
|
-
|
395
|
-
should_receive(:post).
|
429
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
430
|
+
to receive(:post).
|
396
431
|
with("/create", "first_name=John&should_disappear=true", an_instance_of(Hash)).
|
397
432
|
and_return(OpenStruct.new(body:error_content, headers:{'Content-Type' => 'text/html'}, status:200))
|
398
433
|
object = ExampleClient.new(first_name:"John", should_disappear:true)
|
@@ -407,9 +442,8 @@ describe ActiveRestClient::Request do
|
|
407
442
|
end
|
408
443
|
|
409
444
|
it "should not override the attributes of the existing object on error response status" do
|
410
|
-
ActiveRestClient::Connection.
|
411
|
-
|
412
|
-
should_receive(:post).
|
445
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
446
|
+
to receive(:post).
|
413
447
|
with("/create", "first_name=John&should_disappear=true", an_instance_of(Hash)).
|
414
448
|
and_return(OpenStruct.new(body:'{"errors": ["validation": "error in validation"]}', headers:{'Content-Type' => 'text/html'}, status:400))
|
415
449
|
object = ExampleClient.new(first_name:"John", should_disappear:true)
|
@@ -435,6 +469,8 @@ describe ActiveRestClient::Request do
|
|
435
469
|
"http://www.example.com/"
|
436
470
|
end
|
437
471
|
|
472
|
+
def username ; end
|
473
|
+
def password ; end
|
438
474
|
def name ; end
|
439
475
|
def _filter_request(*args) ; end
|
440
476
|
def verbose ; false ; end
|
@@ -446,20 +482,20 @@ describe ActiveRestClient::Request do
|
|
446
482
|
end
|
447
483
|
|
448
484
|
it "should send all class mapped methods through _filter_request" do
|
449
|
-
ActiveRestClient::Connection.
|
450
|
-
ExampleClient.
|
485
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"first_name\":\"Johnny\", \"expenses\":[{\"amount\":1}, {\"amount\":2}]}", status:200, headers:{}))
|
486
|
+
expect(ExampleClient).to receive(:_filter_request).with(any_args).exactly(2).times
|
451
487
|
ExampleClient.all
|
452
488
|
end
|
453
489
|
|
454
490
|
it "should send all instance mapped methods through _filter_request" do
|
455
|
-
ActiveRestClient::Connection.
|
456
|
-
ExampleClient.
|
491
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"first_name\":\"Johnny\", \"expenses\":[{\"amount\":1}, {\"amount\":2}]}", status:200, headers:{}))
|
492
|
+
expect(ExampleClient).to receive(:_filter_request).with(any_args).exactly(2).times
|
457
493
|
e = ExampleClient.new
|
458
494
|
e.all
|
459
495
|
end
|
460
496
|
|
461
497
|
it "should change the generated object if an after_filter changes it" do
|
462
|
-
ActiveRestClient::Connection.
|
498
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/change", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{\"first_name\":\"Johnny\", \"expenses\":[{\"amount\":1}, {\"amount\":2}]}", status:200, headers:{}))
|
463
499
|
obj = ExampleClient.change
|
464
500
|
expect(obj.test).to eq(1)
|
465
501
|
end
|
@@ -479,9 +515,8 @@ describe ActiveRestClient::Request do
|
|
479
515
|
|
480
516
|
it "should allow requests directly to URLs" do
|
481
517
|
ActiveRestClient::ConnectionManager.reset!
|
482
|
-
ActiveRestClient::Connection.
|
483
|
-
|
484
|
-
should_receive(:get).
|
518
|
+
expect_any_instance_of(ActiveRestClient::Connection).
|
519
|
+
to receive(:get).
|
485
520
|
with("/some/url", an_instance_of(Hash)).
|
486
521
|
and_return(OpenStruct.new(body:"{\"first_name\":\"John\", \"id\":1234}", headers:{}, status:200))
|
487
522
|
SameServerExampleClient.same_server
|
@@ -490,14 +525,14 @@ describe ActiveRestClient::Request do
|
|
490
525
|
it "should allow requests directly to URLs even if to different URLs" do
|
491
526
|
ActiveRestClient::ConnectionManager.reset!
|
492
527
|
connection = double("Connection")
|
493
|
-
connection.
|
494
|
-
|
528
|
+
expect(connection).
|
529
|
+
to receive(:get).
|
495
530
|
with("/some/url", an_instance_of(Hash)).
|
496
531
|
and_return(OpenStruct.new(body:"{}", headers:{}, status:304))
|
497
|
-
connection.
|
498
|
-
|
532
|
+
allow(connection).
|
533
|
+
to receive(:base_url).
|
499
534
|
and_return("http://other.example.com")
|
500
|
-
ActiveRestClient::ConnectionManager.
|
535
|
+
expect(ActiveRestClient::ConnectionManager).to receive(:find_connection_for_url).with(OtherServerExampleClient::URL).and_return(connection)
|
501
536
|
OtherServerExampleClient.other_server
|
502
537
|
end
|
503
538
|
|
@@ -505,9 +540,9 @@ describe ActiveRestClient::Request do
|
|
505
540
|
ActiveRestClient::ConnectionManager.reset!
|
506
541
|
connection = double("Connection")
|
507
542
|
allow(connection).to receive(:base_url).and_return("http://www.example.com")
|
508
|
-
ActiveRestClient::ConnectionManager.
|
509
|
-
connection.
|
510
|
-
|
543
|
+
expect(ActiveRestClient::ConnectionManager).to receive(:get_connection).with("http://www.example.com").and_return(connection)
|
544
|
+
expect(connection).
|
545
|
+
to receive(:get).
|
511
546
|
with("/v1/people", an_instance_of(Hash)).
|
512
547
|
and_return(OpenStruct.new(body:"{\"first_name\":\"John\", \"id\":1234}", headers:{}, status:200))
|
513
548
|
@obj = SameServerExampleClient._request('/people')
|
@@ -519,7 +554,7 @@ describe ActiveRestClient::Request do
|
|
519
554
|
let(:hal) { ExampleClient.hal }
|
520
555
|
|
521
556
|
it "should request a HAL response or plain JSON" do
|
522
|
-
ActiveRestClient::Connection.
|
557
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:get).with("/headers", hash_including("Accept" => "application/hal+json, application/json;q=0.5")).and_return(OpenStruct.new(body:'{"result":true}', headers:{}))
|
523
558
|
ExampleClient.headers
|
524
559
|
end
|
525
560
|
|
@@ -536,17 +571,17 @@ describe ActiveRestClient::Request do
|
|
536
571
|
fake_object = RequestFakeObject.new
|
537
572
|
request = ActiveRestClient::Request.new(method, fake_object, {})
|
538
573
|
request.instance_variable_set(:@response, OpenStruct.new(headers:{"Content-Type" => "application/hal+json"}))
|
539
|
-
expect(request.hal_response?).to
|
574
|
+
expect(request.hal_response?).to be_truthy
|
540
575
|
request.instance_variable_set(:@response, OpenStruct.new(headers:{"Content-Type" => "application/json"}))
|
541
|
-
expect(request.hal_response?).to
|
576
|
+
expect(request.hal_response?).to be_truthy
|
542
577
|
request.instance_variable_set(:@response, OpenStruct.new(headers:{"Content-Type" => "text/plain"}))
|
543
|
-
expect(request.hal_response?).to
|
578
|
+
expect(request.hal_response?).to be_falsey
|
544
579
|
request.instance_variable_set(:@response, OpenStruct.new(headers:{"Content-Type" => ["text/plain", "application/hal+json"]}))
|
545
|
-
expect(request.hal_response?).to
|
580
|
+
expect(request.hal_response?).to be_truthy
|
546
581
|
request.instance_variable_set(:@response, OpenStruct.new(headers:{"Content-Type" => ["text/plain", "application/json"]}))
|
547
|
-
expect(request.hal_response?).to
|
582
|
+
expect(request.hal_response?).to be_truthy
|
548
583
|
request.instance_variable_set(:@response, OpenStruct.new(headers:{"Content-Type" => ["text/plain"]}))
|
549
|
-
expect(request.hal_response?).to
|
584
|
+
expect(request.hal_response?).to be_falsey
|
550
585
|
end
|
551
586
|
|
552
587
|
it "should map _links in to the normal attributes" do
|
@@ -579,7 +614,7 @@ describe ActiveRestClient::Request do
|
|
579
614
|
end
|
580
615
|
|
581
616
|
it "replaces the body completely in a filter" do
|
582
|
-
ActiveRestClient::Connection.
|
617
|
+
expect_any_instance_of(ActiveRestClient::Connection).to receive(:post).with("/save", "{\"id\":1234,\"name\":\"john\"}", an_instance_of(Hash)).and_return(OpenStruct.new(body:"{}", headers:{}))
|
583
618
|
FilteredBodyExampleClient.save id:1234, name:'john'
|
584
619
|
end
|
585
620
|
end
|