在RSpec Rails中测试HTTPS(SSL)请求 [英] Test an HTTPS (SSL) request in RSpec Rails
问题描述
我想利用 force_ssl 功能。
I'd like to take advantage of the force_ssl
feature in rails 3.1rc4.
class ApplicationController < ActionController::Base
force_ssl
end
问题是这打破了大部分/全部我现有的RSpec控制器规格。例如,这失败了:
Problem is this breaks most/all of my existing RSpec controller specs. For example, this fails:
describe "GET 'index'" do
it "renders the Start page" do
get :index
response.should render_template 'index'
end
end
而不是呈现页面,响应是301重定向到 https://test.host/
。
Instead of rendering the page the response is a 301 redirect to https://test.host/
.
如何更改我的规范以模拟HTTPS GET / POST?
How can I change my specs to simulate an HTTPS GET/POST?
我是否必须手动更改每个测试或者是否有更简单的方法? (我意识到我只能在生产中调用 force_ssl
但这并不理想。真的,我应该测试 force_ssl
确实会在预期时重定向到https://。)
And do I have to change each test manually or is there an easier way? (I realise I could invoke force_ssl
only in production, but that's not ideal. Really, I should be testing that force_ssl
does indeed redirect to https:// when expected.)
推荐答案
要指示RSpec在控制器规范中发出SSL请求,请使用:
To instruct RSpec to make SSL requests in a controller spec use:
request.env['HTTPS'] = 'on'
在您的示例中,这看起来像:
In your example this looks like:
describe "GET 'index'" do
it "renders the Start page" do
request.env['HTTPS'] = 'on'
get :index
response.should render_template 'index'
end
end
这篇关于在RSpec Rails中测试HTTPS(SSL)请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!