diff --git a/lib/em-http/client.rb b/lib/em-http/client.rb index 93819bcd..311183d2 100644 --- a/lib/em-http/client.rb +++ b/lib/em-http/client.rb @@ -155,6 +155,9 @@ def build_request # Set the User-Agent if it hasn't been specified head['user-agent'] ||= "EventMachine HttpClient" + # Set the auth from the URI if given + head['Authorization'] = @req.uri.userinfo.split(/:/, 2) if @req.uri.userinfo + head end diff --git a/spec/client_spec.rb b/spec/client_spec.rb index 9ca9d004..04a799b0 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -258,7 +258,20 @@ def failed(http=nil) it "should perform basic auth" do EventMachine.run { - http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get :head => {'authorization' => ['user', 'pass']} + http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/authtest').get :head => {'authorization' => ['user', 'pass']} + + http.errback { failed(http) } + http.callback { + http.response_header.status.should == 200 + EventMachine.stop + } + } + end + + it "should perform basic auth via the URL" do + EventMachine.run { + + http = EventMachine::HttpRequest.new('http://user:pass@127.0.0.1:8090/authtest').get http.errback { failed(http) } http.callback { diff --git a/spec/stallion.rb b/spec/stallion.rb index 198d43f7..3fc25672 100644 --- a/spec/stallion.rb +++ b/spec/stallion.rb @@ -194,19 +194,16 @@ def self.call(env) elsif stable.request.env["HTTP_IF_NONE_MATCH"] stable.response.status = 304 - elsif stable.request.env["HTTP_AUTHORIZATION"] - if stable.request.path_info == '/auth' + elsif stable.request.path_info == '/auth' && stable.request.env["HTTP_AUTHORIZATION"] + stable.response.status = 200 + stable.response.write stable.request.env["HTTP_AUTHORIZATION"] + elsif stable.request.path_info == '/authtest' + auth = "Basic %s" % Base64.encode64(['user', 'pass'].join(':')).split.join + if auth == stable.request.env["HTTP_AUTHORIZATION"] stable.response.status = 200 - stable.response.write stable.request.env["HTTP_AUTHORIZATION"] + stable.response.write 'success' else - auth = "Basic %s" % Base64.encode64(['user', 'pass'].join(':')).split.join - - if auth == stable.request.env["HTTP_AUTHORIZATION"] - stable.response.status = 200 - stable.response.write 'success' - else - stable.response.status = 401 - end + stable.response.status = 401 end elsif stable.request.path_info == '/relative-location' stable.response.status = 301