mirror of
https://github.com/lostisland/faraday.git
synced 2025-12-09 00:02:49 -05:00
merrrrrge
This commit is contained in:
parent
e641735706
commit
d0df9abfb0
@ -31,8 +31,6 @@ module Faraday
|
|||||||
@ssl = options[:ssl] || {}
|
@ssl = options[:ssl] || {}
|
||||||
@parallel_manager = options[:parallel]
|
@parallel_manager = options[:parallel]
|
||||||
|
|
||||||
self.url_prefix = url if url
|
|
||||||
|
|
||||||
proxy(options.fetch(:proxy) { ENV['http_proxy'] })
|
proxy(options.fetch(:proxy) { ENV['http_proxy'] })
|
||||||
|
|
||||||
@params.update options[:params] if options[:params]
|
@params.update options[:params] if options[:params]
|
||||||
@ -43,6 +41,12 @@ module Faraday
|
|||||||
else
|
else
|
||||||
@builder = options[:builder] || Builder.new
|
@builder = options[:builder] || Builder.new
|
||||||
end
|
end
|
||||||
|
|
||||||
|
self.url_prefix = url if url
|
||||||
|
proxy(options[:proxy])
|
||||||
|
|
||||||
|
@params.update options[:params] if options[:params]
|
||||||
|
@headers.update options[:headers] if options[:headers]
|
||||||
end
|
end
|
||||||
|
|
||||||
def use(klass, *args, &block)
|
def use(klass, *args, &block)
|
||||||
@ -115,19 +119,11 @@ module Faraday
|
|||||||
end
|
end
|
||||||
|
|
||||||
def basic_auth(login, pass)
|
def basic_auth(login, pass)
|
||||||
auth = Base64.encode64("#{login}:#{pass}")
|
@builder.insert(0, Faraday::Request::BasicAuthentication, login, pass)
|
||||||
auth.gsub!("\n", "")
|
|
||||||
@headers['authorization'] = "Basic #{auth}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def token_auth(token, options = {})
|
def token_auth(token, options = {})
|
||||||
values = ["token=#{token.to_s.inspect}"]
|
@builder.insert(0, Faraday::Request::TokenAuthentication, token, options)
|
||||||
options.each do |key, value|
|
|
||||||
values << "#{key}=#{value.to_s.inspect}"
|
|
||||||
end
|
|
||||||
# 21 = "Authorization: Token ".size
|
|
||||||
comma = ",\n#{' ' * 21}"
|
|
||||||
@headers['authorization'] = "Token #{values * comma}"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def in_parallel?
|
def in_parallel?
|
||||||
|
|||||||
@ -17,13 +17,17 @@ module Faraday
|
|||||||
:UrlEncoded => 'url_encoded',
|
:UrlEncoded => 'url_encoded',
|
||||||
:Multipart => 'multipart',
|
:Multipart => 'multipart',
|
||||||
:Retry => 'retry',
|
:Retry => 'retry',
|
||||||
:Timeout => 'timeout'
|
:Timeout => 'timeout',
|
||||||
|
:BasicAuthentication => 'basic_authentication',
|
||||||
|
:TokenAuthentication => 'token_authentication'
|
||||||
|
|
||||||
register_lookup_modules \
|
register_lookup_modules \
|
||||||
:json => :JSON,
|
:json => :JSON,
|
||||||
:url_encoded => :UrlEncoded,
|
:url_encoded => :UrlEncoded,
|
||||||
:multipart => :Multipart,
|
:multipart => :Multipart,
|
||||||
:retry => :Retry
|
:retry => :Retry,
|
||||||
|
:basic_authentication => :BasicAuthentication,
|
||||||
|
:token_authentication => :TokenAuthentication
|
||||||
|
|
||||||
attr_reader :method
|
attr_reader :method
|
||||||
|
|
||||||
|
|||||||
17
lib/faraday/request/basic_authentication.rb
Normal file
17
lib/faraday/request/basic_authentication.rb
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
require 'base64'
|
||||||
|
|
||||||
|
module Faraday
|
||||||
|
class Request::BasicAuthentication < Faraday::Middleware
|
||||||
|
def initialize(app, login, pass)
|
||||||
|
super(app)
|
||||||
|
@header_value = "Basic #{Base64.encode64([login, pass].join(':')).gsub("\n", '')}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(env)
|
||||||
|
unless env[:request_headers]['Authorization']
|
||||||
|
env[:request_headers]['Authorization'] = @header_value
|
||||||
|
end
|
||||||
|
@app.call(env)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
21
lib/faraday/request/token_authentication.rb
Normal file
21
lib/faraday/request/token_authentication.rb
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
module Faraday
|
||||||
|
class Request::TokenAuthentication < Faraday::Middleware
|
||||||
|
def initialize(app, token, options={})
|
||||||
|
super(app)
|
||||||
|
|
||||||
|
values = ["token=#{token.to_s.inspect}"]
|
||||||
|
options.each do |key, value|
|
||||||
|
values << "#{key}=#{value.to_s.inspect}"
|
||||||
|
end
|
||||||
|
comma = ",\n#{' ' * ('Authorization: Token '.size)}"
|
||||||
|
@header_value = "Token #{values * comma}"
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(env)
|
||||||
|
unless env[:request_headers]['Authorization']
|
||||||
|
env[:request_headers]['Authorization'] = @header_value
|
||||||
|
end
|
||||||
|
@app.call(env)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
48
test/authentication_middleware_test.rb
Normal file
48
test/authentication_middleware_test.rb
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
|
||||||
|
|
||||||
|
class AuthenticationMiddlewareTest < Faraday::TestCase
|
||||||
|
def conn
|
||||||
|
Faraday::Connection.new('http://example.net/') do |builder|
|
||||||
|
yield builder
|
||||||
|
builder.adapter :test do |stub|
|
||||||
|
stub.get('/auth-echo') do |env|
|
||||||
|
[200, {}, env[:request_headers]['Authorization']]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_basic_middleware_adds_basic_header
|
||||||
|
response = conn { |b| b.request :basic_authentication, 'aladdin', 'opensesame' }.get('/auth-echo')
|
||||||
|
assert_equal 'Basic YWxhZGRpbjpvcGVuc2VzYW1l', response.body
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_basic_middleware_adds_basic_header_correctly_with_long_values
|
||||||
|
response = conn { |b| b.request :basic_authentication, 'A' * 255, '' }.get('/auth-echo')
|
||||||
|
assert_equal "Basic #{'QUFB' * 85}Og==", response.body
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_basic_middleware_does_not_interfere_with_existing_authorization
|
||||||
|
response = conn { |b| b.request :basic_authentication, 'aladdin', 'opensesame' }.
|
||||||
|
get('/auth-echo', :authorization => 'Token token="bar"')
|
||||||
|
assert_equal 'Token token="bar"', response.body
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_token_middleware_adds_token_header
|
||||||
|
response = conn { |b| b.request :token_authentication, 'quux' }.get('/auth-echo')
|
||||||
|
assert_equal 'Token token="quux"', response.body
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_token_middleware_includes_other_values_if_provided
|
||||||
|
response = conn { |b|
|
||||||
|
b.request :token_authentication, 'baz', :foo => 42
|
||||||
|
}.get('/auth-echo')
|
||||||
|
assert_equal "Token token=\"baz\",\n foo=\"42\"", response.body
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_token_middleware_does_not_interfere_with_existing_authorization
|
||||||
|
response = conn { |b| b.request :token_authentication, 'quux' }.
|
||||||
|
get('/auth-echo', :authorization => 'Token token="bar"')
|
||||||
|
assert_equal 'Token token="bar"', response.body
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -67,39 +67,24 @@ class TestConnection < Faraday::TestCase
|
|||||||
assert_equal 'Faraday', conn.headers['User-agent']
|
assert_equal 'Faraday', conn.headers['User-agent']
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_basic_auth_sets_authorization_header
|
def test_basic_auth_prepends_basic_auth_middleware
|
||||||
conn = Faraday::Connection.new
|
conn = Faraday::Connection.new
|
||||||
conn.basic_auth 'Aladdin', 'open sesame'
|
conn.basic_auth 'Aladdin', 'open sesame'
|
||||||
assert_equal 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==', conn.headers['Authorization']
|
assert_equal Faraday::Request::BasicAuthentication, conn.builder[0].klass
|
||||||
end
|
assert_equal ['Aladdin', 'open sesame'], conn.builder[0].instance_eval { @args }
|
||||||
|
|
||||||
def test_long_basic_auth_sets_authorization_header_without_new_lines
|
|
||||||
conn = Faraday::Connection.new
|
|
||||||
conn.basic_auth "A" * 255, ""
|
|
||||||
assert_equal "Basic #{'QUFB' * 85}Og==", conn.headers['Authorization']
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_auto_parses_basic_auth_from_url
|
|
||||||
conn = Faraday::Connection.new :url => "http://aladdin:opensesame@sushi.com/fish"
|
|
||||||
assert_equal 'Basic YWxhZGRpbjpvcGVuc2VzYW1l', conn.headers['Authorization']
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_auto_parses_basic_auth_from_url_and_unescapes
|
def test_auto_parses_basic_auth_from_url_and_unescapes
|
||||||
conn = Faraday::Connection.new :url => "http://foo%40bar.com:pass%20word@sushi.com/fish"
|
conn = Faraday::Connection.new :url => "http://foo%40bar.com:pass%20word@sushi.com/fish"
|
||||||
assert_equal 'Basic Zm9vQGJhci5jb206cGFzcyB3b3Jk', conn.headers['Authorization']
|
assert_equal Faraday::Request::BasicAuthentication, conn.builder[0].klass
|
||||||
|
assert_equal ['foo@bar.com', 'pass word'], conn.builder[0].instance_eval { @args }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_token_auth_sets_authorization_header
|
def test_token_auth_prepends_token_auth_middleware
|
||||||
conn = Faraday::Connection.new
|
|
||||||
conn.token_auth 'abcdef'
|
|
||||||
assert_equal 'Token token="abcdef"', conn.headers['Authorization']
|
|
||||||
end
|
|
||||||
|
|
||||||
def test_token_auth_with_options_sets_authorization_header
|
|
||||||
conn = Faraday::Connection.new
|
conn = Faraday::Connection.new
|
||||||
conn.token_auth 'abcdef', :nonce => 'abc'
|
conn.token_auth 'abcdef', :nonce => 'abc'
|
||||||
assert_equal 'Token token="abcdef",
|
assert_equal Faraday::Request::TokenAuthentication, conn.builder[0].klass
|
||||||
nonce="abc"', conn.headers['Authorization']
|
assert_equal ['abcdef', { :nonce => 'abc' }], conn.builder[0].instance_eval { @args }
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_build_url_uses_connection_host_as_default_uri_host
|
def test_build_url_uses_connection_host_as_default_uri_host
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user