Skip to content

Commit

Permalink
Merge pull request oesmith#121 from springleaf/basic-auth
Browse files Browse the repository at this point in the history
Support basic auth in requests
  • Loading branch information
ronwsmith committed Aug 25, 2015
2 parents a2800d4 + dc9264c commit e97ca92
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/billy/handlers/proxy_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def handle_request(method, url, headers, body)
end

req = EventMachine::HttpRequest.new(url, opts)
req = req.send(method.downcase, build_request_options(headers, body))
req = req.send(method.downcase, build_request_options(url, headers, body))

if req.error
return { error: "Request to #{url} failed with error: #{req.error}" }
Expand Down Expand Up @@ -50,12 +50,15 @@ def handle_request(method, url, headers, body)
nil
end

private
private

def build_request_options(headers, body)
def build_request_options(url, headers, body)
headers = Hash[headers.map { |k, v| [k.downcase, v] }]
headers.delete('accept-encoding')

uri = Addressable::URI.parse(url)
headers.merge!({'authorization' => [uri.user, uri.password]}) if uri.userinfo

req_opts = {
redirects: 0,
keepalive: false,
Expand Down
18 changes: 17 additions & 1 deletion spec/lib/billy/handlers/proxy_handler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
let(:request) do
{
method: 'post',
url: 'http://example.test:8080/index?some=param',
url: 'http://usern:pw@example.test:8080/index?some=param',
headers: { 'Accept-Encoding' => 'gzip',
'Cache-Control' => 'no-cache' },
body: 'Some body'
Expand Down Expand Up @@ -207,4 +207,20 @@
end
end
end

describe '#build_request_options' do
it 'creates authorization header when URL has basic auth' do
request_options = subject.send(:build_request_options, request[:url],
request[:headers],
request[:body])
expect(request_options[:head]).to have_key 'authorization'
end

it 'does not include authorization header without basic auth' do
request_options = subject.send(:build_request_options, request[:url].gsub('usern:pw@',''),
request[:headers],
request[:body])
expect(request_options[:head]).not_to have_key 'authorization'
end
end
end

0 comments on commit e97ca92

Please sign in to comment.