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

So, I have a Sinatra application that receives XML via HTTP POST from another service. I want to check it locally. I have a test XML file that I send to the endpoint. It's good. I also set some headers as follows:

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

where xml is an exact copy of plain XML that is sent to my endpoint. But the title that I pass as a parameter never shows up in the output.

Am I doing something wrong here? There are a lot of messages about it, but everyone is out of date.

I am 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 did not think when I asked this question. Sending headers with a request does not mean that I will return them back in response.

So, now the question arises: how to check the request headers without sending them back with the answer?

+7
source share
1 answer

You should be able to check last_request like this:

 last_request.env["HTTP_X_MY_AWESOME_HEADER"] 

using RSpec and your example above, you will test with:

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

And I hope you get a green light :)

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

NTN

+4
source

All Articles