How to test headers with rspec and rack

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?


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

链接地址: http://www.djcxy.com/p/57262.html

上一篇: 写“fib”并行运行:

下一篇: 如何用rspec和机架测试标题