diff --git a/lib/restclient/request.rb b/lib/restclient/request.rb index 38ff2da9..84370142 100644 --- a/lib/restclient/request.rb +++ b/lib/restclient/request.rb @@ -6,8 +6,9 @@ module RestClient # RestClient::Request.execute(:method => :head, :url => 'http://example.com') # class Request - attr_reader :method, :url, :payload, :headers, :cookies, :user, :password, :timeout, :open_timeout, :verify_ssl - + attr_reader :method, :url, :payload, :headers, :cookies, :user, :password, :timeout, :open_timeout, + :verify_ssl, :ssl_client_cert, :ssl_client_key + def self.execute(args) new(args).execute end @@ -23,6 +24,8 @@ def initialize(args) @timeout = args[:timeout] @open_timeout = args[:open_timeout] @verify_ssl = args[:verify_ssl] || false + @ssl_client_cert = args[:ssl_client_cert] || nil + @ssl_client_key = args[:ssl_client_key] || nil end def execute @@ -96,6 +99,8 @@ def transmit(uri, req, payload) net = net_http_class.new(uri.host, uri.port) net.use_ssl = uri.is_a?(URI::HTTPS) net.verify_mode = OpenSSL::SSL::VERIFY_NONE if @verify_ssl == false + net.cert = @ssl_client_cert if @ssl_client_cert + net.key = @ssl_client_key if @ssl_client_key net.read_timeout = @timeout if @timeout net.open_timeout = @open_timeout if @open_timeout diff --git a/spec/request_spec.rb b/spec/request_spec.rb index 94fa87d0..bbcb81f5 100644 --- a/spec/request_spec.rb +++ b/spec/request_spec.rb @@ -346,4 +346,66 @@ @request.stub!(:response_log) @request.transmit(@uri, 'req', 'payload') end + + it "should default to not having an ssl_client_cert" do + @request.ssl_client_cert.should be(nil) + end + + it "should set the ssl_client_cert if provided" do + @request = RestClient::Request.new( + :method => :put, + :url => 'https://some/resource', + :payload => 'payload', + :ssl_client_cert => "whatsupdoc!" + ) + @net.should_receive(:cert=).with("whatsupdoc!") + @http.stub!(:request) + @request.stub!(:process_result) + @request.stub!(:response_log) + @request.transmit(@uri, 'req', 'payload') + end + + it "should not set the ssl_client_cert if it is not provided" do + @request = RestClient::Request.new( + :method => :put, + :url => 'https://some/resource', + :payload => 'payload' + ) + @net.should_not_receive(:cert=).with("whatsupdoc!") + @http.stub!(:request) + @request.stub!(:process_result) + @request.stub!(:response_log) + @request.transmit(@uri, 'req', 'payload') + end + + it "should default to not having an ssl_client_key" do + @request.ssl_client_key.should be(nil) + end + + it "should set the ssl_client_key if provided" do + @request = RestClient::Request.new( + :method => :put, + :url => 'https://some/resource', + :payload => 'payload', + :ssl_client_key => "whatsupdoc!" + ) + @net.should_receive(:key=).with("whatsupdoc!") + @http.stub!(:request) + @request.stub!(:process_result) + @request.stub!(:response_log) + @request.transmit(@uri, 'req', 'payload') + end + + it "should not set the ssl_client_key if it is not provided" do + @request = RestClient::Request.new( + :method => :put, + :url => 'https://some/resource', + :payload => 'payload' + ) + @net.should_not_receive(:key=).with("whatsupdoc!") + @http.stub!(:request) + @request.stub!(:process_result) + @request.stub!(:response_log) + @request.transmit(@uri, 'req', 'payload') + end end