gpt4 book ai didi

ruby - HTTP 摘要 + Rspec

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:56 25 4
gpt4 key购买 nike

我的 Rails 3 应用程序上有一个非常基本的 HTTP 摘要身份验证设置。它主要遵循 Rails Controller Guide 中的示例:

我的 ApplicationController 有一个 before_filter:

def digest_authenticate
success = authenticate_or_request_with_http_digest("Application") do |username|
APP_CONFIG["admin"]
end
end

这一切都很好。页面受到应有的保护。

我现在正尝试在 RSpec 中对此进行测试,但惨遭失败。

我关注了this SO accepted answer并将 authenticate_with_http_digest 方法放在支持文件中。这是我的实际测试:

describe DashboardController do
describe "GET 'index'" do
it "returns http success" do
authenticate_with_http_digest(foo, bar, baz)

visit root_path
response.should be_success
response.code.should == '200'
end
end
end

几个问题:

  1. 无论我是否调用 authenticate_with_http_digest,测试每次都通过了
  2. 我传递给 authenticate_with_http_digest 的参数是伪造的,而且似乎无关紧要。这些不需要与我在 APP_CONFIG["admin"] 中存储的内容相匹配吗?
  3. 如果我从我的 digest_authenticate before_filter 打印出 success 的值,它总是打印出 401,即使我确实将正确的参数传递给了我的 rspec 助手。

关于如何有效测试 HTTP 摘要式身份验证的任何想法?

谢谢!

最佳答案

对于 Controller 测试,您应该使用 get :index 调用而不是 visit root_path 调用。这将适用于您正在 Controller 测试的 HTTP 动词和 Rails 操作的任何有效组合。

visit 方法是 Capybara 的一部分,只能在请求规范中使用。

关于ruby - HTTP 摘要 + Rspec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13946463/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com