Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
68fdfde0
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
68fdfde0
编写于
1月 18, 2009
作者:
P
Pratik Naik
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve HTTP Basic authentication tests
上级
39e1ac65
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
88 addition
and
54 deletion
+88
-54
actionpack/test/controller/http_authentication_test.rb
actionpack/test/controller/http_authentication_test.rb
+0
-54
actionpack/test/controller/http_basic_authentication_test.rb
actionpack/test/controller/http_basic_authentication_test.rb
+88
-0
未找到文件。
actionpack/test/controller/http_authentication_test.rb
已删除
100644 → 0
浏览文件 @
39e1ac65
require
'abstract_unit'
class
HttpBasicAuthenticationTest
<
Test
::
Unit
::
TestCase
include
ActionController
::
HttpAuthentication
::
Basic
class
DummyController
attr_accessor
:headers
,
:renders
,
:request
def
initialize
@headers
,
@renders
=
{},
[]
@request
=
ActionController
::
TestRequest
.
new
end
def
render
(
options
)
self
.
renders
<<
options
end
end
def
setup
@controller
=
DummyController
.
new
@credentials
=
ActionController
::
HttpAuthentication
::
Basic
.
encode_credentials
(
"dhh"
,
"secret"
)
end
def
test_successful_authentication
login
=
Proc
.
new
{
|
user_name
,
password
|
user_name
==
"dhh"
&&
password
==
"secret"
}
set_headers
assert
authenticate
(
@controller
,
&
login
)
set_headers
''
assert_nothing_raised
do
assert
!
authenticate
(
@controller
,
&
login
)
end
set_headers
nil
set_headers
@credentials
,
'REDIRECT_X_HTTP_AUTHORIZATION'
assert
authenticate
(
@controller
,
&
login
)
end
def
test_failing_authentication
set_headers
assert
!
authenticate
(
@controller
)
{
|
user_name
,
password
|
user_name
==
"dhh"
&&
password
==
"incorrect"
}
end
def
test_authentication_request
authentication_request
(
@controller
,
"Megaglobalapp"
)
assert_equal
'Basic realm="Megaglobalapp"'
,
@controller
.
headers
[
"WWW-Authenticate"
]
assert_equal
:unauthorized
,
@controller
.
renders
.
first
[
:status
]
end
private
def
set_headers
(
value
=
@credentials
,
name
=
'HTTP_AUTHORIZATION'
)
@controller
.
request
.
env
[
name
]
=
value
end
end
actionpack/test/controller/http_basic_authentication_test.rb
0 → 100644
浏览文件 @
68fdfde0
require
'abstract_unit'
class
DummyController
<
ActionController
::
Base
before_filter
:authenticate
,
:only
=>
:index
before_filter
:authenticate_with_request
,
:only
=>
:display
def
index
render
:text
=>
"Hello Secret"
end
def
display
render
:text
=>
'Definitely Maybe'
end
private
def
authenticate
authenticate_or_request_with_http_basic
do
|
username
,
password
|
username
==
'lifo'
&&
password
==
'world'
end
end
def
authenticate_with_request
if
authenticate_with_http_basic
{
|
username
,
password
|
username
==
'pretty'
&&
password
==
'please'
}
@logged_in
=
true
else
request_http_basic_authentication
(
"SuperSecret"
)
end
end
end
class
HttpBasicAuthenticationTest
<
ActionController
::
TestCase
AUTH_HEADERS
=
[
'HTTP_AUTHORIZATION'
,
'X-HTTP_AUTHORIZATION'
,
'X_HTTP_AUTHORIZATION'
,
'REDIRECT_X_HTTP_AUTHORIZATION'
]
tests
DummyController
AUTH_HEADERS
.
each
do
|
header
|
test
"successful authentication with
#{
header
.
downcase
}
"
do
@request
.
env
[
header
]
=
encode_credentials
(
'lifo'
,
'world'
)
get
:index
assert_response
:success
assert_equal
'Hello Secret'
,
@response
.
body
,
"Authentication failed for request header
#{
header
}
"
end
end
AUTH_HEADERS
.
each
do
|
header
|
test
"unsuccessful authentication with
#{
header
.
downcase
}
"
do
@request
.
env
[
header
]
=
encode_credentials
(
'h4x0r'
,
'world'
)
get
:index
assert_response
:unauthorized
assert_equal
"HTTP Basic: Access denied.
\n
"
,
@response
.
body
,
"Authentication didn't fail for request header
#{
header
}
"
end
end
test
"authentication request without credential"
do
get
:display
assert_response
:unauthorized
assert_equal
"HTTP Basic: Access denied.
\n
"
,
@response
.
body
assert_equal
'Basic realm="SuperSecret"'
,
@response
.
headers
[
'WWW-Authenticate'
]
end
test
"authentication request with invalid credential"
do
@request
.
env
[
'HTTP_AUTHORIZATION'
]
=
encode_credentials
(
'pretty'
,
'foo'
)
get
:display
assert_response
:unauthorized
assert_equal
"HTTP Basic: Access denied.
\n
"
,
@response
.
body
assert_equal
'Basic realm="SuperSecret"'
,
@response
.
headers
[
'WWW-Authenticate'
]
end
test
"authentication request with valid credential"
do
@request
.
env
[
'HTTP_AUTHORIZATION'
]
=
encode_credentials
(
'pretty'
,
'please'
)
get
:display
assert_response
:success
assert
assigns
(
:logged_in
)
assert_equal
'Definitely Maybe'
,
@response
.
body
end
private
def
encode_credentials
(
username
,
password
)
"Basic
#{
ActiveSupport
::
Base64
.
encode64
(
"
#{
username
}
:
#{
password
}
"
)
}
"
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录