我想利用 force_ssl
feature在 rails 3.1rc4 中。
class ApplicationController < ActionController::Base
force_ssl
end
问题是这破坏了我现有的大部分/所有 RSpec Controller 规范。例如,这失败了:
describe "GET 'index'" do
it "renders the Start page" do
get :index
response.should render_template 'index'
end
end
响应不是呈现页面,而是 301 重定向到 https://test.host/
。
如何更改我的规范以模拟 HTTPS GET/POST?
我必须手动更改每个测试还是有更简单的方法? (我意识到我只能在生产中调用 force_ssl
,但这并不理想。实际上,我应该测试 force_ssl
是否确实在预期时重定向到 https://。)
最佳答案
要指示 RSpec 在 Controller 规范中发出 SSL 请求,请使用:
request.env['HTTPS'] = 'on'
在您的示例中,这看起来像:
describe "GET 'index'" do
it "renders the Start page" do
request.env['HTTPS'] = 'on'
get :index
response.should render_template 'index'
end
end
关于ruby-on-rails - 在 RSpec Rails 中测试 HTTPS (SSL) 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6785261/