Skip to content

Commit

Permalink
Allow Basic authentication to be turned off
Browse files Browse the repository at this point in the history
  • Loading branch information
francois committed Aug 4, 2009
1 parent aad5a30 commit c33a0a0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
9 changes: 7 additions & 2 deletions activeresource/lib/active_resource/connection.rb
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions activeresource/test/connection_test.rb
Expand Up @@ -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
Expand Down

0 comments on commit c33a0a0

Please sign in to comment.