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

问题描述

我想利用 force_ssl 功能 在 rails 3.1rc4 中.

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

响应是对 https://test.host/ 的 301 重定向,而不是呈现页面.

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) 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 17:45