0

感谢这个出色的问答,我发现了如何通过将以下代码放入我的应用程序控制器来保护我的 Rails 应用程序:

before_filter :authenticate

protected
def authenticate
    authenticate_or_request_with_http_basic do |username, password|
      username == "username" && password == "password"
    end
end

这一切都很好,但我注意到我编写的所有测试(全部通过)现在都失败了。当我注释掉 before_filter 调用时,它们都会再次通过。

我该怎么办?有没有办法从 htaccess 保护中排除测试?

4

1 回答 1

1

您可以通过包含基本身份验证来更改测试,请参见:

def test_should_get_index
  @request.env["HTTP_AUTHORIZATION"] = "Basic " + Base64::encode64("username:password")
  get :index
  assert_response :success
  assert_not_nil assigns(:articles)
end

资料来源: http: //flip.netzbeben.de/2008/06/functional-test-for-http-authentication-in-rails-2/

于 2012-11-25T00:15:25.673 回答