Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headers are not preserved on redirect #464

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

Headers are not preserved on redirect #464

simonbaird opened this issue Aug 23, 2011 · 6 comments
Milestone

Comments

@simonbaird
Copy link

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
Copy link
Author

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

@simonbaird
Copy link
Author

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

@jnicklas
Copy link
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
Copy link
Collaborator

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

@simonbaird
Copy link
Author

Great stuff. Thanks very much.

@simonbaird
Copy link
Author

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 pushed a commit to reinteractive/capybara that referenced this issue Nov 1, 2011
@lock lock bot locked and limited conversation to collaborators Aug 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants