Simple way to test an HTTPS (SSL) request with RSpec

纵饮孤独 提交于 2019-12-06 07:01:09

When using Sinatra and RSpec, you are using the #get/#post methods provided by Rack::Test, so I suggest you look there to see how they work: https://github.com/brynary/rack-test/blob/master/lib/rack/test.rb

As you can see, they take an optional env Hash, where you can set "HTTPS" to "on", like this:

get '/', {}, {'HTTPS' => 'on'}

If you want to set it by default for all your requests, you need to override the Rack::Test::Session#default_env method, and had 'HTTPS' => 'on' to it (I suggest doing it in your spec_helper.rb), like this:

class Rack::Test::Session
  def default_env
    { "rack.test" => true, "REMOTE_ADDR" => "127.0.0.1", "HTTPS" => 'on' }.merge(headers_for_env)
  end
end

The previous answer with the Rack::Test:Session change did not work for me in rails 4.1.1. Instead I am now using the Rails RSpec bindings to force force all integration tests into https. To do this add the following code to spec_helper.rb:

class ActionDispatch::Integration::Session
  def https?
    true
  end
end
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!