From 0e0e9c58cee3d467b4223af1765c21ef3cdb3365 Mon Sep 17 00:00:00 2001 From: Si Kai Date: Tue, 14 Oct 2025 11:40:59 +0800 Subject: [PATCH] Expose verify_callback option from Net::HTTP --- lib/httparty/connection_adapter.rb | 5 +++++ spec/httparty/connection_adapter_spec.rb | 27 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/httparty/connection_adapter.rb b/lib/httparty/connection_adapter.rb index 262016fb..00cee904 100644 --- a/lib/httparty/connection_adapter.rb +++ b/lib/httparty/connection_adapter.rb @@ -50,6 +50,7 @@ module HTTParty # * :+pem+: contains pem client certificate data. see method 'attach_ssl_certificates' # * :+p12+: contains PKCS12 client client certificate data. see method 'attach_ssl_certificates' # * :+verify+: verify the server’s certificate against the ca certificate. + # * :+verify_callback+: a Proc that will be called to verify the server’s certificate. see method 'attach_ssl_certificates' # * :+verify_peer+: set to false to turn off server verification but still send client certificate # * :+ssl_ca_file+: see HTTParty::ClassMethods.ssl_ca_file. # * :+ssl_ca_path+: see HTTParty::ClassMethods.ssl_ca_path. @@ -227,6 +228,10 @@ def attach_ssl_certificates(http, options) http.verify_mode = OpenSSL::SSL::VERIFY_PEER end + if options[:verify_callback] + http.verify_callback = options[:verify_callback] + end + # This is only Ruby 1.9+ if options[:ssl_version] && http.respond_to?(:ssl_version=) http.ssl_version = options[:ssl_version] diff --git a/spec/httparty/connection_adapter_spec.rb b/spec/httparty/connection_adapter_spec.rb index de386f87..3e0ad41b 100644 --- a/spec/httparty/connection_adapter_spec.rb +++ b/spec/httparty/connection_adapter_spec.rb @@ -661,6 +661,33 @@ it { expect(subject.port).to be 443 } end end + + context "when providing verify_callback" do + let(:verify_callback) { double("verify_callback") } + let(:options) { {verify_callback: verify_callback} } + + context "when scheme is https" do + let(:uri) { URI 'https://google.com' } + + it "uses the provided verify_callback" do + expect(subject.verify_callback).to be verify_callback + end + end + + context "when scheme is not https" do + let(:uri) { URI 'http://google.com' } + let(:http) { Net::HTTP.new(uri) } + + before do + allow(Net::HTTP).to receive_messages(new: http) + expect(http).not_to receive(:verify_callback=) + end + + it "has no verify_callback " do + expect(subject.verify_callback).to be_nil + end + end + end end end end