ruby-on-rails - 在 RSpec Rails 中测试 HTTPS (SSL) 请求

标签 ruby-on-rails ssl https rspec ruby-on-rails-3.1

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

相关文章:

docker - 设置 https 访问 nginx docker 容器

asp.net - 生成 PDF,IE 和 HTTPS 出错

ruby-on-rails - rails 与闹鬼 : Screen won't resize for screenshots

c# - 无法在客户端解码证书 new X509Certificate2()

typescript - 如何为 loopback4 添加自签名 ssl 证书?

linux - 如何在命令行上不显示 KeyStorePassword?

javascript - 将我的文件重命名为 .js.erb 允许我在 javascript 中写入 erb,但不能在 erb 中写入 javascript

ruby-on-rails - 按添加日期对集合中的项目进行排序 (Rails)

ruby-on-rails - 是否有任何与 Rails 模型集成的 ETL 工具?

security - 通过 Apache2 ProxyPass 访问互联网