|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe Faraday::HTTPClient::SSLConfigurator do |
| 4 | + let(:client) { HTTPClient.new } |
| 5 | + let(:ssl) { {} } |
| 6 | + let(:configurator) { described_class.new(client, ssl) } |
| 7 | + |
| 8 | + describe '.configure' do |
| 9 | + it 'creates a new instance and configures it' do |
| 10 | + expect(described_class).to receive(:new).with(client, ssl).and_return(configurator) |
| 11 | + expect(configurator).to receive(:configure) |
| 12 | + described_class.configure(client, ssl) |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + describe '#configure' do |
| 17 | + let(:ssl_config) { client.ssl_config } |
| 18 | + |
| 19 | + context 'with default settings' do |
| 20 | + before { configurator.configure } |
| 21 | + |
| 22 | + it 'sets verify mode to VERIFY_PEER with fail if no peer cert' do |
| 23 | + expected_mode = OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT |
| 24 | + expect(ssl_config.verify_mode).to eq(expected_mode) |
| 25 | + end |
| 26 | + |
| 27 | + it 'sets a default cert store' do |
| 28 | + expect(ssl_config.cert_store).to be_a(OpenSSL::X509::Store) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + context 'with verify: false' do |
| 33 | + let(:ssl) { { verify: false } } |
| 34 | + |
| 35 | + it 'sets verify mode to VERIFY_NONE' do |
| 36 | + configurator.configure |
| 37 | + expect(ssl_config.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE) |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + context 'with explicit verify_mode' do |
| 42 | + let(:ssl) { { verify_mode: OpenSSL::SSL::VERIFY_NONE } } |
| 43 | + |
| 44 | + it 'uses the provided verify mode' do |
| 45 | + configurator.configure |
| 46 | + expect(ssl_config.verify_mode).to eq(OpenSSL::SSL::VERIFY_NONE) |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + context 'with custom cert store' do |
| 51 | + let(:cert_store) { OpenSSL::X509::Store.new } |
| 52 | + let(:ssl) { { cert_store: cert_store } } |
| 53 | + |
| 54 | + it 'uses the provided cert store' do |
| 55 | + configurator.configure |
| 56 | + expect(ssl_config.cert_store).to eq(cert_store) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + context 'with SSL options' do |
| 61 | + require 'tempfile' |
| 62 | + |
| 63 | + let(:client_cert) { OpenSSL::X509::Certificate.new } |
| 64 | + let(:client_key) { OpenSSL::PKey::RSA.new } |
| 65 | + let(:verify_depth) { 5 } |
| 66 | + let(:ca_file) do |
| 67 | + file = Tempfile.new(['ca', '.pem']) |
| 68 | + file.write('dummy CA content') |
| 69 | + file.close |
| 70 | + file.path |
| 71 | + end |
| 72 | + let(:ca_path) do |
| 73 | + Dir.mktmpdir('ca_certs') |
| 74 | + end |
| 75 | + let(:ssl) do |
| 76 | + { |
| 77 | + ca_file: ca_file, |
| 78 | + ca_path: ca_path, |
| 79 | + client_cert: client_cert, |
| 80 | + client_key: client_key, |
| 81 | + verify_depth: verify_depth |
| 82 | + } |
| 83 | + end |
| 84 | + |
| 85 | + before do |
| 86 | + allow(ssl_config).to receive(:add_trust_ca) |
| 87 | + configurator.configure |
| 88 | + end |
| 89 | + |
| 90 | + after do |
| 91 | + FileUtils.rm_f(ca_file) |
| 92 | + FileUtils.rm_rf(ca_path) |
| 93 | + end |
| 94 | + |
| 95 | + it 'configures all SSL options' do |
| 96 | + expect(ssl_config.cert_store).to be_a(OpenSSL::X509::Store) |
| 97 | + expect(ssl_config.client_cert).to eq(client_cert) |
| 98 | + expect(ssl_config.client_key).to eq(client_key) |
| 99 | + expect(ssl_config.verify_depth).to eq(verify_depth) |
| 100 | + end |
| 101 | + |
| 102 | + it 'adds trusted CA file and path' do |
| 103 | + expect(ssl_config).to have_received(:add_trust_ca).with(ca_file) |
| 104 | + expect(ssl_config).to have_received(:add_trust_ca).with(ca_path) |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + context 'with cipher configuration' do |
| 109 | + let(:ciphers) { ['TLS_AES_256_GCM_SHA384'] } |
| 110 | + let(:ssl) { { ciphers: ciphers } } |
| 111 | + |
| 112 | + before do |
| 113 | + stub_const('Faraday::VERSION', '2.11.0') |
| 114 | + configurator.configure |
| 115 | + end |
| 116 | + |
| 117 | + it 'configures ciphers when supported' do |
| 118 | + expect(ssl_config).to respond_to(:ciphers=) |
| 119 | + expect(ssl_config.ciphers).to eq(ciphers) |
| 120 | + end |
| 121 | + |
| 122 | + context 'with older Faraday version' do |
| 123 | + before do |
| 124 | + stub_const('Faraday::VERSION', '2.10.0') |
| 125 | + allow(ssl_config).to receive(:respond_to?).with(:ciphers=).and_return(false) |
| 126 | + configurator.configure |
| 127 | + end |
| 128 | + |
| 129 | + it 'does not configure ciphers' do |
| 130 | + expect(ssl_config).not_to receive(:ciphers=) |
| 131 | + end |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | +end |
0 commit comments