rest-client 1.6.7 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -0
  3. data/.rspec +1 -0
  4. data/.travis.yml +14 -0
  5. data/AUTHORS +81 -0
  6. data/Gemfile +11 -0
  7. data/LICENSE +21 -0
  8. data/README.rdoc +63 -24
  9. data/Rakefile +85 -35
  10. data/bin/restclient +9 -8
  11. data/history.md +63 -1
  12. data/lib/restclient/abstract_response.rb +44 -15
  13. data/lib/restclient/exceptions.rb +20 -10
  14. data/lib/restclient/payload.rb +21 -18
  15. data/lib/restclient/platform.rb +30 -0
  16. data/lib/restclient/raw_response.rb +3 -2
  17. data/lib/restclient/request.rb +368 -63
  18. data/lib/restclient/resource.rb +3 -4
  19. data/lib/restclient/response.rb +2 -5
  20. data/lib/restclient/version.rb +7 -0
  21. data/lib/restclient/windows/root_certs.rb +105 -0
  22. data/lib/restclient/windows.rb +8 -0
  23. data/lib/restclient.rb +6 -15
  24. data/rest-client.gemspec +30 -0
  25. data/rest-client.windows.gemspec +19 -0
  26. data/spec/integration/capath_digicert/244b5494.0 +19 -0
  27. data/spec/integration/capath_digicert/81b9768f.0 +19 -0
  28. data/spec/integration/capath_digicert/README +8 -0
  29. data/spec/integration/capath_digicert/digicert.crt +19 -0
  30. data/spec/integration/capath_verisign/415660c1.0 +14 -0
  31. data/spec/integration/capath_verisign/7651b327.0 +14 -0
  32. data/spec/integration/capath_verisign/README +8 -0
  33. data/spec/integration/capath_verisign/verisign.crt +14 -0
  34. data/spec/integration/certs/digicert.crt +19 -0
  35. data/spec/{integration_spec.rb → integration/integration_spec.rb} +10 -13
  36. data/spec/integration/request_spec.rb +86 -7
  37. data/spec/spec_helper.rb +2 -0
  38. data/spec/{abstract_response_spec.rb → unit/abstract_response_spec.rb} +18 -15
  39. data/spec/{exceptions_spec.rb → unit/exceptions_spec.rb} +17 -20
  40. data/spec/unit/master_shake.jpg +0 -0
  41. data/spec/{payload_spec.rb → unit/payload_spec.rb} +42 -31
  42. data/spec/unit/raw_response_spec.rb +18 -0
  43. data/spec/{request2_spec.rb → unit/request2_spec.rb} +6 -14
  44. data/spec/unit/request_spec.rb +917 -0
  45. data/spec/{resource_spec.rb → unit/resource_spec.rb} +27 -31
  46. data/spec/{response_spec.rb → unit/response_spec.rb} +63 -57
  47. data/spec/{restclient_spec.rb → unit/restclient_spec.rb} +8 -2
  48. data/spec/unit/windows/root_certs_spec.rb +22 -0
  49. metadata +210 -112
  50. data/VERSION +0 -1
  51. data/lib/restclient/net_http_ext.rb +0 -55
  52. data/spec/base.rb +0 -16
  53. data/spec/integration/certs/equifax.crt +0 -19
  54. data/spec/master_shake.jpg +0 -0
  55. data/spec/raw_response_spec.rb +0 -17
  56. data/spec/request_spec.rb +0 -529
