require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") describe Cardflex::Http do original_env = Cardflex::Configuration.environment describe 'logging' do it 'should log the start of a request' do begin old_logger = Cardflex::Configuration.logger the_time = Time.utc(2014, 1, 1) SpecHelper.stub_now(the_time) do new_output = StringIO.new Cardflex::Configuration.logger = Logger.new(new_output) Cardflex::Configuration.logger.level = Logger::DEBUG Cardflex::Configuration.instantiate.http._do_http(Net::HTTP::Get, '/') expect(new_output.string).to include("[Cardflex] [01/Jan/2014 00:00:00 UTC] GET") expect(new_output.string).to include("[Cardflex] [01/Jan/2014 00:00:00 UTC] GET 200") end ensure Cardflex::Configuration.logger = old_logger end end it 'should log an ssl error' do begin old_logger = Cardflex::Configuration.logger the_time = Time.utc(2014, 1, 1) context = OpenSSL::X509::StoreContext.new(OpenSSL::X509::Store.new) SpecHelper.stub_now(the_time) do new_output = StringIO.new Cardflex::Configuration.logger = Logger.new(new_output) Cardflex::Configuration.logger.level = Logger::DEBUG Cardflex::Configuration.instantiate.http._verify_ssl_certificate(0, context) expect(new_output.string).to include("SSL Verification failed --") end ensure Cardflex::Configuration.logger = old_logger end end end describe 'invalid certificate rejection' do before do @config = Cardflex::Configuration.instantiate @fake_certs_dir = File.expand_path(File.dirname(__FILE__) + '/../../ssl/') end it 'should reject a self-signed certificate' do expect(@config).to receive(:ca_file).and_return(@fake_certs_dir + 'certificate.crt') expect do @config.http._do_http(Net::HTTP::Get, '/') end.to raise_error Cardflex::SSLCertificateError end end describe 'certificate verification' do it 'should verify the ceritificate' do begin Cardflex::Configuration.environment = :test expect do Cardflex::Configuration.instantiate.http._do_http(Net::HTTP::Get, '/') end.to_not raise_error ensure Cardflex::Configuration.environment = original_env end end end end