Skip to content

Commit

Permalink
Added SSL client certificate support
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhjk committed Mar 16, 2009
1 parent 7be2bea commit c72955b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/restclient/request.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
62 changes: 62 additions & 0 deletions spec/request_spec.rb
Expand Up @@ -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

0 comments on commit c72955b

Please sign in to comment.