@@ -0,0 +1,917 @@
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
+ @net.stub(:verify_callback=)
19
+ allow(@net).to receive(:ciphers=)
20
+ allow(@net).to receive(:cert_store=)
21
+ RestClient.log = nil
22
+ end
23
+
24
+ it "accept */* mimetype, preferring xml" do
25
+ @request.default_headers[:accept].should eq '*/*; 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 eq '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 eq "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 eq "some deflated text"
49
+ end
50
+ end
51
+
52
+ it "processes a successful result" do
53
+ res = double("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 eq 'body'
58
+ @request.process_result(res).to_s.should eq 'body'
59
+ end
60
+
61
+ it "doesn't classify successful requests as failed" do
62
+ 203.upto(207) do |code|
63
+ res = double("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(double('uri', :user => 'joe', :password => 'pass1'))
84
+ @request.parse_url_with_auth('http://joe:pass1@example.com/resource')
85
+ @request.user.should eq 'joe'
86
+ @request.password.should eq '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(double('uri', :user => 'joe%20', :password => 'pass1'))
91
+ @request.parse_url_with_auth('http://joe%20:pass1@example.com/resource')
92
+ @request.user.should eq 'joe '
93
+ @request.password.should eq '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(double('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 eq 'beth'
101
+ @request.password.should eq 'pass2'
102
+ end
103
+ end
104
+
105
+ it "correctly formats cookies provided to the constructor" do
106
+ URI.stub(:parse).and_return(double('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 eq({ 'Foo' => 'bar', 'Cookie' => 'session_id=1; user_id=someone'})
110
+ end
111
+
112
+ it "does not escape or unescape cookies" do
113
+ cookie = 'Foo%20:Bar%0A~'
114
+ @request = RestClient::Request.new(:method => 'get', :url => 'example.com',
115
+ :cookies => {:test => cookie})
116
+ @request.should_receive(:default_headers).and_return({'Foo' => 'bar'})
117
+ @request.make_headers({}).should eq({
118
+ 'Foo' => 'bar',
119
+ 'Cookie' => "test=#{cookie}"
120
+ })
121
+ end
122
+
123
+ it "rejects cookie names containing invalid characters" do
124
+ # Cookie validity is something of a mess, but we should reject the worst of
125
+ # the RFC 6265 (4.1.1) prohibited characters such as control characters.
126
+
127
+ ['', 'foo=bar', 'foo;bar', "foo\nbar"].each do |cookie_name|
128
+ lambda {
129
+ RestClient::Request.new(:method => 'get', :url => 'example.com',
130
+ :cookies => {cookie_name => 'value'})
131
+ }.should raise_error(ArgumentError, /\AInvalid cookie name/)
132
+ end
133
+ end
134
+
135
+ it "rejects cookie values containing invalid characters" do
136
+ # Cookie validity is something of a mess, but we should reject the worst of
137
+ # the RFC 6265 (4.1.1) prohibited characters such as control characters.
138
+
139
+ ['foo,bar', 'foo;bar', "foo\nbar"].each do |cookie_value|
140
+ lambda {
141
+ RestClient::Request.new(:method => 'get', :url => 'example.com',
142
+ :cookies => {'test' => cookie_value})
143
+ }.should raise_error(ArgumentError, /\AInvalid cookie value/)
144
+ end
145
+ end
146
+
147
+ it "uses netrc credentials" do
148
+ URI.stub(:parse).and_return(double('uri', :user => nil, :password => nil, :host => 'example.com'))
149
+ Netrc.stub(:read).and_return('example.com' => ['a', 'b'])
150
+ @request.parse_url_with_auth('http://example.com/resource')
151
+ @request.user.should eq 'a'
152
+ @request.password.should eq 'b'
153
+ end
154
+
155
+ it "uses credentials in the url in preference to netrc" do
156
+ URI.stub(:parse).and_return(double('uri', :user => 'joe%20', :password => 'pass1', :host => 'example.com'))
157
+ Netrc.stub(:read).and_return('example.com' => ['a', 'b'])
158
+ @request.parse_url_with_auth('http://joe%20:pass1@example.com/resource')
159
+ @request.user.should eq 'joe '
160
+ @request.password.should eq 'pass1'
161
+ end
162
+
163
+ it "determines the Net::HTTP class to instantiate by the method name" do
164
+ @request.net_http_request_class(:put).should eq Net::HTTP::Put
165
+ end
166
+
167
+ describe "user headers" do
168
+ it "merges user headers with the default headers" do
169
+ @request.should_receive(:default_headers).and_return({ :accept => '*/*; q=0.5, application/xml', :accept_encoding => 'gzip, deflate' })
170
+ headers = @request.make_headers("Accept" => "application/json", :accept_encoding => 'gzip')
171
+ headers.should have_key "Accept-Encoding"
172
+ headers["Accept-Encoding"].should eq "gzip"
173
+ headers.should have_key "Accept"
174
+ headers["Accept"].should eq "application/json"
175
+ end
176
+
177
+ it "prefers the user header when the same header exists in the defaults" do
178
+ @request.should_receive(:default_headers).and_return({ '1' => '2' })
179
+ headers = @request.make_headers('1' => '3')
180
+ headers.should have_key('1')
181
+ headers['1'].should eq '3'
182
+ end
183
+
184
+ it "converts user headers to string before calling CGI::unescape which fails on non string values" do
185
+ @request.should_receive(:default_headers).and_return({ '1' => '2' })
186
+ headers = @request.make_headers('1' => 3)
187
+ headers.should have_key('1')
188
+ headers['1'].should eq '3'
189
+ end
190
+ end
191
+
192
+ describe "header symbols" do
193
+
194
+ it "converts header symbols from :content_type to 'Content-Type'" do
195
+ @request.should_receive(:default_headers).and_return({})
196
+ headers = @request.make_headers(:content_type => 'abc')
197
+ headers.should have_key('Content-Type')
198
+ headers['Content-Type'].should eq 'abc'
199
+ end
200
+
201
+ it "converts content-type from extension to real content-type" do
202
+ @request.should_receive(:default_headers).and_return({})
203
+ headers = @request.make_headers(:content_type => 'json')
204
+ headers.should have_key('Content-Type')
205
+ headers['Content-Type'].should eq 'application/json'
206
+ end
207
+
208
+ it "converts accept from extension(s) to real content-type(s)" do
209
+ @request.should_receive(:default_headers).and_return({})
210
+ headers = @request.make_headers(:accept => 'json, mp3')
211
+ headers.should have_key('Accept')
212
+ headers['Accept'].should eq 'application/json, audio/mpeg'
213
+
214
+ @request.should_receive(:default_headers).and_return({})
215
+ headers = @request.make_headers(:accept => :json)
216
+ headers.should have_key('Accept')
217
+ headers['Accept'].should eq 'application/json'
218
+ end
219
+
220
+ it "only convert symbols in header" do
221
+ @request.should_receive(:default_headers).and_return({})
222
+ headers = @request.make_headers({:foo_bar => 'value', "bar_bar" => 'value'})
223
+ headers['Foo-Bar'].should eq 'value'
224
+ headers['bar_bar'].should eq 'value'
225
+ end
226
+
227
+ it "converts header values to strings" do
228
+ @request.make_headers('A' => 1)['A'].should eq '1'
229
+ end
230
+ end
231
+
232
+ it "executes by constructing the Net::HTTP object, headers, and payload and calling transmit" do
233
+ @request.should_receive(:parse_url_with_auth).with('http://some/resource').and_return(@uri)
234
+ klass = double("net:http class")
235
+ @request.should_receive(:net_http_request_class).with(:put).and_return(klass)
236
+ klass.should_receive(:new).and_return('result')
237
+ @request.should_receive(:transmit).with(@uri, 'result', kind_of(RestClient::Payload::Base))
238
+ @request.execute
239
+ end
240
+
241
+ it "transmits the request with Net::HTTP" do
242
+ @http.should_receive(:request).with('req', 'payload')
243
+ @request.should_receive(:process_result)
244
+ @request.transmit(@uri, 'req', 'payload')
245
+ end
246
+
247
+ describe "payload" do
248
+ it "sends nil payloads" do
249
+ @http.should_receive(:request).with('req', nil)
250
+ @request.should_receive(:process_result)
251
+ @request.stub(:response_log)
252
+ @request.transmit(@uri, 'req', nil)
253
+ end
254
+
255
+ it "passes non-hash payloads straight through" do
256
+ @request.process_payload("x").should eq "x"
257
+ end
258
+
259
+ it "converts a hash payload to urlencoded data" do
260
+ @request.process_payload(:a => 'b c+d').should eq "a=b%20c%2Bd"
261
+ end
262
+
263
+ it "accepts nested hashes in payload" do
264
+ payload = @request.process_payload(:user => { :name => 'joe', :location => { :country => 'USA', :state => 'CA' }})
265
+ payload.should include('user[name]=joe')
266
+ payload.should include('user[location][country]=USA')
267
+ payload.should include('user[location][state]=CA')
268
+ end
269
+ end
270
+
271
+ it "set urlencoded content_type header on hash payloads" do
272
+ @request.process_payload(:a => 1)
273
+ @request.headers[:content_type].should eq 'application/x-www-form-urlencoded'
274
+ end
275
+
276
+ describe "credentials" do
277
+ it "sets up the credentials prior to the request" do
278
+ @http.stub(:request)
279
+
280
+ @request.stub(:process_result)
281
+ @request.stub(:response_log)
282
+
283
+ @request.stub(:user).and_return('joe')
284
+ @request.stub(:password).and_return('mypass')
285
+ @request.should_receive(:setup_credentials).with('req')
286
+
287
+ @request.transmit(@uri, 'req', nil)
288
+ end
289
+
290
+ it "does not attempt to send any credentials if user is nil" do
291
+ @request.stub(:user).and_return(nil)
292
+ req = double("request")
293
+ req.should_not_receive(:basic_auth)
294
+ @request.setup_credentials(req)
295
+ end
296
+
297
+ it "setup credentials when there's a user" do
298
+ @request.stub(:user).and_return('joe')
299
+ @request.stub(:password).and_return('mypass')
300
+ req = double("request")
301
+ req.should_receive(:basic_auth).with('joe', 'mypass')
302
+ @request.setup_credentials(req)
303
+ end
304
+ end
305
+
306
+ it "catches EOFError and shows the more informative ServerBrokeConnection" do
307
+ @http.stub(:request).and_raise(EOFError)
308
+ lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::ServerBrokeConnection)
309
+ end
310
+
311
+ it "catches OpenSSL::SSL::SSLError and raise it back without more informative message" do
312
+ @http.stub(:request).and_raise(OpenSSL::SSL::SSLError)
313
+ lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(OpenSSL::SSL::SSLError)
314
+ end
315
+
316
+ it "catches Timeout::Error and raise the more informative RequestTimeout" do
317
+ @http.stub(:request).and_raise(Timeout::Error)
318
+ lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::RequestTimeout)
319
+ end
320
+
321
+ it "catches Timeout::Error and raise the more informative RequestTimeout" do
322
+ @http.stub(:request).and_raise(Errno::ETIMEDOUT)
323
+ lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::RequestTimeout)
324
+ end
325
+
326
+ it "class method execute wraps constructor" do
327
+ req = double("rest request")
328
+ RestClient::Request.should_receive(:new).with(1 => 2).and_return(req)
329
+ req.should_receive(:execute)
330
+ RestClient::Request.execute(1 => 2)
331
+ end
332
+
333
+ describe "exception" do
334
+ it "raises Unauthorized when the response is 401" do
335
+ res = double('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' )
336
+ lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
337
+ end
338
+
339
+ it "raises ResourceNotFound when the response is 404" do
340
+ res = double('response', :code => '404', :[] => ['content-encoding' => ''], :body => '' )
341
+ lambda { @request.process_result(res) }.should raise_error(RestClient::ResourceNotFound)
342
+ end
343
+
344
+ it "raises RequestFailed otherwise" do
345
+ res = double('response', :code => '500', :[] => ['content-encoding' => ''], :body => '' )
346
+ lambda { @request.process_result(res) }.should raise_error(RestClient::InternalServerError)
347
+ end
348
+ end
349
+
350
+ describe "block usage" do
351
+ it "returns what asked to" do
352
+ res = double('response', :code => '401', :[] => ['content-encoding' => ''], :body => '' )
353
+ @request.process_result(res){|response, request| "foo"}.should eq "foo"
354
+ end
355
+ end
356
+
357
+ describe "proxy" do
358
+ it "creates a proxy class if a proxy url is given" do
359
+ RestClient.stub(:proxy).and_return("http://example.com/")
360
+ @request.net_http_class.proxy_class?.should be_true
361
+ end
362
+
363
+ it "creates a non-proxy class if a proxy url is not given" do
364
+ @request.net_http_class.proxy_class?.should be_false
365
+ end
366
+ end
367
+
368
+
369
+ describe "logging" do
370
+ it "logs a get request" do
371
+ log = RestClient.log = []
372
+ RestClient::Request.new(:method => :get, :url => 'http://url').log_request
373
+ log[0].should eq %Q{RestClient.get "http://url", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate"\n}
374
+ end
375
+
376
+ it "logs a post request with a small payload" do
377
+ log = RestClient.log = []
378
+ RestClient::Request.new(:method => :post, :url => 'http://url', :payload => 'foo').log_request
379
+ log[0].should eq %Q{RestClient.post "http://url", "foo", "Accept"=>"*/*; q=0.5, application/xml", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"3"\n}
380
+ end
381
+
382
+ it "logs a post request with a large payload" do
383
+ log = RestClient.log = []
384
+ RestClient::Request.new(:method => :post, :url => 'http://url', :payload => ('x' * 1000)).log_request
385
+ 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}
386
+ end
387
+
388
+ it "logs input headers as a hash" do
389
+ log = RestClient.log = []
390
+ RestClient::Request.new(:method => :get, :url => 'http://url', :headers => { :accept => 'text/plain' }).log_request
391
+ log[0].should eq %Q{RestClient.get "http://url", "Accept"=>"text/plain", "Accept-Encoding"=>"gzip, deflate"\n}
392
+ end
393
+
394
+ it "logs a response including the status code, content type, and result body size in bytes" do
395
+ log = RestClient.log = []
396
+ res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
397
+ res.stub(:[]).with('Content-type').and_return('text/html')
398
+ @request.log_response res
399
+ log[0].should eq "# => 200 OK | text/html 4 bytes\n"
400
+ end
401
+
402
+ it "logs a response with a nil Content-type" do
403
+ log = RestClient.log = []
404
+ res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
405
+ res.stub(:[]).with('Content-type').and_return(nil)
406
+ @request.log_response res
407
+ log[0].should eq "# => 200 OK | 4 bytes\n"
408
+ end
409
+
410
+ it "logs a response with a nil body" do
411
+ log = RestClient.log = []
412
+ res = double('result', :code => '200', :class => Net::HTTPOK, :body => nil)
413
+ res.stub(:[]).with('Content-type').and_return('text/html; charset=utf-8')
414
+ @request.log_response res
415
+ log[0].should eq "# => 200 OK | text/html 0 bytes\n"
416
+ end
417
+
418
+ it 'does not log request password' do
419
+ log = RestClient.log = []
420
+ RestClient::Request.new(:method => :get, :url => 'http://user:password@url', :headers => {:user_agent => 'rest-client', :accept => '*/*'}).log_request
421
+ log[0].should eq %Q{RestClient.get "http://user:REDACTED@url", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client"\n}
422
+ end
423
+
424
+ it 'logs invalid URIs, even though they will fail elsewhere' do
425
+ log = RestClient.log = []
426
+ RestClient::Request.new(:method => :get, :url => 'http://a@b:c', :headers => {:user_agent => 'rest-client', :accept => '*/*'}).log_request
427
+ log[0].should eq %Q{RestClient.get "[invalid uri]", "Accept"=>"*/*", "Accept-Encoding"=>"gzip, deflate", "User-Agent"=>"rest-client"\n}
428
+ end
429
+ end
430
+
431
+ it "strips the charset from the response content type" do
432
+ log = RestClient.log = []
433
+ res = double('result', :code => '200', :class => Net::HTTPOK, :body => 'abcd')
434
+ res.stub(:[]).with('Content-type').and_return('text/html; charset=utf-8')
435
+ @request.log_response res
436
+ log[0].should eq "# => 200 OK | text/html 4 bytes\n"
437
+ end
438
+
439
+ describe "timeout" do
440
+ it "does not set timeouts if not specified" do
441
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload')
442
+ @http.stub(:request)
443
+ @request.stub(:process_result)
444
+ @request.stub(:response_log)
445
+
446
+ @net.should_not_receive(:read_timeout=)
447
+ @net.should_not_receive(:open_timeout=)
448
+
449
+ @request.transmit(@uri, 'req', nil)
450
+ end
451
+
452
+ it "set read_timeout" do
453
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123)
454
+ @http.stub(:request)
455
+ @request.stub(:process_result)
456
+ @request.stub(:response_log)
457
+
458
+ @net.should_receive(:read_timeout=).with(123)
459
+
460
+ @request.transmit(@uri, 'req', nil)
461
+ end
462
+
463
+ it "set open_timeout" do
464
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :open_timeout => 123)
465
+ @http.stub(:request)
466
+ @request.stub(:process_result)
467
+ @request.stub(:response_log)
468
+
469
+ @net.should_receive(:open_timeout=).with(123)
470
+
471
+ @request.transmit(@uri, 'req', nil)
472
+ end
473
+
474
+ it "disable timeout by setting it to nil" do
475
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => nil, :open_timeout => nil)
476
+ @http.stub(:request)
477
+ @request.stub(:process_result)
478
+ @request.stub(:response_log)
479
+
480
+ @net.should_receive(:read_timeout=).with(nil)
481
+ @net.should_receive(:open_timeout=).with(nil)
482
+
483
+ @request.transmit(@uri, 'req', nil)
484
+ end
485
+
486
+ it "deprecated: disable timeout by setting it to -1" do
487
+ @request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => -1, :open_timeout => -1)
488
+ @http.stub(:request)
489
+ @request.stub(:process_result)
490
+ @request.stub(:response_log)
491
+
492
+ @request.should_receive(:warn)
493
+ @net.should_receive(:read_timeout=).with(nil)
494
+
495
+ @request.should_receive(:warn)
496
+ @net.should_receive(:open_timeout=).with(nil)
497
+
498
+ @request.transmit(@uri, 'req', nil)
499
+ end
500
+ end
501
+
502
+ describe "ssl" do
503
+ it "uses SSL when the URI refers to a https address" do
504
+ @uri.stub(:is_a?).with(URI::HTTPS).and_return(true)
505
+ @net.should_receive(:use_ssl=).with(true)
506
+ @http.stub(:request)
507
+ @request.stub(:process_result)
508
+ @request.stub(:response_log)
509
+ @request.transmit(@uri, 'req', 'payload')
510
+ end
511
+
512
+ it "should default to verifying ssl certificates" do
513
+ @request.verify_ssl.should eq OpenSSL::SSL::VERIFY_PEER
514
+ end
515
+
516
+ it "should have expected values for VERIFY_PEER and VERIFY_NONE" do
517
+ OpenSSL::SSL::VERIFY_NONE.should eq(0)
518
+ OpenSSL::SSL::VERIFY_PEER.should eq(1)
519
+ end
520
+
521
+ it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do
522
+ @request = RestClient::Request.new(:method => :put, :verify_ssl => false, :url => 'http://some/resource', :payload => 'payload')
523
+ @net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
524
+ @http.stub(:request)
525
+ @request.stub(:process_result)
526
+ @request.stub(:response_log)
527
+ @request.transmit(@uri, 'req', 'payload')
528
+ end
529
+
530
+ it "should not set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is true" do
531
+ @request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true)
532
+ @net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
533
+ @http.stub(:request)
534
+ @request.stub(:process_result)
535
+ @request.stub(:response_log)
536
+ @request.transmit(@uri, 'req', 'payload')
537
+ end
538
+
539
+ it "should set net.verify_mode to OpenSSL::SSL::VERIFY_PEER if verify_ssl is true" do
540
+ @request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true)
541
+ @net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
542
+ @http.stub(:request)
543
+ @request.stub(:process_result)
544
+ @request.stub(:response_log)
545
+ @request.transmit(@uri, 'req', 'payload')
546
+ end
547
+
548
+ it "should set net.verify_mode to OpenSSL::SSL::VERIFY_PEER if verify_ssl is not given" do
549
+ @request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload')
550
+ @net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_PEER)
551
+ @http.stub(:request)
552
+ @request.stub(:process_result)
553
+ @request.stub(:response_log)
554
+ @request.transmit(@uri, 'req', 'payload')
555
+ end
556
+
557
+ it "should set net.verify_mode to the passed value if verify_ssl is an OpenSSL constant" do
558
+ mode = OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
559
+ @request = RestClient::Request.new( :method => :put,
560
+ :url => 'https://some/resource',
561
+ :payload => 'payload',
562
+ :verify_ssl => mode )
563
+ @net.should_receive(:verify_mode=).with(mode)
564
+ @http.stub(:request)
565
+ @request.stub(:process_result)
566
+ @request.stub(:response_log)
567
+ @request.transmit(@uri, 'req', 'payload')
568
+ end
569
+
570
+ it "should default to not having an ssl_client_cert" do
571
+ @request.ssl_client_cert.should be(nil)
572
+ end
573
+
574
+ it "should set the ssl_version if provided" do
575
+ @request = RestClient::Request.new(
576
+ :method => :put,
577
+ :url => 'https://some/resource',
578
+ :payload => 'payload',
579
+ :ssl_version => "TLSv1"
580
+ )
581
+ @net.should_receive(:ssl_version=).with("TLSv1")
582
+ @http.stub(:request)
583
+ @request.stub(:process_result)
584
+ @request.stub(:response_log)
585
+ @request.transmit(@uri, 'req', 'payload')
586
+ end
587
+
588
+ it "should not set the ssl_version if not provided" do
589
+ @request = RestClient::Request.new(
590
+ :method => :put,
591
+ :url => 'https://some/resource',
592
+ :payload => 'payload'
593
+ )
594
+ @net.should_not_receive(:ssl_version=).with("TLSv1")
595
+ @http.stub(:request)
596
+ @request.stub(:process_result)
597
+ @request.stub(:response_log)
598
+ @request.transmit(@uri, 'req', 'payload')
599
+ end
600
+
601
+ it "should set the ssl_ciphers if provided" do
602
+ ciphers = 'AESGCM:HIGH:!aNULL:!eNULL:RC4+RSA'
603
+ @request = RestClient::Request.new(
604
+ :method => :put,
605
+ :url => 'https://some/resource',
606
+ :payload => 'payload',
607
+ :ssl_ciphers => ciphers
608
+ )
609
+ @net.should_receive(:ciphers=).with(ciphers)
610
+ @http.stub(:request)
611
+ @request.stub(:process_result)
612
+ @request.stub(:response_log)
613
+ @request.transmit(@uri, 'req', 'payload')
614
+ end
615
+
616
+ it "should not set the ssl_ciphers if set to nil" do
617
+ @request = RestClient::Request.new(
618
+ :method => :put,
619
+ :url => 'https://some/resource',
620
+ :payload => 'payload',
621
+ :ssl_ciphers => nil,
622
+ )
623
+ @net.should_not_receive(:ciphers=)
624
+ @http.stub(:request)
625
+ @request.stub(:process_result)
626
+ @request.stub(:response_log)
627
+ @request.transmit(@uri, 'req', 'payload')
628
+ end
629
+
630
+ it "should override ssl_ciphers with better defaults with weak default ciphers" do
631
+ stub_const(
632
+ '::OpenSSL::SSL::SSLContext::DEFAULT_PARAMS',
633
+ {
634
+ :ssl_version=>"SSLv23",
635
+ :verify_mode=>1,
636
+ :ciphers=>"ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW",
637
+ :options=>-2147480577,
638
+ }
639
+ )
640
+
641
+ @request = RestClient::Request.new(
642
+ :method => :put,
643
+ :url => 'https://some/resource',
644
+ :payload => 'payload',
645
+ )
646
+
647
+ @net.should_receive(:ciphers=).with(RestClient::Request::DefaultCiphers)
648
+
649
+ @http.stub(:request)
650
+ @request.stub(:process_result)
651
+ @request.stub(:response_log)
652
+ @request.transmit(@uri, 'req', 'payload')
653
+ end
654
+
655
+ it "should not override ssl_ciphers with better defaults with different default ciphers" do
656
+ stub_const(
657
+ '::OpenSSL::SSL::SSLContext::DEFAULT_PARAMS',
658
+ {
659
+ :ssl_version=>"SSLv23",
660
+ :verify_mode=>1,
661
+ :ciphers=>"HIGH:!aNULL:!eNULL:!EXPORT:!LOW:!MEDIUM:!SSLv2",
662
+ :options=>-2147480577,
663
+ }
664
+ )
665
+
666
+ @request = RestClient::Request.new(
667
+ :method => :put,
668
+ :url => 'https://some/resource',
669
+ :payload => 'payload',
670
+ )
671
+
672
+ @net.should_not_receive(:ciphers=)
673
+
674
+ @http.stub(:request)
675
+ @request.stub(:process_result)
676
+ @request.stub(:response_log)
677
+ @request.transmit(@uri, 'req', 'payload')
678
+ end
679
+
680
+ it "should set the ssl_client_cert if provided" do
681
+ @request = RestClient::Request.new(
682
+ :method => :put,
683
+ :url => 'https://some/resource',
684
+ :payload => 'payload',
685
+ :ssl_client_cert => "whatsupdoc!"
686
+ )
687
+ @net.should_receive(:cert=).with("whatsupdoc!")
688
+ @http.stub(:request)
689
+ @request.stub(:process_result)
690
+ @request.stub(:response_log)
691
+ @request.transmit(@uri, 'req', 'payload')
692
+ end
693
+
694
+ it "should not set the ssl_client_cert if it is not provided" do
695
+ @request = RestClient::Request.new(
696
+ :method => :put,
697
+ :url => 'https://some/resource',
698
+ :payload => 'payload'
699
+ )
700
+ @net.should_not_receive(:cert=)
701
+ @http.stub(:request)
702
+ @request.stub(:process_result)
703
+ @request.stub(:response_log)
704
+ @request.transmit(@uri, 'req', 'payload')
705
+ end
706
+
707
+ it "should default to not having an ssl_client_key" do
708
+ @request.ssl_client_key.should be(nil)
709
+ end
710
+
711
+ it "should set the ssl_client_key if provided" do
712
+ @request = RestClient::Request.new(
713
+ :method => :put,
714
+ :url => 'https://some/resource',
715
+ :payload => 'payload',
716
+ :ssl_client_key => "whatsupdoc!"
717
+ )
718
+ @net.should_receive(:key=).with("whatsupdoc!")
719
+ @http.stub(:request)
720
+ @request.stub(:process_result)
721
+ @request.stub(:response_log)
722
+ @request.transmit(@uri, 'req', 'payload')
723
+ end
724
+
725
+ it "should not set the ssl_client_key if it is not provided" do
726
+ @request = RestClient::Request.new(
727
+ :method => :put,
728
+ :url => 'https://some/resource',
729
+ :payload => 'payload'
730
+ )
731
+ @net.should_not_receive(:key=)
732
+ @http.stub(:request)
733
+ @request.stub(:process_result)
734
+ @request.stub(:response_log)
735
+ @request.transmit(@uri, 'req', 'payload')
736
+ end
737
+
738
+ it "should default to not having an ssl_ca_file" do
739
+ @request.ssl_ca_file.should be(nil)
740
+ end
741
+
742
+ it "should set the ssl_ca_file if provided" do
743
+ @request = RestClient::Request.new(
744
+ :method => :put,
745
+ :url => 'https://some/resource',
746
+ :payload => 'payload',
747
+ :ssl_ca_file => "Certificate Authority File"
748
+ )
749
+ @net.should_receive(:ca_file=).with("Certificate Authority File")
750
+ @net.should_not_receive(:cert_store=)
751
+ @http.stub(:request)
752
+ @request.stub(:process_result)
753
+ @request.stub(:response_log)
754
+ @request.transmit(@uri, 'req', 'payload')
755
+ end
756
+
757
+ it "should not set the ssl_ca_file if it is not provided" do
758
+ @request = RestClient::Request.new(
759
+ :method => :put,
760
+ :url => 'https://some/resource',
761
+ :payload => 'payload'
762
+ )
763
+ @net.should_not_receive(:ca_file=)
764
+ @http.stub(:request)
765
+ @request.stub(:process_result)
766
+ @request.stub(:response_log)
767
+ @request.transmit(@uri, 'req', 'payload')
768
+ end
769
+
770
+ it "should default to not having an ssl_ca_path" do
771
+ @request.ssl_ca_path.should be(nil)
772
+ end
773
+
774
+ it "should set the ssl_ca_path if provided" do
775
+ @request = RestClient::Request.new(
776
+ :method => :put,
777
+ :url => 'https://some/resource',
778
+ :payload => 'payload',
779
+ :ssl_ca_path => "Certificate Authority Path"
780
+ )
781
+ @net.should_receive(:ca_path=).with("Certificate Authority Path")
782
+ @net.should_not_receive(:cert_store=)
783
+ @http.stub(:request)
784
+ @request.stub(:process_result)
785
+ @request.stub(:response_log)
786
+ @request.transmit(@uri, 'req', 'payload')
787
+ end
788
+
789
+ it "should not set the ssl_ca_path if it is not provided" do
790
+ @request = RestClient::Request.new(
791
+ :method => :put,
792
+ :url => 'https://some/resource',
793
+ :payload => 'payload'
794
+ )
795
+ @net.should_not_receive(:ca_path=)
796
+ @http.stub(:request)
797
+ @request.stub(:process_result)
798
+ @request.stub(:response_log)
799
+ @request.transmit(@uri, 'req', 'payload')
800
+ end
801
+
802
+ it "should set the ssl_cert_store if provided" do
803
+ store = OpenSSL::X509::Store.new
804
+ store.set_default_paths
805
+
806
+ @request = RestClient::Request.new(
807
+ :method => :put,
808
+ :url => 'https://some/resource',
809
+ :payload => 'payload',
810
+ :ssl_cert_store => store
811
+ )
812
+ @net.should_receive(:cert_store=).with(store)
813
+ @net.should_not_receive(:ca_path=)
814
+ @net.should_not_receive(:ca_file=)
815
+ @http.stub(:request)
816
+ @request.stub(:process_result)
817
+ @request.stub(:response_log)
818
+ @request.transmit(@uri, 'req', 'payload')
819
+ end
820
+
821
+ it "should by default set the ssl_cert_store if no CA info is provided" do
822
+ @request = RestClient::Request.new(
823
+ :method => :put,
824
+ :url => 'https://some/resource',
825
+ :payload => 'payload'
826
+ )
827
+ @net.should_receive(:cert_store=)
828
+ @net.should_not_receive(:ca_path=)
829
+ @net.should_not_receive(:ca_file=)
830
+ @http.stub(:request)
831
+ @request.stub(:process_result)
832
+ @request.stub(:response_log)
833
+ @request.transmit(@uri, 'req', 'payload')
834
+ end
835
+
836
+ it "should not set the ssl_cert_store if it is set falsy" do
837
+ @request = RestClient::Request.new(
838
+ :method => :put,
839
+ :url => 'https://some/resource',
840
+ :payload => 'payload',
841
+ :ssl_cert_store => nil,
842
+ )
843
+ @net.should_not_receive(:cert_store=)
844
+ @http.stub(:request)
845
+ @request.stub(:process_result)
846
+ @request.stub(:response_log)
847
+ @request.transmit(@uri, 'req', 'payload')
848
+ end
849
+
850
+ it "should not set the ssl_verify_callback by default" do
851
+ @request = RestClient::Request.new(
852
+ :method => :put,
853
+ :url => 'https://some/resource',
854
+ :payload => 'payload',
855
+ )
856
+ @net.should_not_receive(:verify_callback=)
857
+ @http.stub(:request)
858
+ @request.stub(:process_result)
859
+ @request.stub(:response_log)
860
+ @request.transmit(@uri, 'req', 'payload')
861
+ end
862
+
863
+ it "should set the ssl_verify_callback if passed" do
864
+ callback = lambda {}
865
+ @request = RestClient::Request.new(
866
+ :method => :put,
867
+ :url => 'https://some/resource',
868
+ :payload => 'payload',
869
+ :ssl_verify_callback => callback,
870
+ )
871
+ @net.should_receive(:verify_callback=).with(callback)
872
+
873
+ # we'll read cert_store on jruby
874
+ # https://github.com/jruby/jruby/issues/597
875
+ if RestClient::Platform.jruby?
876
+ allow(@net).to receive(:cert_store)
877
+ end
878
+
879
+ @http.stub(:request)
880
+ @request.stub(:process_result)
881
+ @request.stub(:response_log)
882
+ @request.transmit(@uri, 'req', 'payload')
883
+ end
884
+
885
+ # </ssl>
886
+ end
887
+
888
+ it "should still return a response object for 204 No Content responses" do
889
+ @request = RestClient::Request.new(
890
+ :method => :put,
891
+ :url => 'https://some/resource',
892
+ :payload => 'payload'
893
+ )
894
+ net_http_res = Net::HTTPNoContent.new("", "204", "No Content")
895
+ net_http_res.stub(:read_body).and_return(nil)
896
+ @http.should_receive(:request).and_return(@request.fetch_body(net_http_res))
897
+ response = @request.transmit(@uri, 'req', 'payload')
898
+ response.should_not be_nil
899
+ response.code.should eq 204
900
+ end
901
+
902
+ describe "raw response" do
903
+ it "should read the response into a binary-mode tempfile" do
904
+ @request = RestClient::Request.new(:method => "get", :url => "example.com", :raw_response => true)
905
+
906
+ tempfile = double("tempfile")
907
+ tempfile.should_receive(:binmode)
908
+ tempfile.stub(:open)
909
+ tempfile.stub(:close)
910
+ Tempfile.should_receive(:new).with("rest-client").and_return(tempfile)
911
+
912
+ net_http_res = Net::HTTPOK.new(nil, "200", "body")
913
+ net_http_res.stub(:read_body).and_return("body")
914
+ @request.fetch_body(net_http_res)
915
+ end
916
+ end
917
+ end