Skip to content

Commit

Permalink
Allow individual requests to provide a user and password. Close #588
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberdelia committed Aug 20, 2016
1 parent 3893295 commit dd14d98
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/excon/connection.rb
Expand Up @@ -223,6 +223,11 @@ def request(params={}, &block)
datum = @data.merge(params)
datum[:headers] = @data[:headers].merge(datum[:headers] || {})

if datum[:user] || datum[:password]
user, pass = Utils.unescape_form(datum[:user].to_s), Utils.unescape_form(datum[:password].to_s)
datum[:headers]['Authorization'] ||= 'Basic ' + ["#{user}:#{pass}"].pack('m').delete(Excon::CR_NL)
end

if datum[:scheme] == UNIX
datum[:headers]['Host'] = ''
else
Expand Down
7 changes: 7 additions & 0 deletions tests/basic_tests.rb
Expand Up @@ -91,12 +91,19 @@ def timed_streaming_test(endpoint, timeout)
Shindo.tests('Excon basics (Basic Auth Pass)') do
with_rackup('basic_auth.ru') do
basic_tests('http://test_user:test_password@127.0.0.1:9292')

tests('with frozen args').returns(200) do
user, pass, uri = ['test_user', 'test_password', 'http://127.0.0.1:9292'].map(&:freeze)
connection = Excon.new(uri, :user => user, :password => pass )
response = connection.request(:method => :get, :path => '/content-length/100')
response.status
end

tests('with persistent connection').returns(200) do
connection = Excon.new('http://127.0.0.1:9292', :persistent => true, :method => :get, :path => '/content-length/100')
response = connection.request(user: 'test_user', password: 'test_password')
response.status
end
end
end

Expand Down

0 comments on commit dd14d98

Please sign in to comment.