Headers are not preserved on redirect #464

Closed
simonbaird opened this Issue Aug 23, 2011 · 6 comments

Projects

None yet

2 participants

@simonbaird

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:

simonbaird@cdc7ad7

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?

@simonbaird

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

@simonbaird

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

@jnicklas
Collaborator

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' })
end

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

@jnicklas
Collaborator

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
@simonbaird

Great stuff. Thanks very much.

@simonbaird

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)
    visit_without_redir_header_fix(path)
    # Now visit again and the headers will be used correctly
    visit_without_redir_header_fix(current_url)
  end
  alias_method_chain :visit, :redir_header_fix
end

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