Skip to content

Instantly share code, notes, and snippets.

@odf
Created August 10, 2011 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odf/1135616 to your computer and use it in GitHub Desktop.
Save odf/1135616 to your computer and use it in GitHub Desktop.
Handling cookies in Capybara with Rack::Test so that controllers see them
def set_cookie(key, value)
# This is how one needs to set cookies in Rack::Test.
jar.merge "#{key.to_s}=#{value};domain=#{domain};path=/"
end
def get_cookie(key)
jar[key.to_s]
end
def jar
# Sorry Selenium!
driver = Capybara.current_session.driver
driver.class.should == Capybara::RackTest::Driver
@jar ||= driver.browser.rack_mock_session.cookie_jar
end
def domain
@domain ||= current_host.sub /\A (?:https?:\/\/)? (.*) (?::\d+)? \z/x, '\\1'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment