Skip to content

Instantly share code, notes, and snippets.

@brenes
Created January 13, 2012 13:27
Show Gist options
  • Select an option

  • Save brenes/1606111 to your computer and use it in GitHub Desktop.

Select an option

Save brenes/1606111 to your computer and use it in GitHub Desktop.
Fix for problem between cucumber/Mundo Pepino/Webrat and Rack::Rewrite
# I had Cucumber / Mundo Pepino configured with Webrat and used jtrupiano's Rack::Rewrite (https://github.com/jtrupiano/rack-rewrite) to handle redirections between some old URLs and new ones
# When I tested if redirections were being handled correctly I encountered with a big issue: URL of the redirection appeared to be nil.
# I tested it manually and redirections were fine, so I though this could be a problem with the interaction between Rack::Rewrite and Webrat.
# After some dig and debug on the gems (cucumber, webrat and rack mostly) code I found the problem:
# Line 283 of lib/webrat/core/session.rb
#
# def response_location
# response.headers["Location"]
# end
# puts response.headers #--> {"Location" : "http://www.example.com/new-url"}
# puts response.headers["Location"] #--> nil
# So, although the header is there, when requested it returns nil... WTF?
# response.headers is an instance of Rack::Utils::HeaderHash class, so I went to the code. It's a class that inherits from Hash and redefines [] and []= methods
# Line 210 of lib/rack/utils.rb on Rack gem
#
# def [](k)
# super @names[k.downcase]
# end
#
# def []=(k, v)
# delete k
# @names[k.downcase] = k
# super k, v
# end
# I'm not quite sure on why it behaves like this, but when response["Location"] is requested @names[k.downcase] (this means @names["location"]) is 'nil' so we only need to call super with the actual key.
# Following code opens Rack::Utils::HeaderHash class and redefines [] method. Now response["Location"] returns "http://www.example.com/new-url"
# Strange bug on the interaction between Capybara and Rack found and fixed
module Rack
module Utils
class HeaderHash
def [](k)
super(@names[k.downcase] || k)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment