1 require 'abstract_unit'
3 class HttpBasicAuthenticationTest
< Test
::Unit::TestCase
4 include ActionController
::HttpAuthentication::Basic
7 attr_accessor
:headers, :renders, :request
10 @headers, @renders = {}, []
11 @request = ActionController
::TestRequest.new
15 self.renders
<< options
20 @controller = DummyController
.new
21 @credentials = ActionController
::HttpAuthentication::Basic.encode_credentials("dhh", "secret")
24 def test_successful_authentication
25 login
= Proc
.new
{ |user_name
, password
| user_name
== "dhh" && password
== "secret" }
27 assert
authenticate(@controller, &login
)
30 assert_nothing_raised
do
31 assert
!authenticate(@controller, &login
)
35 set_headers
@credentials, 'REDIRECT_X_HTTP_AUTHORIZATION'
36 assert
authenticate(@controller, &login
)
39 def test_failing_authentication
41 assert
!authenticate(@controller) { |user_name
, password
| user_name
== "dhh" && password
== "incorrect" }
44 def test_authentication_request
45 authentication_request(@controller, "Megaglobalapp")
46 assert_equal
'Basic realm="Megaglobalapp"', @controller.headers
["WWW-Authenticate"]
47 assert_equal
:unauthorized, @controller.renders
.first
[:status]
51 def set_headers(value
= @credentials, name
= 'HTTP_AUTHORIZATION')
52 @controller.request
.env[name
] = value