1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
# frozen_string_literal: true
RSpec.describe Faraday::Request::Authorization do
let(:conn) do
Faraday.new do |b|
b.request :authorization, auth_type, *auth_config
b.adapter :test do |stub|
stub.get('/auth-echo') do |env|
[200, {}, env[:request_headers]['Authorization']]
end
end
end
end
shared_examples 'does not interfere with existing authentication' do
context 'and request already has an authentication header' do
let(:response) { conn.get('/auth-echo', nil, authorization: 'OAuth oauth_token') }
it 'does not interfere with existing authorization' do
expect(response.body).to eq('OAuth oauth_token')
end
end
end
let(:response) { conn.get('/auth-echo') }
describe 'basic_auth' do
let(:auth_type) { :basic }
context 'when passed correct params' do
let(:auth_config) { %w[aladdin opensesame] }
it { expect(response.body).to eq('Basic YWxhZGRpbjpvcGVuc2VzYW1l') }
include_examples 'does not interfere with existing authentication'
end
context 'when passed very long values' do
let(:auth_config) { ['A' * 255, ''] }
it { expect(response.body).to eq("Basic #{'QUFB' * 85}Og==") }
include_examples 'does not interfere with existing authentication'
end
end
describe 'authorization' do
let(:auth_type) { :Bearer }
context 'when passed a string' do
let(:auth_config) { ['custom'] }
it { expect(response.body).to eq('Bearer custom') }
include_examples 'does not interfere with existing authentication'
end
context 'when passed a proc' do
let(:auth_config) { [-> { 'custom_from_proc' }] }
it { expect(response.body).to eq('Bearer custom_from_proc') }
include_examples 'does not interfere with existing authentication'
end
context 'when passed a callable' do
let(:callable) { double('Callable Authorizer', call: 'custom_from_callable') }
let(:auth_config) { [callable] }
it { expect(response.body).to eq('Bearer custom_from_callable') }
include_examples 'does not interfere with existing authentication'
end
context 'with an argument' do
let(:response) { conn.get('/auth-echo', nil, 'middle' => 'crunchy surprise') }
context 'when passed a proc' do
let(:auth_config) { [proc { |env| "proc #{env.request_headers['middle']}" }] }
it { expect(response.body).to eq('Bearer proc crunchy surprise') }
include_examples 'does not interfere with existing authentication'
end
context 'when passed a lambda' do
let(:auth_config) { [->(env) { "lambda #{env.request_headers['middle']}" }] }
it { expect(response.body).to eq('Bearer lambda crunchy surprise') }
include_examples 'does not interfere with existing authentication'
end
context 'when passed a callable with an argument' do
let(:callable) do
Class.new do
def call(env)
"callable #{env.request_headers['middle']}"
end
end.new
end
let(:auth_config) { [callable] }
it { expect(response.body).to eq('Bearer callable crunchy surprise') }
include_examples 'does not interfere with existing authentication'
end
end
context 'when passed too many arguments' do
let(:auth_config) { %w[baz foo] }
it { expect { response }.to raise_error(ArgumentError) }
include_examples 'does not interfere with existing authentication'
end
end
end
|