authorization header helpers don't modify the stack

This commit is contained in:
rick 2012-04-21 19:46:05 -06:00
parent 52a951aed2
commit 18dae1447d
5 changed files with 34 additions and 20 deletions

View File

@ -102,11 +102,18 @@ module Faraday
end
def basic_auth(login, pass)
@builder.insert(0, Faraday::Request::BasicAuthentication, login, pass)
headers[Faraday::Request::Authorization::KEY] =
Faraday::Request::BasicAuthentication.header(login, pass)
end
def token_auth(token, options = {})
@builder.insert(0, Faraday::Request::TokenAuthentication, token, options)
def token_auth(token, options = nil)
headers[Faraday::Request::Authorization::KEY] =
Faraday::Request::TokenAuthentication.header(token, options)
end
def authorization(type, token)
headers[Faraday::Request::Authorization::KEY] =
Faraday::Request::Authorization.header(type, token)
end
# Internal: Traverse the middleware stack in search of a

View File

@ -1,9 +1,9 @@
module Faraday
class Request::Authorization < Faraday::Middleware
HEADER_KEY = "Authorization".freeze
KEY = "Authorization".freeze
# Public
def self.build(type, token)
def self.header(type, token)
case token
when String, Symbol then "#{type} #{token}"
when Hash then build_hash(type.to_s, token)
@ -14,7 +14,7 @@ module Faraday
# Internal
def self.build_hash(type, hash)
offset = HEADER_KEY.size + type.size + 3
offset = KEY.size + type.size + 3
comma = ",\n#{' ' * offset}"
values = []
hash.each do |key, value|
@ -24,14 +24,14 @@ module Faraday
end
def initialize(app, type, token)
@header_value = self.class.build(type, token)
@header_value = self.class.header(type, token)
super(app)
end
# Public
def call(env)
unless env[:request_headers][HEADER_KEY]
env[:request_headers][HEADER_KEY] = @header_value
unless env[:request_headers][KEY]
env[:request_headers][KEY] = @header_value
end
@app.call(env)
end

View File

@ -2,7 +2,8 @@ require 'base64'
module Faraday
class Request::BasicAuthentication < Request::Authorization
def self.build(login, pass)
# Public
def self.header(login, pass)
value = Base64.encode64([login, pass].join(':'))
value.gsub!("\n", '')
super(:Basic, value)

View File

@ -1,7 +1,7 @@
module Faraday
class Request::TokenAuthentication < Request::Authorization
# Public
def self.build(token, options = nil)
def self.header(token, options = nil)
options ||= {}
options[:token] = token
super :Token, options

View File

@ -68,24 +68,30 @@ class TestConnection < Faraday::TestCase
assert_equal 'Faraday', conn.headers['User-agent']
end
def test_basic_auth_prepends_basic_auth_middleware
def test_basic_auth_sets_header
conn = Faraday::Connection.new
assert_nil conn.headers['Authorization']
conn.basic_auth 'Aladdin', 'open sesame'
assert_equal Faraday::Request::BasicAuthentication, conn.builder[0].klass
assert_equal ['Aladdin', 'open sesame'], conn.builder[0].instance_eval { @args }
assert auth = conn.headers['Authorization']
assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', auth
end
def test_auto_parses_basic_auth_from_url_and_unescapes
conn = Faraday::Connection.new :url => "http://foo%40bar.com:pass%20word@sushi.com/fish"
assert_equal Faraday::Request::BasicAuthentication, conn.builder[0].klass
assert_equal ['foo@bar.com', 'pass word'], conn.builder[0].instance_eval { @args }
assert auth = conn.headers['Authorization']
assert_equal Faraday::Request::BasicAuthentication.header("foo@bar.com", "pass word"), auth
end
def test_token_auth_prepends_token_auth_middleware
def test_token_auth_sets_header
conn = Faraday::Connection.new
assert_nil conn.headers['Authorization']
conn.token_auth 'abcdef', :nonce => 'abc'
assert_equal Faraday::Request::TokenAuthentication, conn.builder[0].klass
assert_equal ['abcdef', { :nonce => 'abc' }], conn.builder[0].instance_eval { @args }
assert auth = conn.headers['Authorization']
assert_match /^Token /, auth
assert_match /token="abcdef"/, auth
assert_match /nonce="abc"/, auth
end
def test_build_url_uses_connection_host_as_default_uri_host
@ -268,7 +274,7 @@ class TestConnection < Faraday::TestCase
other.headers['content-length'] = 12
other.params['b'] = '2'
assert_equal 3, other.builder.handlers.size
assert_equal 2, other.builder.handlers.size
assert_equal 2, conn.builder.handlers.size
assert !conn.headers.key?('content-length')
assert !conn.params.key?('b')