Skip to content

Commit

Permalink
authorization header helpers don't modify the stack
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Apr 22, 2012
1 parent 52a951a commit 18dae14
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 20 deletions.
13 changes: 10 additions & 3 deletions lib/faraday/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,18 @@ def #{method}(url = nil, body = nil, headers = nil, &block)
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
Expand Down
12 changes: 6 additions & 6 deletions lib/faraday/request/authorization.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -14,7 +14,7 @@ def self.build(type, token)

# 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|
Expand All @@ -24,14 +24,14 @@ def self.build_hash(type, hash)
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
Expand Down
3 changes: 2 additions & 1 deletion lib/faraday/request/basic_authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

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)
Expand Down
2 changes: 1 addition & 1 deletion lib/faraday/request/token_authentication.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
24 changes: 15 additions & 9 deletions test/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,30 @@ def test_initialize_stores_default_headers_from_options
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
Expand Down Expand Up @@ -268,7 +274,7 @@ def test_dups_connection_object
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')
Expand Down

0 comments on commit 18dae14

Please sign in to comment.