require 'spec_helper' describe MinatoRubyApiClient::Configuration do let(:url) { 'https://example.com/v1' } before :each do require 'uri' uri = URI.parse(url) MinatoRubyApiClient.configure do |c| c.host = uri.host c.base_path = uri.path c.api_key['api_key'] = 'test_key' end @config = described_class.default end describe '#scheme' do it 'should have the default value https' do expect(@config.scheme).to eq('https') end it 'should remove :// from scheme' do @config.scheme = 'https://' expect(@config.scheme).to eq('https') end end describe '#host' do it 'should do not have the scheme value' do expect(@config.host).not_to match(/https/) end end describe '#base_path' do it 'should add leading slashes if missing' do @config.base_path = 'v1' expect(@config.base_path).to eq('/v1') end it 'should be empty if is /' do @config.base_path = '/' expect(@config.base_path).to eq('') end end describe '#user_agent' do it 'should have user agent' do @config.user_agent = 'Minato' expect(@config.user_agent).to eq('Minato') end end describe '#base_url' do it 'should have the default value' do expect(@config.base_url).to eq(url) end it 'should remove trailing slashes' do [nil, '', '/', '//'].each do |base_path| @config.base_path = base_path expect(@config.base_url).to eq("https://example.com") end end end describe '#timeout' do it 'should have the default value' do expect(@config.timeout).to eq(60) end end describe '#debugging' do it 'should have the default value falsy' do expect(@config.debugging).to be_falsy end context 'when MINATO_RUBY_API_CLIENT_DEBUG is true' do it 'should be truthy' do allow(ENV).to receive(:[]).with('MINATO_RUBY_API_CLIENT_DEBUG').and_return('true') expect(@config.debugging).to be_truthy end end context 'when debug is active' do it 'should be truthy' do config = described_class.new config.debugging = true expect(config.debugging).to be_truthy end end end describe '#auth_settings' do it 'should have default auth types' do expect(@config.auth_settings.keys).to eq(%w[bearer_auth basic_auth apikey]) end end describe '#basic_auth_token' do let(:username) { 'test' } let(:password) { 'test' } let(:token) { ["#{username}:#{password}"].pack('m').delete("\r\n") } it 'should return basic auth string' do @config.username = username @config.password = password expect(@config.basic_auth_token).to eq("Basic #{token}") end end describe '#logger' do let(:logger) { Logger.new(STDOUT) } context 'when Rails is defined' do it 'should return Rails.logger' do allow(Object).to receive(:const_defined?).with(:Rails).and_return(true) allow(Rails).to receive(:logger).and_return(logger) config = described_class.new expect(config.logger).to eq(Rails.logger) end end context 'when Rails is not defined' do it 'should return Logger instance' do allow(Object).to receive(:const_defined?).with(:Rails).and_return(nil) config = described_class.new expect(config.logger).to be_instance_of(Logger) end end end describe '#api_key_with_prefix' do context 'when api_key_prefix is not defined' do it 'return the key without prefix' do expect(@config.api_key_with_prefix('api_key')).to eq('test_key') end end context 'when api_key_prefix is defined' do it 'return the key with prefix' do @config.api_key_prefix['api_key'] = 'Prefix' expect(@config.api_key_with_prefix('api_key')).to eq('Prefix test_key') end end end describe '#configure' do context 'when block is given' do it 'should yield the configuration object' do expect { |b| @config.configure(&b) }.to yield_with_args end end context 'when block is not given' do it 'should return nil' do expect(@config.configure).to be_nil end end end describe '#use' do it 'add middleware to the middlewares list' do @config.use(:test) expect(@config.instance_variable_get('@middlewares')).to include([:test]) end end describe '#request' do it 'add middleware to the request middlewares list' do @config.request(:test) expect(@config.instance_variable_get('@request_middlewares')).to include([:test]) end end describe '#response' do it 'add middleware to the response middlewares list' do @config.response(:test) expect(@config.instance_variable_get('@response_middlewares')).to include([:test]) end end describe '#configure_middleware' do let(:conn) { double('conn') } let(:config) { described_class.new } before do config.use(:use) config.request(:request) config.response(:response) allow(conn).to receive(:use) allow(conn).to receive(:request) allow(conn).to receive(:response) config.configure_middleware(conn) end it 'add middlewares to connection middleware stack' do expect(conn).to have_received(:use).once.with(:use) expect(conn).to have_received(:request).once.with(:request) expect(conn).to have_received(:response).once.with(:response) end end end