1 require "#{File.dirname(__FILE__)}/abstract_unit"
4 class AuthorizationTest < Test::Unit::TestCase
5 Response = Struct.new(:code)
8 @conn = ActiveResource::Connection.new('http://localhost')
9 @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person')
10 @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person')
11 @authenticated_conn = ActiveResource::Connection.new("http://david:test123@localhost")
12 @authorization_request_header = { 'Authorization' => 'Basic ZGF2aWQ6dGVzdDEyMw==' }
14 ActiveResource::HttpMock.respond_to do |mock|
15 mock.get "/people/2.xml", @authorization_request_header, @david
16 mock.put "/people/2.xml", @authorization_request_header, nil, 204
17 mock.delete "/people/2.xml", @authorization_request_header, nil, 200
18 mock.post "/people/2/addresses.xml", @authorization_request_header, nil, 201, 'Location' => '/people/1/addresses/5'
22 def test_authorization_header
23 authorization_header = @authenticated_conn.send!(:authorization_header)
24 assert_equal @authorization_request_header['Authorization'], authorization_header['Authorization']
25 authorization = authorization_header["Authorization"].to_s.split
27 assert_equal "Basic", authorization[0]
28 assert_equal ["david", "test123"], Base64.decode64(authorization[1]).split(":")[0..1]
31 def test_authorization_header_with_username_but_no_password
32 @conn = ActiveResource::Connection.new("http://david:@localhost")
33 authorization_header = @conn.send!(:authorization_header)
34 authorization = authorization_header["Authorization"].to_s.split
36 assert_equal "Basic", authorization[0]
37 assert_equal ["david"], Base64.decode64(authorization[1]).split(":")[0..1]
40 def test_authorization_header_with_password_but_no_username
41 @conn = ActiveResource::Connection.new("http://:test123@localhost")
42 authorization_header = @conn.send!(:authorization_header)
43 authorization = authorization_header["Authorization"].to_s.split
45 assert_equal "Basic", authorization[0]
46 assert_equal ["", "test123"], Base64.decode64(authorization[1]).split(":")[0..1]
50 david = @authenticated_conn.get("/people/2.xml")
51 assert_equal "David", david["name"]
55 response = @authenticated_conn.post("/people/2/addresses.xml")
56 assert_equal "/people/1/addresses/5", response["Location"]
60 response = @authenticated_conn.put("/people/2.xml")
61 assert_equal 204, response.code
65 response = @authenticated_conn.delete("/people/2.xml")
66 assert_equal 200, response.code
69 def test_raises_invalid_request_on_unauthorized_requests
70 assert_raises(ActiveResource::InvalidRequestError) { @conn.post("/people/2.xml") }
71 assert_raises(ActiveResource::InvalidRequestError) { @conn.post("/people/2/addresses.xml") }
72 assert_raises(ActiveResource::InvalidRequestError) { @conn.put("/people/2.xml") }
73 assert_raises(ActiveResource::InvalidRequestError) { @conn.delete("/people/2.xml") }
77 def assert_response_raises(klass, code)
78 assert_raise(klass, "Expected response code #{code} to raise #{klass}") do
79 @conn.send!(:handle_response, Response.new(code))