cft_smartcloud 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGELOG +11 -0
  3. data/README.md +106 -0
  4. data/VERSION +1 -1
  5. data/bin/cft_smartcloud +33 -7
  6. data/bin/smartcloud +33 -7
  7. data/cft_smartcloud.gemspec +84 -20
  8. data/lib/config/config.yml +2 -0
  9. data/lib/curl_client.rb +42 -0
  10. data/lib/rest-client-1.6.6-master/.gitignore +6 -0
  11. data/lib/{rest-client-1.6.3 → rest-client-1.6.6-master}/README.rdoc +10 -1
  12. data/lib/{rest-client-1.6.3 → rest-client-1.6.6-master}/Rakefile +0 -0
  13. data/lib/rest-client-1.6.6-master/VERSION +1 -0
  14. data/lib/{rest-client-1.6.3 → rest-client-1.6.6-master}/bin/restclient +5 -4
  15. data/lib/{rest-client-1.6.3 → rest-client-1.6.6-master}/history.md +22 -0
  16. data/lib/{rest-client-1.6.3 → rest-client-1.6.6-master}/lib/rest-client.rb +0 -0
  17. data/lib/{rest-client-1.6.3 → rest-client-1.6.6-master}/lib/rest_client.rb +0 -0
  18. data/lib/{rest-client-1.6.3 → rest-client-1.6.6-master}/lib/restclient/abstract_response.rb +0 -0
  19. data/lib/{rest-client-1.6.3 → rest-client-1.6.6-master}/lib/restclient/exceptions.rb +0 -0
  20. data/lib/rest-client-1.6.6-master/lib/restclient/net_http_ext.rb +55 -0
  21. data/lib/{rest-client-1.6.3 → rest-client-1.6.6-master}/lib/restclient/payload.rb +17 -2
  22. data/lib/{rest-client-1.6.3 → rest-client-1.6.6-master}/lib/restclient/raw_response.rb +0 -0
  23. data/lib/{rest-client-1.6.3 → rest-client-1.6.6-master}/lib/restclient/request.rb +21 -19
  24. data/lib/{rest-client-1.6.3 → rest-client-1.6.6-master}/lib/restclient/resource.rb +0 -0
  25. data/lib/{rest-client-1.6.3 → rest-client-1.6.6-master}/lib/restclient/response.rb +0 -0
  26. data/lib/{rest-client-1.6.3 → rest-client-1.6.6-master}/lib/restclient.rb +0 -0
  27. data/lib/rest-client-1.6.6-master/rest-client.gemspec +76 -0
  28. data/lib/rest-client-1.6.6-master/spec/abstract_response_spec.rb +85 -0
  29. data/lib/rest-client-1.6.6-master/spec/base.rb +16 -0
  30. data/lib/rest-client-1.6.6-master/spec/exceptions_spec.rb +98 -0
  31. data/lib/rest-client-1.6.6-master/spec/integration/certs/equifax.crt +19 -0
  32. data/lib/rest-client-1.6.6-master/spec/integration/certs/verisign.crt +14 -0
  33. data/lib/rest-client-1.6.6-master/spec/integration/request_spec.rb +25 -0
  34. data/lib/rest-client-1.6.6-master/spec/integration_spec.rb +38 -0
  35. data/lib/rest-client-1.6.6-master/spec/master_shake.jpg +0 -0
  36. data/lib/rest-client-1.6.6-master/spec/payload_spec.rb +234 -0
  37. data/lib/rest-client-1.6.6-master/spec/raw_response_spec.rb +17 -0
  38. data/lib/rest-client-1.6.6-master/spec/request2_spec.rb +40 -0
  39. data/lib/rest-client-1.6.6-master/spec/request_spec.rb +536 -0
  40. data/lib/rest-client-1.6.6-master/spec/resource_spec.rb +134 -0
  41. data/lib/rest-client-1.6.6-master/spec/response_spec.rb +169 -0
  42. data/lib/rest-client-1.6.6-master/spec/restclient_spec.rb +73 -0
  43. data/lib/slop-2.3.1/.gemtest +0 -0
  44. data/lib/slop-2.3.1/.gitignore +6 -0
  45. data/lib/slop-2.3.1/.yardopts +6 -0
  46. data/lib/slop-2.3.1/CHANGES.md +137 -0
  47. data/lib/slop-2.3.1/LICENSE +20 -0
  48. data/lib/slop-2.3.1/README.md +293 -0
  49. data/lib/slop-2.3.1/Rakefile +6 -0
  50. data/lib/slop-2.3.1/lib/slop.rb +1022 -0
  51. data/lib/slop-2.3.1/slop.gemspec +11 -0
  52. data/lib/slop-2.3.1/test/commands_test.rb +151 -0
  53. data/lib/slop-2.3.1/test/helper.rb +13 -0
  54. data/lib/slop-2.3.1/test/option_test.rb +198 -0
  55. data/lib/slop-2.3.1/test/slop_test.rb +574 -0
  56. data/lib/smartcloud.rb +186 -116
  57. data/lib/terminal-table-1.4.4/History.rdoc +53 -0
  58. data/lib/terminal-table-1.4.4/Manifest +24 -0
  59. data/lib/terminal-table-1.4.4/README.rdoc +240 -0
  60. data/lib/terminal-table-1.4.4/Rakefile +15 -0
  61. data/lib/terminal-table-1.4.4/Todo.rdoc +14 -0
  62. data/lib/terminal-table-1.4.4/examples/examples.rb +80 -0
  63. data/lib/terminal-table-1.4.4/lib/terminal-table/cell.rb +88 -0
  64. data/lib/terminal-table-1.4.4/lib/terminal-table/core_ext.rb +8 -0
  65. data/lib/terminal-table-1.4.4/lib/terminal-table/import.rb +4 -0
  66. data/lib/terminal-table-1.4.4/lib/terminal-table/row.rb +48 -0
  67. data/lib/terminal-table-1.4.4/lib/terminal-table/separator.rb +14 -0
  68. data/lib/terminal-table-1.4.4/lib/terminal-table/style.rb +61 -0
  69. data/lib/terminal-table-1.4.4/lib/terminal-table/table.rb +217 -0
  70. data/lib/terminal-table-1.4.4/lib/terminal-table/table_helper.rb +9 -0
  71. data/lib/terminal-table-1.4.4/lib/terminal-table/version.rb +6 -0
  72. data/lib/terminal-table-1.4.4/lib/terminal-table.rb +27 -0
  73. data/lib/terminal-table-1.4.4/spec/cell_spec.rb +54 -0
  74. data/lib/terminal-table-1.4.4/spec/core_ext_spec.rb +18 -0
  75. data/lib/terminal-table-1.4.4/spec/import_spec.rb +11 -0
  76. data/lib/terminal-table-1.4.4/spec/spec.opts +1 -0
  77. data/lib/terminal-table-1.4.4/spec/spec_helper.rb +8 -0
  78. data/lib/terminal-table-1.4.4/spec/table_spec.rb +525 -0
  79. data/lib/terminal-table-1.4.4/tasks/docs.rake +13 -0
  80. data/lib/terminal-table-1.4.4/tasks/gemspec.rake +3 -0
  81. data/lib/terminal-table-1.4.4/tasks/spec.rake +25 -0
  82. data/lib/terminal-table-1.4.4/terminal-table.gemspec +30 -0
  83. data/responses/addresses +26 -0
  84. data/responses/addresses.blank +2 -0
  85. data/responses/instances +74 -0
  86. data/responses/keys +33 -0
  87. data/responses/locations +142 -0
  88. data/responses/offerings_image +3780 -0
  89. data/responses/storage +379 -0
  90. metadata +86 -22
  91. data/README.rdoc +0 -75
  92. data/lib/rest-client-1.6.3/VERSION +0 -1
  93. data/lib/rest-client-1.6.3/lib/restclient/net_http_ext.rb +0 -21
