Upgrading to rails 3.0 — making sure you use rack headers everywhere

Normal headers like Accept or :authorization do not work in rails 3 integration tests and you need to convert everything to HTTP_ACCEPT etc, to help find all those places and make sure you do not introduce new bugs in rails 2 add this:

# https://grosser.it/2012/10/19/upgrading-to-rails-3-0-making-sure-you-use-rack-headers-everywhere/
# message can be changed on rails 3, but keep the warning, it's so hard to track down missing headers
# maybe try to remove in rails 3.1+
# can be tested by e.g. changing header to Accept instead of HTTP_ACCEPT
class ActionController::Integration::Session
  # headers that are only used by our code and not rails/rack can be whitelisted, but make sure they work on rails 2 and 3
  HEADER_WHITELIST = ['Funky-Headers-You-Have-To-Use']
  def process_with_header_warning(*args)
    if args[3] && bad = args[3].keys.detect{|k| !k.is_a?(String) || (!HEADER_WHITELIST.include?(k) && k !~ /^[A-Z_\d]+$/) }
      raise "Header #{bad} will not work on rails 3, please uppercase (Content-Type -> CONTENT_TYPE) and prefix HTTP_ (Accept -> HTTP_ACCEPT)"
    end
    process_without_header_warning(*args)
  end
  alias_method_chain :process, :header_warning
end

Leave a comment