Setting request headers in Rails functional tests
I have some Ruby on Rails controllers that depend June on checking Collective a custom HTTP header value, which I wanted to test. In integration tests, you cheap NBA jerseys can pass a hash of headers wholesale NFL jerseys when you call your request Een method:
post(:create, cheap NBA jerseys {:something => {:name => "foo"}}, {"CUSTOM_HEADER" L?t => "bar"})
But in functional wholesale NFL jerseys tests, you set custom headers on the @request object:
@request.env["CUSTOM_HEADER"] = "bar" post(:create, {:something => {:name => "foo"}})
Why this isn’t handled consistently, I don’t know, but I had some trouble tracking it down as it’s not documented in the API docs for ActionController::TestCase.
5 Comments
Ernest Surudo
June 17, 2013Neither of these methods seem to work in Rails 4.rc1. Have you run into this?
Ernest Surudo
June 17, 2013My mistake. I was trying to set “Authorization” header, when I really needed to set “HTTP_AUTHORIZATION”. Thanks!
Caillou
February 22, 2016Man, this comment of yours just saved my day. Thanks to both the author of the post, and the author of the comment ! ๐
Ernest Surudo
February 29, 2016Glad I could help ๐ Feels good that something I posted over 2 years ago saved someone potentially hours of time, whereas it only took me a minute to make the post.
Damien
April 15, 2016Insane! Never would have guessed this myself. My test now passes. Thank you. You saved me hours, my friend.