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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/indirector/rest'
describe "a REST http call", :shared => true do
it "should accept a path" do
lambda { @search.send(@method, *@arguments) }.should_not raise_error(ArgumentError)
end
it "should require a path" do
lambda { @searcher.send(@method) }.should raise_error(ArgumentError)
end
it "should return the results of deserializing the response to the request" do
conn = mock 'connection'
conn.stubs(:put).returns @response
conn.stubs(:delete).returns @response
conn.stubs(:get).returns @response
Puppet::Network::HttpPool.stubs(:http_instance).returns conn
@searcher.expects(:deserialize).with(@response).returns "myobject"
@searcher.send(@method, *@arguments).should == 'myobject'
end
end
describe Puppet::Indirector::REST do
before do
Puppet::Indirector::Terminus.stubs(:register_terminus_class)
@model = stub('model', :supported_formats => %w{}, :convert_from => nil)
@instance = stub('model instance')
@indirection = stub('indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model)
Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)
@rest_class = Class.new(Puppet::Indirector::REST) do
def self.to_s
"This::Is::A::Test::Class"
end
end
@response = stub('mock response', :body => 'result', :code => "200")
@response.stubs(:[]).with('content-type').returns "text/plain"
@searcher = @rest_class.new
end
it "should have a method for specifying what setting a subclass should use to retrieve its server" do
@rest_class.should respond_to(:use_server_setting)
end
it "should use any specified setting to pick the server" do
@rest_class.expects(:server_setting).returns :servset
Puppet.settings.expects(:value).with(:servset).returns "myserver"
@rest_class.server.should == "myserver"
end
it "should default to :server for the server setting" do
@rest_class.expects(:server_setting).returns nil
Puppet.settings.expects(:value).with(:server).returns "myserver"
@rest_class.server.should == "myserver"
end
it "should have a method for specifying what setting a subclass should use to retrieve its port" do
@rest_class.should respond_to(:use_port_setting)
end
it "should use any specified setting to pick the port" do
@rest_class.expects(:port_setting).returns :servset
Puppet.settings.expects(:value).with(:servset).returns "321"
@rest_class.port.should == 321
end
it "should default to :port for the port setting" do
@rest_class.expects(:port_setting).returns nil
Puppet.settings.expects(:value).with(:masterport).returns "543"
@rest_class.port.should == 543
end
describe "when deserializing responses" do
it "should return nil if the response code is 404" do
response = mock 'response'
response.expects(:code).returns "404"
@searcher.deserialize(response).should be_nil
end
it "should fail if the response code is not in the 200s" do
@model.expects(:convert_from).never
response = mock 'response'
response.stubs(:code).returns "300"
response.stubs(:message).returns "There was a problem"
lambda { @searcher.deserialize(response) }.should raise_error(Net::HTTPError)
end
it "should return the results of converting from the format specified by the content-type header if the response code is in the 200s" do
@model.expects(:convert_from).with("myformat", "mydata").returns "myobject"
response = mock 'response'
response.stubs(:[]).with("content-type").returns "myformat"
response.stubs(:body).returns "mydata"
response.stubs(:code).returns "200"
@searcher.deserialize(response).should == "myobject"
end
it "should convert and return multiple instances if the return code is in the 200s and 'multiple' is specified" do
@model.expects(:convert_from_multiple).with("myformat", "mydata").returns "myobjects"
response = mock 'response'
response.stubs(:[]).with("content-type").returns "myformat"
response.stubs(:body).returns "mydata"
response.stubs(:code).returns "200"
@searcher.deserialize(response, true).should == "myobjects"
end
end
describe "when creating an HTTP client" do
before do
Puppet.settings.stubs(:value).returns("rest_testing")
end
it "should use the class's server and port if the indirection request provides neither" do
@request = stub 'request', :key => "foo", :server => nil, :port => nil
@searcher.class.expects(:port).returns 321
@searcher.class.expects(:server).returns "myserver"
Puppet::Network::HttpPool.expects(:http_instance).with("myserver", 321).returns "myconn"
@searcher.network(@request).should == "myconn"
end
it "should use the server from the indirection request if one is present" do
@request = stub 'request', :key => "foo", :server => "myserver", :port => nil
@searcher.class.stubs(:port).returns 321
Puppet::Network::HttpPool.expects(:http_instance).with("myserver", 321).returns "myconn"
@searcher.network(@request).should == "myconn"
end
it "should use the port from the indirection request if one is present" do
@request = stub 'request', :key => "foo", :server => nil, :port => 321
@searcher.class.stubs(:server).returns "myserver"
Puppet::Network::HttpPool.expects(:http_instance).with("myserver", 321).returns "myconn"
@searcher.network(@request).should == "myconn"
end
end
describe "when building a query string from request options" do
it "should return an empty query string if there are no options" do
@request.stubs(:options).returns nil
@searcher.query_string(@request).should == ""
end
it "should return an empty query string if the options are empty" do
@request.stubs(:options).returns({})
@searcher.query_string(@request).should == ""
end
it "should prefix the query string with '?'" do
@request.stubs(:options).returns(:one => "two")
@searcher.query_string(@request).should =~ /^\?/
end
it "should include all options in the query string, separated by '&'" do
@request.stubs(:options).returns(:one => "two", :three => "four")
@searcher.query_string(@request).sub(/^\?/, '').split("&").sort.should == %w{one=two three=four}.sort
end
it "should ignore nil options" do
@request.stubs(:options).returns(:one => "two", :three => nil)
@searcher.query_string(@request).should_not be_include("three")
end
it "should convert 'true' option values into strings" do
@request.stubs(:options).returns(:one => true)
@searcher.query_string(@request).should == "?one=true"
end
it "should convert 'false' option values into strings" do
@request.stubs(:options).returns(:one => false)
@searcher.query_string(@request).should == "?one=false"
end
it "should URI-escape all option values that are strings" do
escaping = URI.escape("one two")
@request.stubs(:options).returns(:one => "one two")
@searcher.query_string(@request).should == "?one=#{escaping}"
end
it "should YAML-dump and URI-escape arrays" do
escaping = URI.escape(YAML.dump(%w{one two}))
@request.stubs(:options).returns(:one => %w{one two})
@searcher.query_string(@request).should == "?one=#{escaping}"
end
it "should convert to a string and URI-escape all option values that are symbols" do
escaping = URI.escape("sym bol")
@request.stubs(:options).returns(:one => :"sym bol")
@searcher.query_string(@request).should == "?one=#{escaping}"
end
it "should fail if options other than booleans or strings are provided" do
@request.stubs(:options).returns(:one => {:one => :two})
lambda { @searcher.query_string(@request) }.should raise_error(ArgumentError)
end
end
describe "when doing a find" do
before :each do
@connection = stub('mock http connection', :get => @response)
@searcher.stubs(:network).returns(@connection) # neuter the network connection
# Use a key with spaces, so we can test escaping
@request = stub 'request', :escaped_key => 'foo', :options => {}
end
it "should call the GET http method on a network connection" do
@searcher.expects(:network).returns @connection
@connection.expects(:get).returns @response
@searcher.find(@request)
end
it "should deserialize and return the http response" do
@connection.expects(:get).returns @response
@searcher.expects(:deserialize).with(@response).returns "myobject"
@searcher.find(@request).should == 'myobject'
end
it "should use the indirection name and escaped request key to create the path" do
should_path = "/%s/%s" % [@indirection.name.to_s, "foo"]
@connection.expects(:get).with { |path, args| path == should_path }.returns(@response)
@searcher.find(@request)
end
it "should provide an Accept header containing the list of supported formats joined with commas" do
@connection.expects(:get).with { |path, args| args["Accept"] == "supported, formats" }.returns(@response)
@searcher.model.expects(:supported_formats).returns %w{supported formats}
@searcher.find(@request)
end
it "should deserialize and return the network response" do
@searcher.expects(:deserialize).with(@response).returns @instance
@searcher.find(@request).should equal(@instance)
end
it "should generate an error when result data deserializes fails" do
@searcher.expects(:deserialize).raises(ArgumentError)
lambda { @searcher.find(@request) }.should raise_error(ArgumentError)
end
end
describe "when doing a search" do
before :each do
@connection = stub('mock http connection', :get => @response)
@searcher.stubs(:network).returns(@connection) # neuter the network connection
@model.stubs(:convert_from_multiple)
@request = stub 'request', :escaped_key => 'foo', :options => {}, :key => "bar"
end
it "should call the GET http method on a network connection" do
@searcher.expects(:network).returns @connection
@connection.expects(:get).returns @response
@searcher.search(@request)
end
it "should deserialize as multiple instances and return the http response" do
@connection.expects(:get).returns @response
@searcher.expects(:deserialize).with(@response, true).returns "myobject"
@searcher.search(@request).should == 'myobject'
end
it "should use the plural indirection name as the path if there is no request key" do
should_path = "/%ss" % [@indirection.name.to_s]
@request.stubs(:key).returns nil
@connection.expects(:get).with { |path, args| path == should_path }.returns(@response)
@searcher.search(@request)
end
it "should use the plural indirection name and escaped request key to create the path if the request key is set" do
should_path = "/%ss/%s" % [@indirection.name.to_s, "foo"]
@connection.expects(:get).with { |path, args| path == should_path }.returns(@response)
@searcher.search(@request)
end
it "should include all options in the query string" do
@request.stubs(:options).returns(:one => "two", :three => "four")
should_path = "/%s/%s" % [@indirection.name.to_s, "foo"]
@connection.expects(:get).with { |path, args| path =~ /\?one=two&three=four$/ or path =~ /\?three=four&one=two$/ }.returns(@response)
@searcher.search(@request)
end
it "should provide an Accept header containing the list of supported formats joined with commas" do
@connection.expects(:get).with { |path, args| args["Accept"] == "supported, formats" }.returns(@response)
@searcher.model.expects(:supported_formats).returns %w{supported formats}
@searcher.search(@request)
end
it "should return an empty array if serialization returns nil" do
@model.stubs(:convert_from_multiple).returns nil
@searcher.search(@request).should == []
end
it "should generate an error when result data deserializes fails" do
@searcher.expects(:deserialize).raises(ArgumentError)
lambda { @searcher.search(@request) }.should raise_error(ArgumentError)
end
end
describe "when doing a destroy" do
before :each do
@connection = stub('mock http connection', :delete => @response)
@searcher.stubs(:network).returns(@connection) # neuter the network connection
@request = stub 'request', :escaped_key => 'foo', :options => {}
end
it "should call the DELETE http method on a network connection" do
@searcher.expects(:network).returns @connection
@connection.expects(:delete).returns @response
@searcher.destroy(@request)
end
it "should fail if any options are provided, since DELETE apparently does not support query options" do
@request.stubs(:options).returns(:one => "two", :three => "four")
lambda { @searcher.destroy(@request) }.should raise_error(ArgumentError)
end
it "should deserialize and return the http response" do
@connection.expects(:delete).returns @response
@searcher.expects(:deserialize).with(@response).returns "myobject"
@searcher.destroy(@request).should == 'myobject'
end
it "should use the indirection name and escaped request key to create the path" do
should_path = "/%s/%s" % [@indirection.name.to_s, "foo"]
@connection.expects(:delete).with { |path, args| path == should_path }.returns(@response)
@searcher.destroy(@request)
end
it "should provide an Accept header containing the list of supported formats joined with commas" do
@connection.expects(:delete).with { |path, args| args["Accept"] == "supported, formats" }.returns(@response)
@searcher.model.expects(:supported_formats).returns %w{supported formats}
@searcher.destroy(@request)
end
it "should deserialize and return the network response" do
@searcher.expects(:deserialize).with(@response).returns @instance
@searcher.destroy(@request).should equal(@instance)
end
it "should generate an error when result data deserializes fails" do
@searcher.expects(:deserialize).raises(ArgumentError)
lambda { @searcher.destroy(@request) }.should raise_error(ArgumentError)
end
end
describe "when doing a save" do
before :each do
@connection = stub('mock http connection', :put => @response)
@searcher.stubs(:network).returns(@connection) # neuter the network connection
@instance = stub 'instance', :render => "mydata"
@request = stub 'request', :instance => @instance, :options => {}
end
it "should call the PUT http method on a network connection" do
@searcher.expects(:network).returns @connection
@connection.expects(:put).returns @response
@searcher.save(@request)
end
it "should fail if any options are provided, since DELETE apparently does not support query options" do
@request.stubs(:options).returns(:one => "two", :three => "four")
lambda { @searcher.save(@request) }.should raise_error(ArgumentError)
end
it "should use the indirection name as the path for the request" do
@connection.expects(:put).with { |path, data, args| path == "/#{@indirection.name.to_s}/" }.returns @response
@searcher.save(@request)
end
it "should serialize the instance using the default format and pass the result as the body of the request" do
@instance.expects(:render).returns "serial_instance"
@connection.expects(:put).with { |path, data, args| data == "serial_instance" }.returns @response
@searcher.save(@request)
end
it "should deserialize and return the http response" do
@connection.expects(:put).returns @response
@searcher.expects(:deserialize).with(@response).returns "myobject"
@searcher.save(@request).should == 'myobject'
end
it "should provide an Accept header containing the list of supported formats joined with commas" do
@connection.expects(:put).with { |path, data, args| args["Accept"] == "supported, formats" }.returns(@response)
@searcher.model.expects(:supported_formats).returns %w{supported formats}
@searcher.save(@request)
end
it "should deserialize and return the network response" do
@searcher.expects(:deserialize).with(@response).returns @instance
@searcher.save(@request).should equal(@instance)
end
it "should generate an error when result data deserializes fails" do
@searcher.expects(:deserialize).raises(ArgumentError)
lambda { @searcher.save(@request) }.should raise_error(ArgumentError)
end
end
end
|