简体   繁体   English

如何在 rspec 中测试 HTTPS 请求?

[英]How to test an HTTPS request in rspec?

Stackoverflow has a number of answers for this, but they all don't work. Stackoverflow 对此有很多答案,但它们都不起作用。 It seems like they're outdated.好像它们已经过时了。 I've tried code like我试过像这样的代码

get :index, protocol: :https

What your code snippet suggests it's a spec type: :request .您的代码片段表明它是规范type: :request The thing is - HTTP is not really involved in those kinds of tests.问题是 - HTTP 并没有真正参与这些测试。

What happens is, when you type get:index , a request object is being made, passed to rails, and response object is captured and you test against its state expect(response).to be_success发生的情况是,当您键入get:index时,正在发出请求 object,并将其传递给 rails,并捕获响应 object 并针对其 state 进行测试expect(response).to be_success

So you'd need to specify what exactly do you want to test:因此,您需要指定测试的确切内容:

  • is your app redirecting to https:// if someone uses http://如果有人使用http:// ,您的应用程序是否会重定向到https:// ://
  • are your certificates correctly set up and your production app works with https:// protocol?您的证书是否正确设置并且您的生产应用程序是否与https://协议一起使用? (hint: this can't be testes in the spec tests really) (提示:这在规范测试中真的不能是睾丸)
  • something else?别的东西?

SSL encryption should be transparent to your web app, are you having problems with that? SSL 加密对您的 web 应用程序应该是透明的,您对此有什么问题吗? Is your production app failing with HTTPS, and works properly with HTTP?您的生产应用程序是否因 HTTPS 而失败,并且与 HTTP 一起正常工作? (Feel free to compose a new question when you give it some thought) (考虑一下后,请随意提出一个新问题)

If you're trying to simulate the behavior of your non-HTTPS app behind an HTTPS proxy, the easiest thing is to pass an HTTP header such as X-Forwarded-Proto :如果您尝试在 HTTPS 代理后面模拟非 HTTPS 应用程序的行为,最简单的方法是传递 HTTP header,例如X-Forwarded-Proto

get :index, headers: { 'X-Forwarded-Proto' => 'https' }

This will cause request.base_url to begin with 'https' , request.scheme to return 'https' , and request.ssl?这将导致request.base_url'https'开头, request.scheme返回'https'request.ssl? to return true .返回true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM