Skip to content

Commit

Permalink
Adding a switch to control ssl verification
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhjk committed Mar 16, 2009
1 parent 3b01ab4 commit 7be2bea
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/restclient/request.rb
Expand Up @@ -6,7 +6,7 @@ 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
attr_reader :method, :url, :payload, :headers, :cookies, :user, :password, :timeout, :open_timeout, :verify_ssl

def self.execute(args)
new(args).execute
Expand All @@ -22,6 +22,7 @@ def initialize(args)
@password = args[:password]
@timeout = args[:timeout]
@open_timeout = args[:open_timeout]
@verify_ssl = args[:verify_ssl] || false
end

def execute
Expand Down Expand Up @@ -94,7 +95,7 @@ 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
net.verify_mode = OpenSSL::SSL::VERIFY_NONE if @verify_ssl == false
net.read_timeout = @timeout if @timeout
net.open_timeout = @open_timeout if @open_timeout

Expand Down
21 changes: 21 additions & 0 deletions spec/request_spec.rb
Expand Up @@ -325,4 +325,25 @@

@request.transmit(@uri, 'req', nil)
end

it "should default to not verifying ssl certificates" do
@request.verify_ssl.should == false
end

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)
@http.stub!(:request)
@request.stub!(:process_result)
@request.stub!(:response_log)
@request.transmit(@uri, 'req', 'payload')
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)
@net.should_not_receive(:verify_mode=).with(OpenSSL::SSL::VERIFY_NONE)
@http.stub!(:request)
@request.stub!(:process_result)
@request.stub!(:response_log)
@request.transmit(@uri, 'req', 'payload')
end
end

0 comments on commit 7be2bea

Please sign in to comment.