Setting request headers in Rails functional tests

Posted by on Sep 9, 2010 in code | 5 Comments

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

  1. Ernest Surudo
    June 17, 2013

    Neither of these methods seem to work in Rails 4.rc1. Have you run into this?

    Reply
    • Ernest Surudo
      June 17, 2013

      My mistake. I was trying to set “Authorization” header, when I really needed to set “HTTP_AUTHORIZATION”. Thanks!

      Reply
      • Caillou
        February 22, 2016

        Man, this comment of yours just saved my day. Thanks to both the author of the post, and the author of the comment ! 😀

        Reply
        • Ernest Surudo
          February 29, 2016

          Glad 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.

          Reply
      • Damien
        April 15, 2016

        Insane! Never would have guessed this myself. My test now passes. Thank you. You saved me hours, my friend.

        Reply

Leave a Reply