在RSpec Rails中测试HTTPS(SSL)请求 [英] Test an HTTPS (SSL) request in RSpec Rails

查看:115
本文介绍了在RSpec Rails中测试HTTPS(SSL)请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想利用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