aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/test/authorization_test.rb
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2006-09-29 16:25:49 +0000
committerRick Olson <technoweenie@gmail.com>2006-09-29 16:25:49 +0000
commit7ac6ed893fbfe9b4d4ce0e0ef18c3fecfbd48ff4 (patch)
tree289f036d32d0af56aef052b43f60b7cecf5c350a /activeresource/test/authorization_test.rb
parentd15d15b2c236a556f89536961adf2de7f1fd04dc (diff)
downloadrails-7ac6ed893fbfe9b4d4ce0e0ef18c3fecfbd48ff4.tar.gz
rails-7ac6ed893fbfe9b4d4ce0e0ef18c3fecfbd48ff4.tar.bz2
rails-7ac6ed893fbfe9b4d4ce0e0ef18c3fecfbd48ff4.zip
Add Basic HTTP Authentication to ActiveResource (closes #6305). [jonathan]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5208 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activeresource/test/authorization_test.rb')
-rw-r--r--activeresource/test/authorization_test.rb82
1 files changed, 82 insertions, 0 deletions
diff --git a/activeresource/test/authorization_test.rb b/activeresource/test/authorization_test.rb
new file mode 100644
index 0000000000..bfe51ce1e3
--- /dev/null
+++ b/activeresource/test/authorization_test.rb
@@ -0,0 +1,82 @@
+require "#{File.dirname(__FILE__)}/abstract_unit"
+require 'base64'
+
+class AuthorizationTest < Test::Unit::TestCase
+ Response = Struct.new(:code)
+
+ def setup
+ @conn = ActiveResource::Connection.new('http://localhost')
+ @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person')
+ @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person')
+ @authenticated_conn = ActiveResource::Connection.new("http://david:test123@localhost")
+ @authorization_request_header = { 'Authorization' => 'Basic ZGF2aWQ6dGVzdDEyMw==' }
+
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.get "/people/2.xml", @authorization_request_header, @david
+ mock.put "/people/2.xml", @authorization_request_header, nil, 204
+ mock.delete "/people/2.xml", @authorization_request_header, nil, 200
+ mock.post "/people/2/addresses.xml", @authorization_request_header, nil, 201, 'Location' => '/people/1/addresses/5'
+ end
+ end
+
+ def test_authorization_header
+ authorization_header = @authenticated_conn.send(:authorization_header)
+ assert_equal @authorization_request_header['Authorization'], authorization_header['Authorization']
+ authorization = authorization_header["Authorization"].to_s.split
+
+ assert_equal "Basic", authorization[0]
+ assert_equal ["david", "test123"], Base64.decode64(authorization[1]).split(":")[0..1]
+ end
+
+ def test_authorization_header_with_username_but_no_password
+ @conn = ActiveResource::Connection.new("http://david:@localhost")
+ authorization_header = @conn.send(:authorization_header)
+ authorization = authorization_header["Authorization"].to_s.split
+
+ assert_equal "Basic", authorization[0]
+ assert_equal ["david"], Base64.decode64(authorization[1]).split(":")[0..1]
+ end
+
+ def test_authorization_header_with_password_but_no_username
+ @conn = ActiveResource::Connection.new("http://:test123@localhost")
+ authorization_header = @conn.send(:authorization_header)
+ authorization = authorization_header["Authorization"].to_s.split
+
+ assert_equal "Basic", authorization[0]
+ assert_equal ["", "test123"], Base64.decode64(authorization[1]).split(":")[0..1]
+ end
+
+ def test_get
+ david = @authenticated_conn.get("/people/2.xml")
+ assert_equal "David", david["person"]["name"]
+ end
+
+ def test_post
+ response = @authenticated_conn.post("/people/2/addresses.xml")
+ assert_equal "/people/1/addresses/5", response["Location"]
+ end
+
+ def test_put
+ response = @authenticated_conn.put("/people/2.xml")
+ assert_equal 204, response.code
+ end
+
+ def test_delete
+ response = @authenticated_conn.delete("/people/2.xml")
+ assert_equal 200, response.code
+ end
+
+ def test_raises_invalid_request_on_unauthorized_requests
+ assert_raises(ActiveResource::InvalidRequestError) { @conn.post("/people/2.xml") }
+ assert_raises(ActiveResource::InvalidRequestError) { @conn.post("/people/2/addresses.xml") }
+ assert_raises(ActiveResource::InvalidRequestError) { @conn.put("/people/2.xml") }
+ assert_raises(ActiveResource::InvalidRequestError) { @conn.delete("/people/2.xml") }
+ end
+
+ protected
+ def assert_response_raises(klass, code)
+ assert_raise(klass, "Expected response code #{code} to raise #{klass}") do
+ @conn.send(:handle_response, Response.new(code))
+ end
+ end
+end