如何在 Sinatra 中使用 rspec 和rack-test 测试标头

发布于 2024-12-29 03:14:11 字数 499 浏览 2 评论 0原文

因此,我有一个 Sinatra 应用程序,它通过 HTTP POST 从另一个服务接收 XML。我想在本地测试一下。我有一个发送到端点的测试 XML 文件。进展顺利。我还设置了一些如下标头:

post '/', xml, 'HTTP_X_MY_AWESOME_HEADER' => "It's value"

其中 xml 是发送到我的端点的普通 XML 的精确副本。但我作为参数传递的标头永远不会显示在输出中。

我在这里做错了什么吗? 这里有很多关于它的帖子,但都已经过时了。

我正在使用 Rspec 2.8、Sinatra 1.3.2、Ruby 1.9.3-p0、Rack::Test 0.6.1。

更新2012-01-28 11:37:显然我在问这个问题时没有思考。通过请求发送标头并不意味着我会在响应中收到它们。

所以现在的问题是:如何测试请求标头而不将它们与响应一起发送回来?

So I have a Sinatra app that receives an XML via a HTTP POST from another service. I want to test it locally. I have a test XML-file that I send to the endpoint. That goes well. I also set some headers like this:

post '/', xml, 'HTTP_X_MY_AWESOME_HEADER' => "It's value"

where xml is the exact copy of normal XML that is being sent to my endpoint. But the header I pass as a parameter is never displayed in output.

Am I doing something wrong here?
There are a lot of posts around here about it, but all are outdated.

I'm using Rspec 2.8, Sinatra 1.3.2, Ruby 1.9.3-p0, Rack::Test 0.6.1.

UPDATE 2012-01-28 11:37: Obviously I was not thinking while I was asking this question. Sending headers with request does not mean I will receive them back in the response.

So the question now is: How do I test request headers without sending them back with the response?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戒ㄋ 2025-01-05 03:14:11

您应该能够像这样检查last_request:

last_request.env["HTTP_X_MY_AWESOME_HEADER"]

使用RSpec &您将使用上面的示例进行测试:

last_request.env["HTTP_X_MY_AWESOME_HEADER"].should == "It's value"

希望您能获得批准:)

更多信息请参见:
http://www.sinatrarb.com/testing.html#asserting_expectations_about_the_response

HTH

You should be able to inspect the last_request like so:

last_request.env["HTTP_X_MY_AWESOME_HEADER"]

using RSpec & your example above you would test with:

last_request.env["HTTP_X_MY_AWESOME_HEADER"].should == "It's value"

And hopefully you'll get a green light :)

More info here:
http://www.sinatrarb.com/testing.html#asserting_expectations_about_the_response

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文