使用 RSpec 测试 HTTPS (SSL) 请求的简单方法 [英] Simple way to test an HTTPS (SSL) request with RSpec
问题描述
我希望 Rspec 使用 https 请求根.这是我目前拥有的:
I want Rspec to request the root using https. This is what I currently have:
it "requesting root (/) with HTTPS should return 200" do
get "https://test.host/"
last_response.should be_ok
end
尝试在 测试 HTTPS (SSL) 请求中提出的解决方案RSpec Rails 返回以下错误:
Attempting the solution proposed in Test an HTTPS (SSL) request in RSpec Rails returns the following error:
wrong number of arguments (0 for 1)
我认为我可以执行以下操作:
I would assume I could do something like:
get "/", :https => "on"
有没有办法在不指定主机的情况下请求根?
Is there a way to request the root without specifying the host?
推荐答案
使用 Sinatra 和 RSpec 时,您使用的是 Rack::Test<提供的
#get/#post
方法/code>,所以我建议你看看它们是如何工作的:https://github.com/brynary/rack-test/blob/master/lib/rack/test.rb
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
如您所见,它们采用可选的 env
哈希,您可以在其中将HTTPS"设置为on",如下所示:
As you can see, they take an optional env
Hash, where you can set "HTTPS" to "on", like this:
get '/', {}, {'HTTPS' => 'on'}
如果你想为你的所有请求默认设置它,你需要覆盖 Rack::Test::Session#default_env
方法,并且有 'HTTPS' =>'on'
到它(我建议在你的 spec_helper.rb
中这样做),像这样:
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
这篇关于使用 RSpec 测试 HTTPS (SSL) 请求的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!