gpt4 book ai didi

ruby-on-rails - 人们如何测试 rails 3.1 + force_ssl?

转载 作者:行者123 更新时间:2023-11-28 19:43:17 24 4
gpt4 key购买 nike

rails 3.1 中的 force_ssl 函数被硬编码为忽略开发环境,而不是测试。这在我的(最小测试)测试中给了我重定向错误。是设置我的测试服务器以支持 ssl 的解决方案(如果是,如何?)。如果不是,我是否应该猴子修补 force_ssl 以忽略测试中的请求?

 def force_ssl(options = {})
host = options.delete(:host)
before_filter(options) do
if !request.ssl? && !Rails.env.development?
redirect_options = {:protocol => 'https://', :status => :moved_permanently}
redirect_options.merge!(:host => host) if host
flash.keep
redirect_to redirect_options
end
end
end

编辑 找到了这条链,它证实了其他人认为这是一个问题,但看起来还没有提交修复:https://github.com/rails/rails/pull/2630

最佳答案

替代整个应用程序的猴子修补的另一种选择是仅在您的测试套件中覆盖 force_ssl。例如,在 test/test_helper.rb 中,您可以添加:

# We don't want SSL redirects in our test suite
module ActionController::ForceSSL::ClassMethods
def force_ssl(options = {})
# noop
end
end

关于ruby-on-rails - 人们如何测试 rails 3.1 + force_ssl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8989361/

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