@@ -0,0 +1,536 @@
1
+ require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
2
+
3
+ require 'webmock/rspec'
4
+ include WebMock
5
+
6
+ describe RestClient::Request do
7
+ before do
8
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
9
+
10
+ @uri = mock("uri")
11
+ @uri.stub!(:request_uri).and_return('/resource')
12
+ @uri.stub!(:host).and_return('some')
13
+ @uri.stub!(:port).and_return(80)
14
+
15
+ @net = mock("net::http base")
16
+ @http = mock("net::http connection")
17
+ Net::HTTP.stub!(:new).and_return(@net)
18
+ @net.stub!(:start).and_yield(@http)
19
+ @net.stub!(:use_ssl=)
20
+ @net.stub!(:verify_mode=)
21
+ RestClient.log = nil
22
+ end
23
+
24
+ it "accept */* mimetype, preferring xml" do
25
+ @request.default_headers[:accept].should == '*/*; q=0.5, application/xml'
26
+ end
27
+
28
+ describe "compression" do
29
+
30
+ it "decodes an uncompressed result body by passing it straight through" do
31
+ RestClient::Request.decode(nil, 'xyz').should == 'xyz'
32
+ end
33
+
34
+ it "doesn't fail for nil bodies" do
35
+ RestClient::Request.decode('gzip', nil).should be_nil
36
+ end
37
+
38
+
39
+ it "decodes a gzip body" do
40
+ RestClient::Request.decode('gzip', "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000").should == "i'm gziped\n"
41
+ end
42
+
43
+ it "ingores gzip for empty bodies" do
44
+ RestClient::Request.decode('gzip', '').should be_empty
45
+ end
46
+
47
+ it "decodes a deflated body" do
48
+ RestClient::Request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should == "some deflated text"
49
+ end
50
+ end
51
+
52
+ it "processes a successful result" do
53
+ res = mock("result")
54
+ res.stub!(:code).and_return("200")
55
+ res.stub!(:body).and_return('body')
56
+ res.stub!(:[]).with('content-encoding').and_return(nil)
57
+ @request.process_result(res).body.should == 'body'
58
+ @request.process_result(res).to_s.should == 'body'
59
+ end
60
+
61
+ it "doesn't classify successful requests as failed" do
62
+ 203.upto(207) do |code|
63
+ res = mock("result")
64
+ res.stub!(:code).and_return(code.to_s)
65
+ res.stub!(:body).and_return("")
66
+ res.stub!(:[]).with('content-encoding').and_return(nil)
67
+ @request.process_result(res).should be_empty
68
+ end
69
+ end
70
+
71
+ it "parses a url into a URI object" do
72
+ URI.should_receive(:parse).with('http://example.com/resource')
73
+ @request.parse_url('http://example.com/resource')
74
+ end
75
+
76
+ it "adds http:// to the front of resources specified in the syntax example.com/resource" do
77
+ URI.should_receive(:parse).with('http://example.com/resource')
78
+ @request.parse_url('example.com/resource')
79
+ end
80
+
81
+ describe "user - password" do
82
+ it "extracts the username and password when parsing http://user:password@example.com/" do
83
+ URI.stub!(:parse).and_return(mock('uri', :user => 'joe', :password => 'pass1'))
84
+ @request.parse_url_with_auth('http://joe:pass1@example.com/resource')
85
+ @request.user.should == 'joe'
86
+ @request.password.should == 'pass1'
87
+ end
88
+
89
+ it "extracts with escaping the username and password when parsing http://user:password@example.com/" do
90
+ URI.stub!(:parse).and_return(mock('uri', :user => 'joe%20', :password => 'pass1'))
91
+ @request.parse_url_with_auth('http://joe%20:pass1@example.com/resource')
92
+ @request.user.should == 'joe '
93
+ @request.password.should == 'pass1'
94
+ end
95
+
96
+ it "doesn't overwrite user and password (which may have already been set by the Resource constructor) if there is no user/password in the url" do
97
+ URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
98
+ @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2')
99
+ @request.parse_url_with_auth('http://example.com/resource')
100
+ @request.user.should == 'beth'
101
+ @request.password.should == 'pass2'
102
+ end
103
+ end
104
+
105
+ it "correctly formats cookies provided to the constructor" do
106
+ URI.stub!(:parse).and_return(mock('uri', :user => nil, :password => nil))
107
+ @request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1', :user_id => "someone" })
108
+ @request.should_receive(:default_headers).and_return({'Foo' => 'bar'})
109
+ @request.make_headers({}).should == { 'Foo' => 'bar', 'Cookie' => 'session_id=1; user_id=someone'}
110
+ end
111
+
112
+ it "determines the Net::HTTP class to instantiate by the method name" do
113
+ @request.net_http_request_class(:put).should == Net::HTTP::Put
114
+ end
115
+
116
+ describe "user headers" do
117
+ it "merges user headers with the default headers" do
118
+ @request.should_receive(:default_headers).and_return({ :accept => '*/*; q=0.5, application/xml', :accept_encoding => 'gzip, deflate' })
119
+ headers = @request.make_headers("Accept" => "application/json", :accept_encoding => 'gzip')
120
+ headers.should have_key "Accept-Encoding"
121
+ headers["Accept-Encoding"].should == "gzip"
122
+ headers.should have_key "Accept"
123
+ headers["Accept"].should == "application/json"
124
+ end
125
+
126
+ it "prefers the user header when the same header exists in the defaults" do
127
+ @request.should_receive(:default_headers).and_return({ '1' => '2' })
128
+ headers = @request.make_headers('1' => '3')
129
+ headers.should have_key('1')
130
+ headers['1'].should == '3'
131
+ end
132
+
133
+ it "converts user headers to string before calling CGI::unescape which fails on non string values" do
134
+ @request.should_receive(:default_headers).and_return({ '1' => '2' })
135
+ headers = @request.make_headers('1' => 3)
136
+ headers.should have_key('1')
137
+ headers['1'].should == '3'
138
+ end
139
+ end
140
+
141
+ describe "header symbols" do
142
+
143
+ it "converts header symbols from :content_type to 'Content-Type'" do
144
+ @request.should_receive(:default_headers).and_return({})
145
+ headers = @request.make_headers(:content_type => 'abc')
146
+ headers.should have_key('Content-Type')
147
+ headers['Content-Type'].should == 'abc'
148
+ end
149
+
150
+ it "converts content-type from extension to real content-type" do
151
+ @request.should_receive(:default_headers).and_return({})
152
+ headers = @request.make_headers(:content_type => 'json')
153
+ headers.should have_key('Content-Type')
154
+ headers['Content-Type'].should == 'application/json'
155
+ end
156
+
157
+ it "converts accept from extension(s) to real content-type(s)" do
158
+ @request.should_receive(:default_headers).and_return({})
159
+ headers = @request.make_headers(:accept => 'json, mp3')
160
+ headers.should have_key('Accept')
161
+ headers['Accept'].should == 'application/json, audio/mpeg'
162
+
163
+ @request.should_receive(:default_headers).and_return({})
164
+ headers = @request.make_headers(:accept => :json)
165
+ headers.should have_key('Accept')
166
+ headers['Accept'].should == 'application/json'
167
+ end
168
+
169
+ it "only convert symbols in header" do
170
+ @request.should_receive(:default_headers).and_return({})
171
+ headers = @request.make_headers({:foo_bar => 'value', "bar_bar" => 'value'})
172
+ headers['Foo-Bar'].should == 'value'
173
+ headers['bar_bar'].should == 'value'
174
+ end
175
+
176
+ it "converts header values to strings" do
177
+ @request.make_headers('A' => 1)['A'].should == '1'
178
+ end
179
+ end
180
+
181
+ it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
182
+ @request.should_receive(:parse_url_with_auth).with('http://some/resource').and_return(@uri)
183
+ klass = mock("net:http class")
184
+ @request.should_receive(:net_http_request_class).with(:put).and_return(klass)
185
+ klass.should_receive(:new).and_return('result')
186
+ @request.should_receive(:transmit).with(@uri, 'result', kind_of(RestClient::Payload::Base))
187
+ @request.execute
188
+ end
189
+
190
+ it "transmits the request with Net::HTTP" do
191
+ @http.should_receive(:request).with('req', 'payload')
192
+ @request.should_receive(:process_result)
193
+ @request.transmit(@uri, 'req', 'payload')
194
+ end
195
+
196
+ describe "payload" do
197
+ it "sends nil payloads" do
198
+ @http.should_receive(:request).with('req', nil)
199
+ @request.should_receive(:process_result)
200
+ @request.stub!(:response_log)
201
+ @request.transmit(@uri, 'req', nil)
202
+ end
203
+
204
+ it "passes non-hash payloads straight through" do
205
+ @request.process_payload("x").should == "x"
206
+ end
207
+
208
+ it "converts a hash payload to urlencoded data" do
209
+ @request.process_payload(:a => 'b c+d').should == "a=b%20c%2Bd"
210
+ end
211
+
212
+ it "accepts nested hashes in payload" do
213
+ payload = @request.process_payload(:user => { :name => 'joe', :location => { :country => 'USA', :state => 'CA' }})
214
+ payload.should include('user[name]=joe')
215
+ payload.should include('user[location][country]=USA')
216
+ payload.should include('user[location][state]=CA')
217
+ end
218
+ end
219
+
220
+ it "set urlencoded content_type header on hash payloads" do
221
+ @request.process_payload(:a => 1)
222
+ @request.headers[:content_type].should == 'application/x-www-form-urlencoded'
223
+ end
224
+
225
+ describe "credentials" do
226
+ it "sets up the credentials prior to the request" do
227
+ @http.stub!(:request)
228
+ @request.stub!(:process_result)
229
+ @request.stub!(:response_log)
230
+
231
+ @request.stub!(:user).and_return('joe')
232
+ @request.stub!(:password).and_return('mypass')
233
+ @request.should_receive(:setup_credentials).with('req')
234
+
235
+ @request.transmit(@uri, 'req', nil)
236
+ end
237
+
238
+ it "does not attempt to send any credentials if user is nil" do
239
+ @request.stub!(:user).and_return(nil)
240
+ req = mock("request")
241
+ req.should_not_receive(:basic_auth)
242
+ @request.setup_credentials(req)
243
+ end
244
+
245
+ it "setup credentials when there's a user" do
246
+ @request.stub!(:user).and_return('joe')
247
+ @request.stub!(:password).and_return('mypass')
248
+ req = mock("request")
249
+ req.should_receive(:basic_auth).with('joe', 'mypass')
250
+ @request.setup_credentials(req)
251
+ end
252
+ end
253
+
254
+ it "catches EOFError and shows the more informative ServerBrokeConnection" do
255
+ @http.stub!(:request).and_raise(EOFError)
256
+ lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::ServerBrokeConnection)
257
+ end
258
+
259
+ it "class method execute wraps constructor" do
260
+ req = mock("rest request")
261
+ RestClient::Request.should_receive(:new).with(1 => 2).and_return(req)
262
+ req.should_receive(:execute)
263
+ RestClient::Request.execute(1 => 2)
264
+ end
265
+
266
+ describe "exception" do
267
+ it "raises Unauthorized when the response is 401" do
268
+ res = mock('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' )
269
+ lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
270
+ end
271
+
272
+ it "raises ResourceNotFound when the response is 404" do
273
+ res = mock('response', :code => '404', :[] => ['content-encoding' => ''], :body => '' )
274
+ lambda { @request.process_result(res) }.should raise_error(RestClient::ResourceNotFound)
275
+ end
276
+
277
+ it "raises RequestFailed otherwise" do
278
+ res = mock('response', :code => '500', :[] => ['content-encoding' => ''], :body => '' )
279
+ lambda { @request.process_result(res) }.should raise_error(RestClient::InternalServerError)
280
+ end
281
+ end
282
+
283
+ describe "block usage" do
284
+ it "returns what asked to" do
285
+ res = mock('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' )
286
+ @request.process_result(res){|response, request| "foo"}.should == "foo"
287
+ end
288
+ end
289
+
290
+ describe "proxy" do
291
+ it "creates a proxy class if a proxy url is given" do
292
+ RestClient.stub!(:proxy).and_return("http://example.com/")
293
+ @request.net_http_class.should include(Net::HTTP::ProxyDelta)
294
+ end
295
+
296
+ it "creates a non-proxy class if a proxy url is not given" do
297
+ @request.net_http_class.should_not include(Net::HTTP::ProxyDelta)
298
+ end
299
+ end
300
+
301
+
302
+ describe "logging" do
303
+ it "logs a get request" do
304
+ log = RestClient.log = []
305
+ RestClient::Request.new(:method => :get, :url => 'http://url').log_request
306
+ log[0].should == %Q{RestClient.get "http://url", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate"\n}
307
+ end
308
+
309
+ it "logs a post request with a small payload" do
310
+ log = RestClient.log = []
311
+ RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').log_request
312
+ log[0].should == %Q{RestClient.post "http://url", "foo", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"3"\n}
313
+ end
314
+
315
+ it "logs a post request with a large payload" do
316
+ log = RestClient.log = []
317
+ RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).log_request
318
+ log[0].should == %Q{RestClient.post "http://url", 1000 byte(s) length, "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"1000"\n}
319
+ end
320
+
321
+ it "logs input headers as a hash" do
322
+ log = RestClient.log = []
323
+ RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).log_request
324
+ log[0].should == %Q{RestClient.get "http://url", "Accept"=>"text/plain", "Accept-Encoding"=>"gzip, deflate"\n}
325
+ end
326
+
327
+ it "logs a response including the status code, content type, and result body size in bytes" do
328
+ log = RestClient.log = []
329
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
330
+ res.stub!(:[]).with('Content-type').and_return('text/html')
331
+ @request.log_response res
332
+ log[0].should == "# => 200 OK | text/html 4 bytes\n"
333
+ end
334
+
335
+ it "logs a response with a nil Content-type" do
336
+ log = RestClient.log = []
337
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
338
+ res.stub!(:[]).with('Content-type').and_return(nil)
339
+ @request.log_response res
340
+ log[0].should == "# => 200 OK | 4 bytes\n"
341
+ end
342
+
343
+ it "logs a response with a nil body" do
344
+ log = RestClient.log = []
345
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => nil)
346
+ res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
347
+ @request.log_response res
348
+ log[0].should == "# => 200 OK | text/html 0 bytes\n"
349
+ end
350
+ end
351
+
352
+ it "strips the charset from the response content type" do
353
+ log = RestClient.log = []
354
+ res = mock('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
355
+ res.stub!(:[]).with('Content-type').and_return('text/html; charset=utf-8')
356
+ @request.log_response res
357
+ log[0].should == "# => 200 OK | text/html 4 bytes\n"
358
+ end
359
+
360
+ describe "timeout" do
361
+ it "set read_timeout" do
362
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123)
363
+ @http.stub!(:request)
364
+ @request.stub!(:process_result)
365
+ @request.stub!(:response_log)
366
+
367
+ @net.should_receive(:read_timeout=).with(123)
368
+
369
+ @request.transmit(@uri, 'req', nil)
370
+ end
371
+
372
+ it "set open_timeout" do
373
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :open_timeout => 123)
374
+ @http.stub!(:request)
375
+ @request.stub!(:process_result)
376
+ @request.stub!(:response_log)
377
+
378
+ @net.should_receive(:open_timeout=).with(123)
379
+
380
+ @request.transmit(@uri, 'req', nil)
381
+ end
382
+ end
383
+
384
+ describe "ssl" do
385
+ it "uses SSL when the URI refers to a https address" do
386
+ @uri.stub!(:is_a?).with(URI::HTTPS).and_return(true)
387
+ @net.should_receive(:use_ssl=).with(true)
388
+ @http.stub!(:request)
389
+ @request.stub!(:process_result)
390
+ @request.stub!(:response_log)
391
+ @request.transmit(@uri, 'req', 'payload')
392
+ end
393
+
394
+ it "should default to not verifying ssl certificates" do
395
+ @request.verify_ssl.should == false
396
+ end
397
+
398
+ it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do
399
+ @net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
400
+ @http.stub!(:request)
401
+ @request.stub!(:process_result)
402
+ @request.stub!(:response_log)
403
+ @request.transmit(@uri, 'req', 'payload')
404
+ end
405
+
406
+ it "should not set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is true" do
407
+ @request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true)
408
+ @net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
409
+ @http.stub!(:request)
410
+ @request.stub!(:process_result)
411
+ @request.stub!(:response_log)
412
+ @request.transmit(@uri, 'req', 'payload')
413
+ end
414
+
415
+ it "should set net.verify_mode to the passed value if verify_ssl is an OpenSSL constant" do
416
+ mode = OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
417
+ @request = RestClient::Request.new( :method => :put,
418
+ :url => 'https://some/resource',
419
+ :payload => 'payload',
420
+ :verify_ssl => mode )
421
+ @net.should_receive(:verify_mode=).with(mode)
422
+ @net.should_receive(:verify_callback=)
423
+ @http.stub!(:request)
424
+ @request.stub!(:process_result)
425
+ @request.stub!(:response_log)
426
+ @request.transmit(@uri, 'req', 'payload')
427
+ end
428
+
429
+ it "should default to not having an ssl_client_cert" do
430
+ @request.ssl_client_cert.should be(nil)
431
+ end
432
+
433
+ it "should set the ssl_client_cert if provided" do
434
+ @request = RestClient::Request.new(
435
+ :method => :put,
436
+ :url => 'https://some/resource',
437
+ :payload => 'payload',
438
+ :ssl_client_cert => "whatsupdoc!"
439
+ )
440
+ @net.should_receive(:cert=).with("whatsupdoc!")
441
+ @http.stub!(:request)
442
+ @request.stub!(:process_result)
443
+ @request.stub!(:response_log)
444
+ @request.transmit(@uri, 'req', 'payload')
445
+ end
446
+
447
+ it "should not set the ssl_client_cert if it is not provided" do
448
+ @request = RestClient::Request.new(
449
+ :method => :put,
450
+ :url => 'https://some/resource',
451
+ :payload => 'payload'
452
+ )
453
+ @net.should_not_receive(:cert=).with("whatsupdoc!")
454
+ @http.stub!(:request)
455
+ @request.stub!(:process_result)
456
+ @request.stub!(:response_log)
457
+ @request.transmit(@uri, 'req', 'payload')
458
+ end
459
+
460
+ it "should default to not having an ssl_client_key" do
461
+ @request.ssl_client_key.should be(nil)
462
+ end
463
+
464
+ it "should set the ssl_client_key if provided" do
465
+ @request = RestClient::Request.new(
466
+ :method => :put,
467
+ :url => 'https://some/resource',
468
+ :payload => 'payload',
469
+ :ssl_client_key => "whatsupdoc!"
470
+ )
471
+ @net.should_receive(:key=).with("whatsupdoc!")
472
+ @http.stub!(:request)
473
+ @request.stub!(:process_result)
474
+ @request.stub!(:response_log)
475
+ @request.transmit(@uri, 'req', 'payload')
476
+ end
477
+
478
+ it "should not set the ssl_client_key if it is not provided" do
479
+ @request = RestClient::Request.new(
480
+ :method => :put,
481
+ :url => 'https://some/resource',
482
+ :payload => 'payload'
483
+ )
484
+ @net.should_not_receive(:key=).with("whatsupdoc!")
485
+ @http.stub!(:request)
486
+ @request.stub!(:process_result)
487
+ @request.stub!(:response_log)
488
+ @request.transmit(@uri, 'req', 'payload')
489
+ end
490
+
491
+ it "should default to not having an ssl_ca_file" do
492
+ @request.ssl_ca_file.should be(nil)
493
+ end
494
+
495
+ it "should set the ssl_ca_file if provided" do
496
+ @request = RestClient::Request.new(
497
+ :method => :put,
498
+ :url => 'https://some/resource',
499
+ :payload => 'payload',
500
+ :ssl_ca_file => "Certificate Authority File"
501
+ )
502
+ @net.should_receive(:ca_file=).with("Certificate Authority File")
503
+ @http.stub!(:request)
504
+ @request.stub!(:process_result)
505
+ @request.stub!(:response_log)
506
+ @request.transmit(@uri, 'req', 'payload')
507
+ end
508
+
509
+ it "should not set the ssl_ca_file if it is not provided" do
510
+ @request = RestClient::Request.new(
511
+ :method => :put,
512
+ :url => 'https://some/resource',
513
+ :payload => 'payload'
514
+ )
515
+ @net.should_not_receive(:ca_file=).with("Certificate Authority File")
516
+ @http.stub!(:request)
517
+ @request.stub!(:process_result)
518
+ @request.stub!(:response_log)
519
+ @request.transmit(@uri, 'req', 'payload')
520
+ end
521
+ end
522
+
523
+ it "should still return a response object for 204 No Content responses" do
524
+ @request = RestClient::Request.new(
525
+ :method => :put,
526
+ :url => 'https://some/resource',
527
+ :payload => 'payload'
528
+ )
529
+ net_http_res = Net::HTTPNoContent.new("", "204", "No Content")
530
+ net_http_res.stub!(:read_body).and_return(nil)
531
+ @http.should_receive(:request).and_return(@request.fetch_body(net_http_res))
532
+ response = @request.transmit(@uri, 'req', 'payload')
533
+ response.should_not be_nil
534
+ response.code.should == 204
535
+ end
536
+ end
@@ -0,0 +1,134 @@
1
+ require File.join( File.dirname(File.expand_path(__FILE__)), 'base')
2
+
3
+ require 'webmock/rspec'
4
+ include WebMock
5
+
6
+ describe RestClient::Resource do
7
+ before do
8
+ @resource = RestClient::Resource.new('http://some/resource', :user => 'jane', :password => 'mypass', :headers => {'X-Something' => '1'})
9
+ end
10
+
11
+ context "Resource delegation" do
12
+ it "GET" do
13
+ RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
14
+ @resource.get
15
+ end
16
+
17
+ it "HEAD" do
18
+ RestClient::Request.should_receive(:execute).with(:method => :head, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
19
+ @resource.head
20
+ end
21
+
22
+ it "POST" do
23
+ RestClient::Request.should_receive(:execute).with(:method => :post, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
24
+ @resource.post 'abc', :content_type => 'image/jpg'
25
+ end
26
+
27
+ it "PUT" do
28
+ RestClient::Request.should_receive(:execute).with(:method => :put, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
29
+ @resource.put 'abc', :content_type => 'image/jpg'
30
+ end
31
+
32
+ it "PATCH" do
33
+ RestClient::Request.should_receive(:execute).with(:method => :patch, :url => 'http://some/resource', :payload => 'abc', :headers => {:content_type => 'image/jpg', 'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
34
+ @resource.patch 'abc', :content_type => 'image/jpg'
35
+ end
36
+
37
+ it "DELETE" do
38
+ RestClient::Request.should_receive(:execute).with(:method => :delete, :url => 'http://some/resource', :headers => {'X-Something' => '1'}, :user => 'jane', :password => 'mypass')
39
+ @resource.delete
40
+ end
41
+
42
+ it "overrides resource headers" do
43
+ RestClient::Request.should_receive(:execute).with(:method => :get, :url => 'http://some/resource', :headers => {'X-Something' => '2'}, :user => 'jane', :password => 'mypass')
44
+ @resource.get 'X-Something' => '2'
45
+ end
46
+ end
47
+
48
+ it "can instantiate with no user/password" do
49
+ @resource = RestClient::Resource.new('http://some/resource')
50
+ end
51
+
52
+ it "is backwards compatible with previous constructor" do
53
+ @resource = RestClient::Resource.new('http://some/resource', 'user', 'pass')
54
+ @resource.user.should == 'user'
55
+ @resource.password.should == 'pass'
56
+ end
57
+
58
+ it "concatenates urls, inserting a slash when it needs one" do
59
+ @resource.concat_urls('http://example.com', 'resource').should == 'http://example.com/resource'
60
+ end
61
+
62
+ it "concatenates urls, using no slash if the first url ends with a slash" do
63
+ @resource.concat_urls('http://example.com/', 'resource').should == 'http://example.com/resource'
64
+ end
65
+
66
+ it "concatenates urls, using no slash if the second url starts with a slash" do
67
+ @resource.concat_urls('http://example.com', '/resource').should == 'http://example.com/resource'
68
+ end
69
+
70
+ it "concatenates even non-string urls, :posts + 1 => 'posts/1'" do
71
+ @resource.concat_urls(:posts, 1).should == 'posts/1'
72
+ end
73
+
74
+ it "offers subresources via []" do
75
+ parent = RestClient::Resource.new('http://example.com')
76
+ parent['posts'].url.should == 'http://example.com/posts'
77
+ end
78
+
79
+ it "transports options to subresources" do
80
+ parent = RestClient::Resource.new('http://example.com', :user => 'user', :password => 'password')
81
+ parent['posts'].user.should == 'user'
82
+ parent['posts'].password.should == 'password'
83
+ end
84
+
85
+ it "passes a given block to subresources" do
86
+ block = Proc.new{|r| r}
87
+ parent = RestClient::Resource.new('http://example.com', &block)
88
+ parent['posts'].block.should == block
89
+ end
90
+
91
+ it "the block should be overrideable" do
92
+ block1 = Proc.new{|r| r}
93
+ block2 = Proc.new{|r| r}
94
+ parent = RestClient::Resource.new('http://example.com', &block1)
95
+ # parent['posts', &block2].block.should == block2 # ruby 1.9 syntax
96
+ parent.send(:[], 'posts', &block2).block.should == block2
97
+ end
98
+
99
+ it "the block should be overrideable in ruby 1.9 syntax" do
100
+ block = Proc.new{|r| r}
101
+ parent = RestClient::Resource.new('http://example.com', &block)
102
+ r19_syntax = %q{
103
+ parent['posts', &->(r){r}].block.should_not == block
104
+ }
105
+ if is_ruby_19?
106
+ eval(r19_syntax)
107
+ end
108
+ end
109
+
110
+ it "prints its url with to_s" do
111
+ RestClient::Resource.new('x').to_s.should == 'x'
112
+ end
113
+
114
+ describe 'block' do
115
+ it 'can use block when creating the resource' do
116
+ stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
117
+ resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
118
+ resource.get.should == 'foo'
119
+ end
120
+
121
+ it 'can use block when executing the resource' do
122
+ stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
123
+ resource = RestClient::Resource.new('www.example.com')
124
+ resource.get { |response, request| 'foo' }.should == 'foo'
125
+ end
126
+
127
+ it 'execution block override resource block' do
128
+ stub_request(:get, 'www.example.com').to_return(:body => '', :status => 404)
129
+ resource = RestClient::Resource.new('www.example.com') { |response, request| 'foo' }
130
+ resource.get { |response, request| 'bar' }.should == 'bar'
131
+ end
132
+
133
+ end
134
+ end