diff --git a/activeresource/lib/active_resource/connection.rb b/activeresource/lib/active_resource/connection.rb index fb3fde59d6a30..c70e3d567d3be 100644 --- a/activeresource/lib/active_resource/connection.rb +++ b/activeresource/lib/active_resource/connection.rb @@ -17,7 +17,7 @@ class Connection } attr_reader :site, :user, :password, :timeout - attr_accessor :format + attr_accessor :format, :use_basic_authentication class << self def requests @@ -32,6 +32,7 @@ def initialize(site, format = ActiveResource::Formats::XmlFormat) @user = @password = nil self.site = site self.format = format + self.use_basic_authentication = true end # Set URI for remote service. @@ -150,7 +151,11 @@ def build_request_headers(headers, http_method=nil) # Sets authorization header def authorization_header - (@user || @password ? { 'Authorization' => 'Basic ' + ["#{@user}:#{ @password}"].pack('m').delete("\r\n") } : {}) + if self.use_basic_authentication then + (@user || @password ? { 'Authorization' => 'Basic ' + ["#{@user}:#{ @password}"].pack('m').delete("\r\n") } : {}) + else + {} + end end def http_format_header(http_method) diff --git a/activeresource/test/connection_test.rb b/activeresource/test/connection_test.rb index 831fbc4003f42..9cf016eceb05b 100644 --- a/activeresource/test/connection_test.rb +++ b/activeresource/test/connection_test.rb @@ -183,6 +183,18 @@ def test_accept_http_header assert_nothing_raised(Mocha::ExpectationError) { @conn.get(path, {'Accept' => 'application/xhtml+xml'}) } end + def test_when_use_basic_authentication_is_false_no_authorization_header_is_sent + @http = mock('new Net::HTTP') + @conn.stubs(:http).returns(@http) + + @conn.use_basic_authentication = false + @conn.user = "francois" + @conn.password = "life is good" + + @http.expects(:get).with("/people/1.xml", Not(has_key("Authorization"))).returns(stub_everything("HTTP response", :code => "200")) + @conn.get("/people/1.xml") + end + protected def assert_response_raises(klass, code) assert_raise(klass, "Expected response code #{code} to raise #{klass}") do