spi*_*ike 9 testing ruby-on-rails ruby-on-rails-3.1
rails 3.1中的force_ssl函数是硬编码的,以忽略开发环境,但不进行测试.这给了我(minitest)测试中的重定向错误.解决方案是设置我的测试服务器以支持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
Run Code Online (Sandbox Code Playgroud)
编辑找到这个链,这证实了其他人认为这是一个问题,但看起来还没有一个承诺修复:https://github.com/rails/rails/pull/2630
Rya*_*ary 18
另一个选择而不是猴子修补整个应用程序只是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
Run Code Online (Sandbox Code Playgroud)
Ben*_*tis 17
这是另一种方法,如果你不想搞乱猴子修补......你可以使用前过滤器.这是rspec,而不是最小的语法,但你明白了:
before(:each) do
request.env["rack.url_scheme"] = "https"
end
Run Code Online (Sandbox Code Playgroud)
这使您的控制器确信它收到了SSL请求.
采用这种方法的好处是您可以实际编写测试以确保您的控制器需要SSL.:)
| 归档时间: |
|
| 查看次数: |
4290 次 |
| 最近记录: |