How can I set the HTTP_REFERER request header for a request made from a cucumber step definition using capybara?
Googling around, I tried the following, none of which work:
header 'Referer', 'http://example.com/'
ENV['HTTP_REFERER'] = 'http://example.com/'
get '/', {}, { 'HTTP_REFERER' => 'http://example.com/' }
get '/', nil, { 'HTTP_REFERER' => 'http://example.com/' }
request.env['HTTP_REFERER'] = 'http://example.com/' # raises ArgumentError
visit '/'
I'm printing request.env['HTTP_REFERER'] in the controller, to prove that it's not coming through. If I test the controller with a web browser, the referer is printed correctly.
Note that the 3-parameter use of get above, actually does get the HTTP_REFERER into the request environment observed by the controller, but it seems to perturb the request in a way I can't figure out; all that gets rendered is a doctype element.
I'm using the default cucumber driver, Cucumber::RackTest::Driver, with cucumber-1.1.3, cucumber-rails-1.2.0 and capybara-1.1.2.
Since I'm using Cucumber::RackTest::Driver, the solution for me at the time of writing is:
Capybara.current_session.driver.header 'Referer', 'http://example.com'
This solution was inspired by the following unmerged pull request on capybara:
https://github.com/thoughtbot/capybara-webkit/issues/198
I don't know Cucumber that well, but this [i.e. calling post instead of visit 'some_path'] works for me in Rspec, so perhaps a similar solution might work for you (given that it's also an integration test):
it 'correctly updates the last_login_ip attribute' do
post login_path, { :email => user.email, :password => user.password }, { 'REMOTE_ADDR' => 'some_address' }
user.reload
user.last_login_ip.should == 'some_address'
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With