Altering Request Headers in Rails Integration Tests

Posted by Tieg Zaharia Tue, 18 Mar 2008 14:18:00 GMT

As I just found out, it's way easier than I thought to change request headers during integration testing. Let's say you have defined a helper method that sets up a new "open_session":
 def regular_user
    open_session do |user|      
      def user.login(login, pass)
         post '/sessions', :login => login, :password => pass
         assert_equal 'You Are Logged In On a Browser!!', assigns[:page_title]
      end
    end
end
All of a sudden you decide to turn this into an iPhone-friendly login page, so you have to check for the HTTP_USER_AGENT header in the request. All you need to do is pass an extra hash of request headers (without the "HTTP_" prefix) into the request method (get, post, put, or delete):
 def regular_user
    open_session do |user|      
      def user.login(login, pass)
         post '/sessions', { :login => login, :password => pass } , { :user_agent => 'something iPhone something' }
         assert_equal 'You Are Logged In On an iPhone!!', assigns[:page_title]
      end
    end
end
This is a little obvious if you actually check the api for integration testing. But I didn't do that at first :)
Comments

Leave a response

Comments