mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
parent
39a0034c3e
commit
ae88da404c
2 changed files with 33 additions and 41 deletions
|
@ -24,7 +24,7 @@ module RestClient
|
|||
# * :timeout and :open_timeout are how long to wait for a response and to
|
||||
# open a connection, in seconds. Pass nil to disable the timeout.
|
||||
# * :ssl_client_cert, :ssl_client_key, :ssl_ca_file, :ssl_ca_path
|
||||
# * :ssl_version specifies the SSL version for the underlying Net::HTTP connection (defaults to 'SSLv3')
|
||||
# * :ssl_version specifies the SSL version for the underlying Net::HTTP connection
|
||||
class Request
|
||||
|
||||
attr_reader :method, :url, :headers, :cookies,
|
||||
|
@ -62,7 +62,7 @@ module RestClient
|
|||
@ssl_client_key = args[:ssl_client_key] || nil
|
||||
@ssl_ca_file = args[:ssl_ca_file] || nil
|
||||
@ssl_ca_path = args[:ssl_ca_path] || nil
|
||||
@ssl_version = args[:ssl_version] || 'SSLv3'
|
||||
@ssl_version = args[:ssl_version]
|
||||
@tf = nil # If you are a raw request, this is your tempfile
|
||||
@max_redirects = args[:max_redirects] || 10
|
||||
@processed_headers = make_headers headers
|
||||
|
@ -187,7 +187,7 @@ module RestClient
|
|||
|
||||
net = net_http_class.new(uri.host, uri.port)
|
||||
net.use_ssl = uri.is_a?(URI::HTTPS)
|
||||
net.ssl_version = @ssl_version
|
||||
net.ssl_version = @ssl_version if @ssl_version
|
||||
err_msg = nil
|
||||
if (@verify_ssl == false) || (@verify_ssl == OpenSSL::SSL::VERIFY_NONE)
|
||||
net.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
||||
|
|
|
@ -237,7 +237,6 @@ describe RestClient::Request do
|
|||
|
||||
it "transmits the request with Net::HTTP" do
|
||||
@http.should_receive(:request).with('req', 'payload')
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
@request.should_receive(:process_result)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
end
|
||||
|
@ -245,7 +244,6 @@ describe RestClient::Request do
|
|||
describe "payload" do
|
||||
it "sends nil payloads" do
|
||||
@http.should_receive(:request).with('req', nil)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
@request.should_receive(:process_result)
|
||||
@request.stub(:response_log)
|
||||
@request.transmit(@uri, 'req', nil)
|
||||
|
@ -275,7 +273,6 @@ describe RestClient::Request do
|
|||
describe "credentials" do
|
||||
it "sets up the credentials prior to the request" do
|
||||
@http.stub(:request)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
@ -305,25 +302,21 @@ describe RestClient::Request do
|
|||
|
||||
it "catches EOFError and shows the more informative ServerBrokeConnection" do
|
||||
@http.stub(:request).and_raise(EOFError)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::ServerBrokeConnection)
|
||||
end
|
||||
|
||||
it "catches OpenSSL::SSL::SSLError and raise it back without more informative message" do
|
||||
@http.stub(:request).and_raise(OpenSSL::SSL::SSLError)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(OpenSSL::SSL::SSLError)
|
||||
end
|
||||
|
||||
it "catches Timeout::Error and raise the more informative RequestTimeout" do
|
||||
@http.stub(:request).and_raise(Timeout::Error)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::RequestTimeout)
|
||||
end
|
||||
|
||||
it "catches Timeout::Error and raise the more informative RequestTimeout" do
|
||||
@http.stub(:request).and_raise(Errno::ETIMEDOUT)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
lambda { @request.transmit(@uri, 'req', nil) }.should raise_error(RestClient::RequestTimeout)
|
||||
end
|
||||
|
||||
|
@ -437,31 +430,28 @@ describe RestClient::Request do
|
|||
|
||||
@net.should_not_receive(:read_timeout=)
|
||||
@net.should_not_receive(:open_timeout=)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
|
||||
@request.transmit(@uri, 'req', nil)
|
||||
end
|
||||
|
||||
it "set read_timeout" do
|
||||
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123, :ssl_version => 'SSLv3')
|
||||
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :timeout => 123)
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
||||
@net.should_receive(:read_timeout=).with(123)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
|
||||
@request.transmit(@uri, 'req', nil)
|
||||
end
|
||||
|
||||
it "set open_timeout" do
|
||||
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :open_timeout => 123, :ssl_version => 'SSLv3')
|
||||
@request = RestClient::Request.new(:method => :put, :url => 'http://some/resource', :payload => 'payload', :open_timeout => 123)
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
||||
@net.should_receive(:open_timeout=).with(123)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
|
||||
@request.transmit(@uri, 'req', nil)
|
||||
end
|
||||
|
@ -474,7 +464,6 @@ describe RestClient::Request do
|
|||
|
||||
@net.should_receive(:read_timeout=).with(nil)
|
||||
@net.should_receive(:open_timeout=).with(nil)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
|
||||
@request.transmit(@uri, 'req', nil)
|
||||
end
|
||||
|
@ -490,7 +479,6 @@ describe RestClient::Request do
|
|||
|
||||
@request.should_receive(:warn)
|
||||
@net.should_receive(:open_timeout=).with(nil)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
|
||||
@request.transmit(@uri, 'req', nil)
|
||||
end
|
||||
|
@ -500,7 +488,6 @@ describe RestClient::Request do
|
|||
it "uses SSL when the URI refers to a https address" do
|
||||
@uri.stub(:is_a?).with(URI::HTTPS).and_return(true)
|
||||
@net.should_receive(:use_ssl=).with(true)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
@ -513,7 +500,6 @@ describe RestClient::Request do
|
|||
|
||||
it "should set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is false" do
|
||||
@net.should_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
@ -521,9 +507,8 @@ describe RestClient::Request do
|
|||
end
|
||||
|
||||
it "should not set net.verify_mode to OpenSSL::SSL::VERIFY_NONE if verify_ssl is true" do
|
||||
@request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true, :ssl_version => 'SSLv3')
|
||||
@request = RestClient::Request.new(:method => :put, :url => 'https://some/resource', :payload => 'payload', :verify_ssl => true)
|
||||
@net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
@ -535,11 +520,9 @@ describe RestClient::Request do
|
|||
@request = RestClient::Request.new( :method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload',
|
||||
:ssl_version => 'SSLv3',
|
||||
:verify_ssl => mode )
|
||||
@net.should_receive(:verify_mode=).with(mode)
|
||||
@net.should_receive(:verify_callback=)
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
@ -550,16 +533,41 @@ describe RestClient::Request do
|
|||
@request.ssl_client_cert.should be(nil)
|
||||
end
|
||||
|
||||
it "should set the ssl_version if provided" do
|
||||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload',
|
||||
:ssl_version => "TLSv1"
|
||||
)
|
||||
@net.should_receive(:ssl_version=).with("TLSv1")
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
end
|
||||
|
||||
it "should not set the ssl_version if not provided" do
|
||||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload'
|
||||
)
|
||||
@net.should_not_receive(:ssl_version=).with("TLSv1")
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
@request.transmit(@uri, 'req', 'payload')
|
||||
end
|
||||
|
||||
it "should set the ssl_client_cert if provided" do
|
||||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload',
|
||||
:ssl_version => 'SSLv3',
|
||||
:ssl_client_cert => "whatsupdoc!"
|
||||
)
|
||||
@net.should_receive(:cert=).with("whatsupdoc!")
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
@ -570,11 +578,9 @@ describe RestClient::Request do
|
|||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:ssl_version => 'SSLv3',
|
||||
:payload => 'payload'
|
||||
)
|
||||
@net.should_not_receive(:cert=).with("whatsupdoc!")
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
@ -590,11 +596,9 @@ describe RestClient::Request do
|
|||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload',
|
||||
:ssl_version => 'SSLv3',
|
||||
:ssl_client_key => "whatsupdoc!"
|
||||
)
|
||||
@net.should_receive(:key=).with("whatsupdoc!")
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
@ -605,11 +609,9 @@ describe RestClient::Request do
|
|||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:ssl_version => 'SSLv3',
|
||||
:payload => 'payload'
|
||||
)
|
||||
@net.should_not_receive(:key=).with("whatsupdoc!")
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
@ -625,11 +627,9 @@ describe RestClient::Request do
|
|||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload',
|
||||
:ssl_version => 'SSLv3',
|
||||
:ssl_ca_file => "Certificate Authority File"
|
||||
)
|
||||
@net.should_receive(:ca_file=).with("Certificate Authority File")
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
@ -640,11 +640,9 @@ describe RestClient::Request do
|
|||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:ssl_version => 'TSLv1',
|
||||
:payload => 'payload'
|
||||
)
|
||||
@net.should_not_receive(:ca_file=).with("Certificate Authority File")
|
||||
@net.should_receive(:ssl_version=).with('TSLv1')
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
@ -660,11 +658,9 @@ describe RestClient::Request do
|
|||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:payload => 'payload',
|
||||
:ssl_version => 'SSLv3',
|
||||
:ssl_ca_path => "Certificate Authority Path"
|
||||
)
|
||||
@net.should_receive(:ca_path=).with("Certificate Authority Path")
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
@ -675,11 +671,9 @@ describe RestClient::Request do
|
|||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:ssl_version => 'TSLv1',
|
||||
:payload => 'payload'
|
||||
)
|
||||
@net.should_not_receive(:ca_path=).with("Certificate Authority File")
|
||||
@net.should_receive(:ssl_version=).with('TSLv1')
|
||||
@http.stub(:request)
|
||||
@request.stub(:process_result)
|
||||
@request.stub(:response_log)
|
||||
|
@ -691,13 +685,11 @@ describe RestClient::Request do
|
|||
@request = RestClient::Request.new(
|
||||
:method => :put,
|
||||
:url => 'https://some/resource',
|
||||
:ssl_version => 'SSLv3',
|
||||
:payload => 'payload'
|
||||
)
|
||||
net_http_res = Net::HTTPNoContent.new("", "204", "No Content")
|
||||
net_http_res.stub(:read_body).and_return(nil)
|
||||
@http.should_receive(:request).and_return(@request.fetch_body(net_http_res))
|
||||
@net.should_receive(:ssl_version=).with('SSLv3')
|
||||
response = @request.transmit(@uri, 'req', 'payload')
|
||||
response.should_not be_nil
|
||||
response.code.should eq 204
|
||||
|
|
Loading…
Add table
Reference in a new issue