2015-03-13 21:48:17 -04:00
|
|
|
require_relative '_lib'
|
2010-05-13 12:56:56 -04:00
|
|
|
|
|
|
|
describe RestClient::Request do
|
2013-08-06 11:44:47 -04:00
|
|
|
before(:all) do
|
|
|
|
WebMock.disable!
|
|
|
|
end
|
|
|
|
|
|
|
|
after(:all) do
|
|
|
|
WebMock.enable!
|
|
|
|
end
|
|
|
|
|
2010-05-13 12:56:56 -04:00
|
|
|
describe "ssl verification" do
|
|
|
|
it "is successful with the correct ca_file" do
|
|
|
|
request = RestClient::Request.new(
|
|
|
|
:method => :get,
|
2014-02-11 16:10:10 -05:00
|
|
|
:url => 'https://www.mozilla.org',
|
|
|
|
:ssl_ca_file => File.join(File.dirname(__FILE__), "certs", "digicert.crt")
|
2010-05-13 12:56:56 -04:00
|
|
|
)
|
|
|
|
expect { request.execute }.to_not raise_error
|
|
|
|
end
|
|
|
|
|
2013-07-30 21:22:51 -04:00
|
|
|
it "is successful with the correct ca_path" do
|
|
|
|
request = RestClient::Request.new(
|
|
|
|
:method => :get,
|
2014-02-11 16:10:10 -05:00
|
|
|
:url => 'https://www.mozilla.org',
|
|
|
|
:ssl_ca_path => File.join(File.dirname(__FILE__), "capath_digicert")
|
2013-07-30 21:22:51 -04:00
|
|
|
)
|
|
|
|
expect { request.execute }.to_not raise_error
|
|
|
|
end
|
|
|
|
|
2014-04-02 04:30:50 -04:00
|
|
|
# TODO: deprecate and remove RestClient::SSLCertificateNotVerified and just
|
|
|
|
# pass through OpenSSL::SSL::SSLError directly. See note in
|
|
|
|
# lib/restclient/request.rb.
|
2014-04-03 15:40:21 -04:00
|
|
|
#
|
|
|
|
# On OS X, this test fails since Apple has patched OpenSSL to always fall
|
|
|
|
# back on the system CA store.
|
2014-11-02 20:45:01 -05:00
|
|
|
it "is unsuccessful with an incorrect ca_file", :unless => RestClient::Platform.mac_mri? do
|
2010-05-13 12:56:56 -04:00
|
|
|
request = RestClient::Request.new(
|
|
|
|
:method => :get,
|
2014-04-10 01:02:55 -04:00
|
|
|
:url => 'https://www.mozilla.org',
|
2011-08-15 05:01:43 -04:00
|
|
|
:ssl_ca_file => File.join(File.dirname(__FILE__), "certs", "verisign.crt")
|
2010-05-13 12:56:56 -04:00
|
|
|
)
|
|
|
|
expect { request.execute }.to raise_error(RestClient::SSLCertificateNotVerified)
|
|
|
|
end
|
2013-07-30 21:22:51 -04:00
|
|
|
|
2014-04-03 15:40:21 -04:00
|
|
|
# On OS X, this test fails since Apple has patched OpenSSL to always fall
|
|
|
|
# back on the system CA store.
|
2014-11-02 20:45:01 -05:00
|
|
|
it "is unsuccessful with an incorrect ca_path", :unless => RestClient::Platform.mac_mri? do
|
2013-07-30 21:22:51 -04:00
|
|
|
request = RestClient::Request.new(
|
|
|
|
:method => :get,
|
2014-04-10 01:02:55 -04:00
|
|
|
:url => 'https://www.mozilla.org',
|
2013-07-30 21:22:51 -04:00
|
|
|
:ssl_ca_path => File.join(File.dirname(__FILE__), "capath_verisign")
|
|
|
|
)
|
|
|
|
expect { request.execute }.to raise_error(RestClient::SSLCertificateNotVerified)
|
|
|
|
end
|
2014-03-31 23:50:27 -04:00
|
|
|
|
|
|
|
it "is successful using the default system cert store" do
|
|
|
|
request = RestClient::Request.new(
|
|
|
|
:method => :get,
|
|
|
|
:url => 'https://www.mozilla.org',
|
|
|
|
:verify_ssl => true,
|
|
|
|
)
|
|
|
|
expect {request.execute }.to_not raise_error
|
|
|
|
end
|
2014-04-09 23:59:56 -04:00
|
|
|
|
2014-04-25 20:32:10 -04:00
|
|
|
it "executes the verify_callback" do
|
2014-04-09 23:59:56 -04:00
|
|
|
ran_callback = false
|
|
|
|
request = RestClient::Request.new(
|
|
|
|
:method => :get,
|
|
|
|
:url => 'https://www.mozilla.org',
|
|
|
|
:verify_ssl => true,
|
|
|
|
:ssl_verify_callback => lambda { |preverify_ok, store_ctx|
|
|
|
|
ran_callback = true
|
|
|
|
preverify_ok
|
|
|
|
},
|
|
|
|
)
|
|
|
|
expect {request.execute }.to_not raise_error
|
2016-06-05 19:52:16 -04:00
|
|
|
expect(ran_callback).to eq(true)
|
2014-04-09 23:59:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails verification when the callback returns false",
|
2014-11-02 20:45:01 -05:00
|
|
|
:unless => RestClient::Platform.mac_mri? do
|
2014-04-09 23:59:56 -04:00
|
|
|
request = RestClient::Request.new(
|
|
|
|
:method => :get,
|
2014-04-10 01:02:55 -04:00
|
|
|
:url => 'https://www.mozilla.org',
|
2014-04-09 23:59:56 -04:00
|
|
|
:verify_ssl => true,
|
|
|
|
:ssl_verify_callback => lambda { |preverify_ok, store_ctx| false },
|
|
|
|
)
|
|
|
|
expect { request.execute }.to raise_error(RestClient::SSLCertificateNotVerified)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "succeeds verification when the callback returns true",
|
2014-11-02 20:45:01 -05:00
|
|
|
:unless => RestClient::Platform.mac_mri? do
|
2014-04-09 23:59:56 -04:00
|
|
|
request = RestClient::Request.new(
|
|
|
|
:method => :get,
|
2014-04-10 01:02:55 -04:00
|
|
|
:url => 'https://www.mozilla.org',
|
2014-04-09 23:59:56 -04:00
|
|
|
:verify_ssl => true,
|
|
|
|
:ssl_ca_file => File.join(File.dirname(__FILE__), "certs", "verisign.crt"),
|
|
|
|
:ssl_verify_callback => lambda { |preverify_ok, store_ctx| true },
|
|
|
|
)
|
|
|
|
expect { request.execute }.to_not raise_error
|
|
|
|
end
|
2010-05-13 12:56:56 -04:00
|
|
|
end
|
2014-12-05 17:36:46 -05:00
|
|
|
|
|
|
|
describe "timeouts" do
|
2015-03-14 18:18:36 -04:00
|
|
|
it "raises OpenTimeout when it hits an open timeout" do
|
2014-12-05 17:36:46 -05:00
|
|
|
request = RestClient::Request.new(
|
|
|
|
:method => :get,
|
|
|
|
:url => 'http://www.mozilla.org',
|
|
|
|
:open_timeout => 1e-10,
|
|
|
|
)
|
|
|
|
expect { request.execute }.to(
|
|
|
|
raise_error(RestClient::Exceptions::OpenTimeout))
|
|
|
|
end
|
|
|
|
|
2015-03-14 18:18:36 -04:00
|
|
|
it "raises ReadTimeout when it hits a read timeout via :read_timeout" do
|
2014-12-05 17:36:46 -05:00
|
|
|
request = RestClient::Request.new(
|
|
|
|
:method => :get,
|
|
|
|
:url => 'https://www.mozilla.org',
|
2015-03-14 18:18:36 -04:00
|
|
|
:read_timeout => 1e-10,
|
2014-12-05 17:36:46 -05:00
|
|
|
)
|
|
|
|
expect { request.execute }.to(
|
|
|
|
raise_error(RestClient::Exceptions::ReadTimeout))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-13 12:56:56 -04:00
|
|
|
end
|