From fb33aeabe43726b8c7dbafb5a8b1cfe79b5e95e8 Mon Sep 17 00:00:00 2001 From: Chris Lowis Date: Thu, 19 Nov 2009 23:03:59 +0000 Subject: [PATCH] Pass contents of PEM file rather than file location --- lib/httparty.rb | 10 ++++++++++ lib/httparty/request.rb | 6 ++++++ spec/httparty/request_spec.rb | 12 ++++++++++++ 3 files changed, 28 insertions(+) diff --git a/lib/httparty.rb b/lib/httparty.rb index 4aa8d3c..de271c8 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -110,6 +110,16 @@ module HTTParty default_options[:format] = f end + # Allows setting a PEM file to be used + # + # class Foo + # include HTTParty + # pem_file File.read('/home/user/my.pem') + # end + def pem_file(pem_file_contents) + default_options[:pem_file] = pem_file_contents + end + # Allows setting a custom parser for the response. # # class Foo diff --git a/lib/httparty/request.rb b/lib/httparty/request.rb index 61bfda3..6f1c5c2 100644 --- a/lib/httparty/request.rb +++ b/lib/httparty/request.rb @@ -56,6 +56,12 @@ module HTTParty http.open_timeout = options[:timeout] http.read_timeout = options[:timeout] end + + if options[:pem_file] + http.cert = OpenSSL::X509::Certificate.new(options[:pem_file]) + http.key = OpenSSL::PKey::RSA.new(options[:pem_file]) + end + http end diff --git a/spec/httparty/request_spec.rb b/spec/httparty/request_spec.rb index 518119c..3387360 100644 --- a/spec/httparty/request_spec.rb +++ b/spec/httparty/request_spec.rb @@ -46,6 +46,18 @@ describe HTTParty::Request do request.send(:http).use_ssl?.should == true end + it "should use a PEM certificate when provided" do + http = mock("http", :null_object => true) + http.should_receive(:cert=) + http.should_receive(:key=) + Net::HTTP.stub(:new => http) + pem_file = :contents_of_pem_file + OpenSSL::X509::Certificate.stub!(:new).with(pem_file) + OpenSSL::PKey::RSA.stub!(:new).with(pem_file) + @request.options[:pem_file] = pem_file + @request.perform + end + it "should use basic auth when configured" do @request.options[:basic_auth] = {:username => 'foobar', :password => 'secret'} @request.send(:setup_raw_request)