Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test headers with rspec and rack-test in Sinatra

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?

like image 637
Ivan Avatar asked Jan 26 '12 20:01

Ivan


1 Answers

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

like image 138
gef Avatar answered Oct 22 '22 19:10

gef