rest-client-maestro 1.7.2.maestro
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +8 -0
- data/.rspec +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +8 -0
- data/AUTHORS +69 -0
- data/Gemfile +7 -0
- data/README.rdoc +322 -0
- data/Rakefile +49 -0
- data/bin/restclient +93 -0
- data/history.md +134 -0
- data/lib/rest-client.rb +2 -0
- data/lib/rest_client.rb +2 -0
- data/lib/restclient/abstract_response.rb +106 -0
- data/lib/restclient/exceptions.rb +198 -0
- data/lib/restclient/net_http_ext.rb +55 -0
- data/lib/restclient/payload.rb +242 -0
- data/lib/restclient/raw_response.rb +34 -0
- data/lib/restclient/request.rb +346 -0
- data/lib/restclient/resource.rb +169 -0
- data/lib/restclient/response.rb +26 -0
- data/lib/restclient.rb +174 -0
- data/rest-client-maestro.gemspec +23 -0
- data/spec/integration/capath_equifax/578d5c04.0 +19 -0
- data/spec/integration/capath_equifax/594f1775.0 +19 -0
- data/spec/integration/capath_equifax/README +8 -0
- data/spec/integration/capath_equifax/equifax.crt +19 -0
- data/spec/integration/capath_verisign/415660c1.0 +14 -0
- data/spec/integration/capath_verisign/7651b327.0 +14 -0
- data/spec/integration/capath_verisign/README +8 -0
- data/spec/integration/capath_verisign/verisign.crt +14 -0
- data/spec/integration/certs/equifax.crt +19 -0
- data/spec/integration/certs/verisign.crt +14 -0
- data/spec/integration/integration_spec.rb +35 -0
- data/spec/integration/request_spec.rb +63 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/unit/abstract_response_spec.rb +85 -0
- data/spec/unit/exceptions_spec.rb +95 -0
- data/spec/unit/master_shake.jpg +0 -0
- data/spec/unit/payload_spec.rb +245 -0
- data/spec/unit/raw_response_spec.rb +17 -0
- data/spec/unit/request2_spec.rb +32 -0
- data/spec/unit/request_spec.rb +621 -0
- data/spec/unit/resource_spec.rb +133 -0
- data/spec/unit/response_spec.rb +166 -0
- data/spec/unit/restclient_spec.rb +73 -0
- metadata +220 -0
@@ -0,0 +1,621 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RestClient::Request do
|
4
|
+
before do
|
5
|
+
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
|
6
|
+
|
7
|
+
@uri = double("uri")
|
8
|
+
@uri.stub(:request_uri).and_return('/resource')
|
9
|
+
@uri.stub(:host).and_return('some')
|
10
|
+
@uri.stub(:port).and_return(80)
|
11
|
+
|
12
|
+
@net = double("net::http base")
|
13
|
+
@http = double("net::http connection")
|
14
|
+
Net::HTTP.stub(:new).and_return(@net)
|
15
|
+
@net.stub(:start).and_yield(@http)
|
16
|
+
@net.stub(:use_ssl=)
|
17
|
+
@net.stub(:verify_mode=)
|
18
|
+
RestClient.log = nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def net_http_post_request
|
22
|
+
Net::HTTP::Post.new('http://example.com/')
|
23
|
+
end
|
24
|
+
|
25
|
+
def payload
|
26
|
+
RestClient::Payload.generate('payload')
|
27
|
+
end
|
28
|
+
|
29
|
+
it "accept */* mimetype, preferring xml" do
|
30
|
+
@request.default_headers[:accept].should eq '*/*; q=0.5, application/xml'
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "compression" do
|
34
|
+
|
35
|
+
it "decodes an uncompressed result body by passing it straight through" do
|
36
|
+
RestClient::Request.decode(nil, 'xyz').should eq 'xyz'
|
37
|
+
end
|
38
|
+
|
39
|
+
it "doesn't fail for nil bodies" do
|
40
|
+
RestClient::Request.decode('gzip', nil).should be_nil
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
it "decodes a gzip body" do
|
45
|
+
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 eq "i'm gziped\n"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "ingores gzip for empty bodies" do
|
49
|
+
RestClient::Request.decode('gzip', '').should be_empty
|
50
|
+
end
|
51
|
+
|
52
|
+
it "decodes a deflated body" do
|
53
|
+
RestClient::Request.decode('deflate', "x\234+\316\317MUHIM\313I,IMQ(I\255(\001\000A\223\006\363").should eq "some deflated text"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it "processes a successful result" do
|
58
|
+
res = double("result")
|
59
|
+
res.stub(:code).and_return("200")
|
60
|
+
res.stub(:body).and_return('body')
|
61
|
+
res.stub(:[]).with('content-encoding').and_return(nil)
|
62
|
+
@request.process_result(res).body.should eq 'body'
|
63
|
+
@request.process_result(res).to_s.should eq 'body'
|
64
|
+
end
|
65
|
+
|
66
|
+
it "doesn't classify successful requests as failed" do
|
67
|
+
203.upto(207) do |code|
|
68
|
+
res = double("result")
|
69
|
+
res.stub(:code).and_return(code.to_s)
|
70
|
+
res.stub(:body).and_return("")
|
71
|
+
res.stub(:[]).with('content-encoding').and_return(nil)
|
72
|
+
@request.process_result(res).should be_empty
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
it "parses a url into a URI object" do
|
77
|
+
URI.should_receive(:parse).with('http://example.com/resource')
|
78
|
+
@request.parse_url('http://example.com/resource')
|
79
|
+
end
|
80
|
+
|
81
|
+
it "adds http:// to the front of resources specified in the syntax example.com/resource" do
|
82
|
+
URI.should_receive(:parse).with('http://example.com/resource')
|
83
|
+
@request.parse_url('example.com/resource')
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "user - password" do
|
87
|
+
it "extracts the username and password when parsing http://user:password@example.com/" do
|
88
|
+
URI.stub(:parse).and_return(double('uri', :user => 'joe', :password => 'pass1'))
|
89
|
+
@request.parse_url_with_auth('http://joe:pass1@example.com/resource')
|
90
|
+
@request.user.should eq 'joe'
|
91
|
+
@request.password.should eq 'pass1'
|
92
|
+
end
|
93
|
+
|
94
|
+
it "extracts with escaping the username and password when parsing http://user:password@example.com/" do
|
95
|
+
URI.stub(:parse).and_return(double('uri', :user => 'joe%20', :password => 'pass1'))
|
96
|
+
@request.parse_url_with_auth('http://joe%20:pass1@example.com/resource')
|
97
|
+
@request.user.should eq 'joe '
|
98
|
+
@request.password.should eq 'pass1'
|
99
|
+
end
|
100
|
+
|
101
|
+
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
|
102
|
+
URI.stub(:parse).and_return(double('uri', :user => nil, :password => nil))
|
103
|
+
@request = RestClient::Request.new(:method => 'get', :url => 'example.com', :user => 'beth', :password => 'pass2')
|
104
|
+
@request.parse_url_with_auth('http://example.com/resource')
|
105
|
+
@request.user.should eq 'beth'
|
106
|
+
@request.password.should eq 'pass2'
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
it "correctly formats cookies provided to the constructor" do
|
111
|
+
URI.stub(:parse).and_return(double('uri', :user => nil, :password => nil))
|
112
|
+
@request = RestClient::Request.new(:method => 'get', :url => 'example.com', :cookies => {:session_id => '1', :user_id => "someone" })
|
113
|
+
@request.should_receive(:default_headers).and_return({'Foo' => 'bar'})
|
114
|
+
@request.make_headers({}).should eq({ 'Foo' => 'bar', 'Cookie' => 'session_id=1; user_id=someone'})
|
115
|
+
end
|
116
|
+
|
117
|
+
it "uses netrc credentials" do
|
118
|
+
URI.stub(:parse).and_return(double('uri', :user => nil, :password => nil, :host => 'example.com'))
|
119
|
+
Netrc.stub(:read).and_return('example.com' => ['a', 'b'])
|
120
|
+
@request.parse_url_with_auth('http://example.com/resource')
|
121
|
+
@request.user.should eq 'a'
|
122
|
+
@request.password.should eq 'b'
|
123
|
+
end
|
124
|
+
|
125
|
+
it "uses credentials in the url in preference to netrc" do
|
126
|
+
URI.stub(:parse).and_return(double('uri', :user => 'joe%20', :password => 'pass1', :host => 'example.com'))
|
127
|
+
Netrc.stub(:read).and_return('example.com' => ['a', 'b'])
|
128
|
+
@request.parse_url_with_auth('http://joe%20:pass1@example.com/resource')
|
129
|
+
@request.user.should eq 'joe '
|
130
|
+
@request.password.should eq 'pass1'
|
131
|
+
end
|
132
|
+
|
133
|
+
it "determines the Net::HTTP class to instantiate by the method name" do
|
134
|
+
@request.net_http_request_class(:put).should eq Net::HTTP::Put
|
135
|
+
end
|
136
|
+
|
137
|
+
describe "user headers" do
|
138
|
+
it "merges user headers with the default headers" do
|
139
|
+
@request.should_receive(:default_headers).and_return({ :accept => '*/*; q=0.5, application/xml', :accept_encoding => 'gzip, deflate' })
|
140
|
+
headers = @request.make_headers("Accept" => "application/json", :accept_encoding => 'gzip')
|
141
|
+
headers.should have_key "Accept-Encoding"
|
142
|
+
headers["Accept-Encoding"].should eq "gzip"
|
143
|
+
headers.should have_key "Accept"
|
144
|
+
headers["Accept"].should eq "application/json"
|
145
|
+
end
|
146
|
+
|
147
|
+
it "prefers the user header when the same header exists in the defaults" do
|
148
|
+
@request.should_receive(:default_headers).and_return({ '1' => '2' })
|
149
|
+
headers = @request.make_headers('1' => '3')
|
150
|
+
headers.should have_key('1')
|
151
|
+
headers['1'].should eq '3'
|
152
|
+
end
|
153
|
+
|
154
|
+
it "converts user headers to string before calling CGI::unescape which fails on non string values" do
|
155
|
+
@request.should_receive(:default_headers).and_return({ '1' => '2' })
|
156
|
+
headers = @request.make_headers('1' => 3)
|
157
|
+
headers.should have_key('1')
|
158
|
+
headers['1'].should eq '3'
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe "header symbols" do
|
163
|
+
|
164
|
+
it "converts header symbols from :content_type to 'Content-Type'" do
|
165
|
+
@request.should_receive(:default_headers).and_return({})
|
166
|
+
headers = @request.make_headers(:content_type => 'abc')
|
167
|
+
headers.should have_key('Content-Type')
|
168
|
+
headers['Content-Type'].should eq 'abc'
|
169
|
+
end
|
170
|
+
|
171
|
+
it "converts content-type from extension to real content-type" do
|
172
|
+
@request.should_receive(:default_headers).and_return({})
|
173
|
+
headers = @request.make_headers(:content_type => 'json')
|
174
|
+
headers.should have_key('Content-Type')
|
175
|
+
headers['Content-Type'].should eq 'application/json'
|
176
|
+
end
|
177
|
+
|
178
|
+
it "converts accept from extension(s) to real content-type(s)" do
|
179
|
+
@request.should_receive(:default_headers).and_return({})
|
180
|
+
headers = @request.make_headers(:accept => 'json, mp3')
|
181
|
+
headers.should have_key('Accept')
|
182
|
+
headers['Accept'].should eq 'application/json, audio/mpeg'
|
183
|
+
|
184
|
+
@request.should_receive(:default_headers).and_return({})
|
185
|
+
headers = @request.make_headers(:accept => :json)
|
186
|
+
headers.should have_key('Accept')
|
187
|
+
headers['Accept'].should eq 'application/json'
|
188
|
+
end
|
189
|
+
|
190
|
+
it "only convert symbols in header" do
|
191
|
+
@request.should_receive(:default_headers).and_return({})
|
192
|
+
headers = @request.make_headers({:foo_bar => 'value', "bar_bar" => 'value'})
|
193
|
+
headers['Foo-Bar'].should eq 'value'
|
194
|
+
headers['bar_bar'].should eq 'value'
|
195
|
+
end
|
196
|
+
|
197
|
+
it "converts header values to strings" do
|
198
|
+
@request.make_headers('A' => 1)['A'].should eq '1'
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
|
203
|
+
@request.should_receive(:parse_url_with_auth).with('http://some/resource').and_return(@uri)
|
204
|
+
klass = double("net:http class")
|
205
|
+
@request.should_receive(:net_http_request_class).with(:put).and_return(klass)
|
206
|
+
klass.should_receive(:new).and_return('result')
|
207
|
+
@request.should_receive(:transmit).with(@uri, 'result', kind_of(RestClient::Payload::Base))
|
208
|
+
@request.execute
|
209
|
+
end
|
210
|
+
|
211
|
+
it "transmits the request with Net::HTTP" do
|
212
|
+
req = net_http_post_request
|
213
|
+
@http.should_receive(:request).with(req)
|
214
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
215
|
+
@request.should_receive(:process_result)
|
216
|
+
@request.transmit(@uri, req, payload)
|
217
|
+
end
|
218
|
+
|
219
|
+
describe "payload" do
|
220
|
+
it "sends nil payloads" do
|
221
|
+
req = net_http_post_request
|
222
|
+
@http.should_receive(:request).with(req)
|
223
|
+
@http.should_not_receive(:body_stream=)
|
224
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
225
|
+
@request.should_receive(:process_result)
|
226
|
+
@request.stub(:response_log)
|
227
|
+
@request.transmit(@uri, req, nil)
|
228
|
+
end
|
229
|
+
|
230
|
+
it "passes non-hash payloads straight through" do
|
231
|
+
@request.process_payload("x").should eq "x"
|
232
|
+
end
|
233
|
+
|
234
|
+
it "converts a hash payload to urlencoded data" do
|
235
|
+
@request.process_payload(:a => 'b c+d').should eq "a=b%20c%2Bd"
|
236
|
+
end
|
237
|
+
|
238
|
+
it "accepts nested hashes in payload" do
|
239
|
+
payload = @request.process_payload(:user => { :name => 'joe', :location => { :country => 'USA', :state => 'CA' }})
|
240
|
+
payload.should include('user[name]=joe')
|
241
|
+
payload.should include('user[location][country]=USA')
|
242
|
+
payload.should include('user[location][state]=CA')
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
it "set urlencoded content_type header on hash payloads" do
|
247
|
+
@request.process_payload(:a => 1)
|
248
|
+
@request.headers[:content_type].should eq 'application/x-www-form-urlencoded'
|
249
|
+
end
|
250
|
+
|
251
|
+
describe "credentials" do
|
252
|
+
it "sets up the credentials prior to the request" do
|
253
|
+
@http.stub(:request)
|
254
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
255
|
+
|
256
|
+
@request.stub(:process_result)
|
257
|
+
@request.stub(:response_log)
|
258
|
+
|
259
|
+
@request.stub(:user).and_return('joe')
|
260
|
+
@request.stub(:password).and_return('mypass')
|
261
|
+
@request.should_receive(:setup_credentials).with('req')
|
262
|
+
|
263
|
+
@request.transmit(@uri, 'req', nil)
|
264
|
+
end
|
265
|
+
|
266
|
+
it "does not attempt to send any credentials if user is nil" do
|
267
|
+
@request.stub(:user).and_return(nil)
|
268
|
+
req = double("request")
|
269
|
+
req.should_not_receive(:basic_auth)
|
270
|
+
@request.setup_credentials(req)
|
271
|
+
end
|
272
|
+
|
273
|
+
it "setup credentials when there's a user" do
|
274
|
+
@request.stub(:user).and_return('joe')
|
275
|
+
@request.stub(:password).and_return('mypass')
|
276
|
+
req = double("request")
|
277
|
+
req.should_receive(:basic_auth).with('joe', 'mypass')
|
278
|
+
@request.setup_credentials(req)
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
it "catches EOFError and shows the more informative ServerBrokeConnection" do
|
283
|
+
@http.stub(:request).and_raise(EOFError)
|
284
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
285
|
+
lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::ServerBrokeConnection)
|
286
|
+
end
|
287
|
+
|
288
|
+
it "class method execute wraps constructor" do
|
289
|
+
req = double("rest request")
|
290
|
+
RestClient::Request.should_receive(:new).with(1 => 2).and_return(req)
|
291
|
+
req.should_receive(:execute)
|
292
|
+
RestClient::Request.execute(1 => 2)
|
293
|
+
end
|
294
|
+
|
295
|
+
describe "exception" do
|
296
|
+
it "raises Unauthorized when the response is 401" do
|
297
|
+
res = double('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' )
|
298
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
|
299
|
+
end
|
300
|
+
|
301
|
+
it "raises ResourceNotFound when the response is 404" do
|
302
|
+
res = double('response', :code => '404', :[] => ['content-encoding' => ''], :body => '' )
|
303
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::ResourceNotFound)
|
304
|
+
end
|
305
|
+
|
306
|
+
it "raises RequestFailed otherwise" do
|
307
|
+
res = double('response', :code => '500', :[] => ['content-encoding' => ''], :body => '' )
|
308
|
+
lambda { @request.process_result(res) }.should raise_error(RestClient::InternalServerError)
|
309
|
+
end
|
310
|
+
end
|
311
|
+
|
312
|
+
describe "block usage" do
|
313
|
+
it "returns what asked to" do
|
314
|
+
res = double('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' )
|
315
|
+
@request.process_result(res){|response, request| "foo"}.should eq "foo"
|
316
|
+
end
|
317
|
+
end
|
318
|
+
|
319
|
+
describe "proxy" do
|
320
|
+
it "creates a proxy class if a proxy url is given" do
|
321
|
+
RestClient.stub(:proxy).and_return("http://example.com/")
|
322
|
+
@request.net_http_class.proxy_class?.should be_true
|
323
|
+
end
|
324
|
+
|
325
|
+
it "creates a non-proxy class if a proxy url is not given" do
|
326
|
+
@request.net_http_class.proxy_class?.should be_false
|
327
|
+
end
|
328
|
+
end
|
329
|
+
|
330
|
+
|
331
|
+
describe "logging" do
|
332
|
+
it "logs a get request" do
|
333
|
+
log = RestClient.log = []
|
334
|
+
RestClient::Request.new(:method => :get, :url => 'http://url').log_request
|
335
|
+
log[0].should eq %Q{RestClient.get "http://url", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate"\n}
|
336
|
+
end
|
337
|
+
|
338
|
+
it "logs a post request with a small payload" do
|
339
|
+
log = RestClient.log = []
|
340
|
+
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').log_request
|
341
|
+
log[0].should eq %Q{RestClient.post "http://url", "foo", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"3"\n}
|
342
|
+
end
|
343
|
+
|
344
|
+
it "logs a post request with a large payload" do
|
345
|
+
log = RestClient.log = []
|
346
|
+
RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).log_request
|
347
|
+
log[0].should eq %Q{RestClient.post "http://url", 1000 byte(s) length, "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"1000"\n}
|
348
|
+
end
|
349
|
+
|
350
|
+
it "logs input headers as a hash" do
|
351
|
+
log = RestClient.log = []
|
352
|
+
RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).log_request
|
353
|
+
log[0].should eq %Q{RestClient.get "http://url", "Accept"=>"text/plain", "Accept-Encoding"=>"gzip, deflate"\n}
|
354
|
+
end
|
355
|
+
|
356
|
+
it "logs a response including the status code, content type, and result body size in bytes" do
|
357
|
+
log = RestClient.log = []
|
358
|
+
res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
|
359
|
+
res.stub(:[]).with('Content-type').and_return('text/html')
|
360
|
+
@request.log_response res
|
361
|
+
log[0].should eq "# => 200 OK | text/html 4 bytes\n"
|
362
|
+
end
|
363
|
+
|
364
|
+
it "logs a response with a nil Content-type" do
|
365
|
+
log = RestClient.log = []
|
366
|
+
res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
|
367
|
+
res.stub(:[]).with('Content-type').and_return(nil)
|
368
|
+
@request.log_response res
|
369
|
+
log[0].should eq "# => 200 OK | 4 bytes\n"
|
370
|
+
end
|
371
|
+
|
372
|
+
it "logs a response with a nil body" do
|
373
|
+
log = RestClient.log = []
|
374
|
+
res = double('result', :code => '200', :class => Net::HTTPOK, :body => nil)
|
375
|
+
res.stub(:[]).with('Content-type').and_return('text/html; charset=utf-8')
|
376
|
+
@request.log_response res
|
377
|
+
log[0].should eq "# => 200 OK | text/html 0 bytes\n"
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
381
|
+
it "strips the charset from the response content type" do
|
382
|
+
log = RestClient.log = []
|
383
|
+
res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
|
384
|
+
res.stub(:[]).with('Content-type').and_return('text/html; charset=utf-8')
|
385
|
+
@request.log_response res
|
386
|
+
log[0].should eq "# => 200 OK | text/html 4 bytes\n"
|
387
|
+
end
|
388
|
+
|
389
|
+
describe "timeout" do
|
390
|
+
it "set read_timeout" do
|
391
|
+
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123, :ssl_version => 'SSLv3')
|
392
|
+
@http.stub(:request)
|
393
|
+
@request.stub(:process_result)
|
394
|
+
@request.stub(:response_log)
|
395
|
+
|
396
|
+
@net.should_receive(:read_timeout=).with(123)
|
397
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
398
|
+
|
399
|
+
@request.transmit(@uri, 'req', nil)
|
400
|
+
end
|
401
|
+
|
402
|
+
it "set open_timeout" do
|
403
|
+
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :open_timeout => 123, :ssl_version => 'SSLv3')
|
404
|
+
@http.stub(:request)
|
405
|
+
@request.stub(:process_result)
|
406
|
+
@request.stub(:response_log)
|
407
|
+
|
408
|
+
@net.should_receive(:open_timeout=).with(123)
|
409
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
410
|
+
|
411
|
+
@request.transmit(@uri, 'req', nil)
|
412
|
+
end
|
413
|
+
end
|
414
|
+
|
415
|
+
describe "ssl" do
|
416
|
+
it "uses SSL when the URI refers to a https address" do
|
417
|
+
@uri.stub(:is_a?).with(URI::HTTPS).and_return(true)
|
418
|
+
@net.should_receive(:use_ssl=).with(true)
|
419
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
420
|
+
@http.stub(:request)
|
421
|
+
@request.stub(:process_result)
|
422
|
+
@request.stub(:response_log)
|
423
|
+
@request.transmit(@uri, net_http_post_request, payload)
|
424
|
+
end
|
425
|
+
|
426
|
+
it "should default to not verifying ssl certificates" do
|
427
|
+
@request.verify_ssl.should eq false
|
428
|
+
end
|
429
|
+
|
430
|
+
it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do
|
431
|
+
@net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
|
432
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
433
|
+
@http.stub(:request)
|
434
|
+
@request.stub(:process_result)
|
435
|
+
@request.stub(:response_log)
|
436
|
+
@request.transmit(@uri, net_http_post_request, payload)
|
437
|
+
end
|
438
|
+
|
439
|
+
it "should not set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is true" do
|
440
|
+
@request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true, :ssl_version => 'SSLv3')
|
441
|
+
@net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
|
442
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
443
|
+
@http.stub(:request)
|
444
|
+
@request.stub(:process_result)
|
445
|
+
@request.stub(:response_log)
|
446
|
+
@request.transmit(@uri, net_http_post_request, payload)
|
447
|
+
end
|
448
|
+
|
449
|
+
it "should set net.verify_mode to the passed value if verify_ssl is an OpenSSL constant" do
|
450
|
+
mode = OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
|
451
|
+
@request = RestClient::Request.new( :method => :put,
|
452
|
+
:url => 'https://some/resource',
|
453
|
+
:payload => 'payload',
|
454
|
+
:ssl_version => 'SSLv3',
|
455
|
+
:verify_ssl => mode )
|
456
|
+
@net.should_receive(:verify_mode=).with(mode)
|
457
|
+
@net.should_receive(:verify_callback=)
|
458
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
459
|
+
@http.stub(:request)
|
460
|
+
@request.stub(:process_result)
|
461
|
+
@request.stub(:response_log)
|
462
|
+
@request.transmit(@uri, net_http_post_request, payload)
|
463
|
+
end
|
464
|
+
|
465
|
+
it "should default to not having an ssl_client_cert" do
|
466
|
+
@request.ssl_client_cert.should be(nil)
|
467
|
+
end
|
468
|
+
|
469
|
+
it "should set the ssl_client_cert if provided" do
|
470
|
+
@request = RestClient::Request.new(
|
471
|
+
:method => :put,
|
472
|
+
:url => 'https://some/resource',
|
473
|
+
:payload => 'payload',
|
474
|
+
:ssl_version => 'SSLv3',
|
475
|
+
:ssl_client_cert => "whatsupdoc!"
|
476
|
+
)
|
477
|
+
@net.should_receive(:cert=).with("whatsupdoc!")
|
478
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
479
|
+
@http.stub(:request)
|
480
|
+
@request.stub(:process_result)
|
481
|
+
@request.stub(:response_log)
|
482
|
+
@request.transmit(@uri, net_http_post_request, payload)
|
483
|
+
end
|
484
|
+
|
485
|
+
it "should not set the ssl_client_cert if it is not provided" do
|
486
|
+
@request = RestClient::Request.new(
|
487
|
+
:method => :put,
|
488
|
+
:url => 'https://some/resource',
|
489
|
+
:ssl_version => 'SSLv3',
|
490
|
+
:payload => 'payload'
|
491
|
+
)
|
492
|
+
@net.should_not_receive(:cert=).with("whatsupdoc!")
|
493
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
494
|
+
@http.stub(:request)
|
495
|
+
@request.stub(:process_result)
|
496
|
+
@request.stub(:response_log)
|
497
|
+
@request.transmit(@uri, net_http_post_request, payload)
|
498
|
+
end
|
499
|
+
|
500
|
+
it "should default to not having an ssl_client_key" do
|
501
|
+
@request.ssl_client_key.should be(nil)
|
502
|
+
end
|
503
|
+
|
504
|
+
it "should set the ssl_client_key if provided" do
|
505
|
+
@request = RestClient::Request.new(
|
506
|
+
:method => :put,
|
507
|
+
:url => 'https://some/resource',
|
508
|
+
:payload => 'payload',
|
509
|
+
:ssl_version => 'SSLv3',
|
510
|
+
:ssl_client_key => "whatsupdoc!"
|
511
|
+
)
|
512
|
+
@net.should_receive(:key=).with("whatsupdoc!")
|
513
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
514
|
+
@http.stub(:request)
|
515
|
+
@request.stub(:process_result)
|
516
|
+
@request.stub(:response_log)
|
517
|
+
@request.transmit(@uri, net_http_post_request, payload)
|
518
|
+
end
|
519
|
+
|
520
|
+
it "should not set the ssl_client_key if it is not provided" do
|
521
|
+
@request = RestClient::Request.new(
|
522
|
+
:method => :put,
|
523
|
+
:url => 'https://some/resource',
|
524
|
+
:ssl_version => 'SSLv3',
|
525
|
+
:payload => 'payload'
|
526
|
+
)
|
527
|
+
@net.should_not_receive(:key=).with("whatsupdoc!")
|
528
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
529
|
+
@http.stub(:request)
|
530
|
+
@request.stub(:process_result)
|
531
|
+
@request.stub(:response_log)
|
532
|
+
@request.transmit(@uri, net_http_post_request, payload)
|
533
|
+
end
|
534
|
+
|
535
|
+
it "should default to not having an ssl_ca_file" do
|
536
|
+
@request.ssl_ca_file.should be(nil)
|
537
|
+
end
|
538
|
+
|
539
|
+
it "should set the ssl_ca_file if provided" do
|
540
|
+
@request = RestClient::Request.new(
|
541
|
+
:method => :put,
|
542
|
+
:url => 'https://some/resource',
|
543
|
+
:payload => 'payload',
|
544
|
+
:ssl_version => 'SSLv3',
|
545
|
+
:ssl_ca_file => "Certificate Authority File"
|
546
|
+
)
|
547
|
+
@net.should_receive(:ca_file=).with("Certificate Authority File")
|
548
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
549
|
+
@http.stub(:request)
|
550
|
+
@request.stub(:process_result)
|
551
|
+
@request.stub(:response_log)
|
552
|
+
@request.transmit(@uri, net_http_post_request, payload)
|
553
|
+
end
|
554
|
+
|
555
|
+
it "should not set the ssl_ca_file if it is not provided" do
|
556
|
+
@request = RestClient::Request.new(
|
557
|
+
:method => :put,
|
558
|
+
:url => 'https://some/resource',
|
559
|
+
:ssl_version => 'TSLv1',
|
560
|
+
:payload => 'payload'
|
561
|
+
)
|
562
|
+
@net.should_not_receive(:ca_file=).with("Certificate Authority File")
|
563
|
+
@net.should_receive(:ssl_version=).with('TSLv1')
|
564
|
+
@http.stub(:request)
|
565
|
+
@request.stub(:process_result)
|
566
|
+
@request.stub(:response_log)
|
567
|
+
@request.transmit(@uri, net_http_post_request, payload)
|
568
|
+
end
|
569
|
+
|
570
|
+
it "should default to not having an ssl_ca_path" do
|
571
|
+
@request.ssl_ca_path.should be(nil)
|
572
|
+
end
|
573
|
+
|
574
|
+
it "should set the ssl_ca_path if provided" do
|
575
|
+
@request = RestClient::Request.new(
|
576
|
+
:method => :put,
|
577
|
+
:url => 'https://some/resource',
|
578
|
+
:payload => 'payload',
|
579
|
+
:ssl_version => 'SSLv3',
|
580
|
+
:ssl_ca_path => "Certificate Authority Path"
|
581
|
+
)
|
582
|
+
@net.should_receive(:ca_path=).with("Certificate Authority Path")
|
583
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
584
|
+
@http.stub(:request)
|
585
|
+
@request.stub(:process_result)
|
586
|
+
@request.stub(:response_log)
|
587
|
+
@request.transmit(@uri, net_http_post_request, payload)
|
588
|
+
end
|
589
|
+
|
590
|
+
it "should not set the ssl_ca_path if it is not provided" do
|
591
|
+
@request = RestClient::Request.new(
|
592
|
+
:method => :put,
|
593
|
+
:url => 'https://some/resource',
|
594
|
+
:ssl_version => 'TSLv1',
|
595
|
+
:payload => 'payload'
|
596
|
+
)
|
597
|
+
@net.should_not_receive(:ca_path=).with("Certificate Authority File")
|
598
|
+
@net.should_receive(:ssl_version=).with('TSLv1')
|
599
|
+
@http.stub(:request)
|
600
|
+
@request.stub(:process_result)
|
601
|
+
@request.stub(:response_log)
|
602
|
+
@request.transmit(@uri, net_http_post_request, payload)
|
603
|
+
end
|
604
|
+
end
|
605
|
+
|
606
|
+
it "should still return a response object for 204 No Content responses" do
|
607
|
+
@request = RestClient::Request.new(
|
608
|
+
:method => :put,
|
609
|
+
:url => 'https://some/resource',
|
610
|
+
:ssl_version => 'SSLv3',
|
611
|
+
:payload => 'payload'
|
612
|
+
)
|
613
|
+
net_http_res = Net::HTTPNoContent.new("", "204", "No Content")
|
614
|
+
net_http_res.stub(:read_body).and_return(nil)
|
615
|
+
@http.should_receive(:request).and_return(@request.fetch_body(net_http_res))
|
616
|
+
@net.should_receive(:ssl_version=).with('SSLv3')
|
617
|
+
response = @request.transmit(@uri, net_http_post_request, payload)
|
618
|
+
response.should_not be_nil
|
619
|
+
response.code.should eq 204
|
620
|
+
end
|
621
|
+
end
|