Headers are not preserved on redirect #464

simonbaird opened this Issue Aug 23, 2011 · 6 comments


None yet

2 participants


I'm doing something like this to simulate an authenticated (kerberos) user:

page.driver.browser.options[:headers]['HTTP_X_REMOTE_USER'] = username

It works fine unless I visit a url with a redirect. Then the header is lost and I'm no longer authed.

I wrote a failing test that illustrates the problem. See here:


I'm not sure how to fix this, but I'd give it a try given a small amount of encouragement...

Ps, anyone know a way to work around this issue?


Maybe implement a follow_redirect! method instead of using the rack method?


Maybe this is really a bug in rack-test...


you're really not supposed to set headers this way. The Rack-Test driver has a headers option which you can use. Like this:

Capybara.register_driver :rack_test do |app|
  Capybara::RackTest::Driver.new(app, :headers => { 'Foo' => 'Bar' })

however, that actually might have the same issue. Try it out and let me know how it goes.


Ahh, never mind my comment, just understood why you're doing what you're doing. This does indeed appear to be a bug.

@jnicklas jnicklas pushed a commit that closed this issue Aug 30, 2011
Jonas Nicklas and Nicklas Ramhöj Preserve headers on redirects, closes #464 33b1a27
@jnicklas jnicklas closed this in 33b1a27 Aug 30, 2011

Great stuff. Thanks very much.


Here's a workaround for anyone waiting for the updated gem.

module Capybara::DSL
  # Work around for problem fixed here:
  # https://github.com/jnicklas/capybara/issues/464
  def visit_with_redir_header_fix(path)
    # This will follow redirects (but not preserve headers)
    # Now visit again and the headers will be used correctly
  alias_method_chain :visit, :redir_header_fix

Disclaimer: This is not pretty. Since you will be visiting everything twice there might be some unintended side-effects. Use at your own risk, etc.

@radar radar added a commit to reinteractive/capybara that referenced this issue Nov 1, 2011
@radar Jonas Nicklas and Nicklas Ramhöj + radar Preserve headers on redirects, closes #464 133cc77
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment