Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set HTTP_REFERER in cucumber+capybara step definitions?

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.

like image 240
sheldonh Avatar asked Dec 06 '11 14:12

sheldonh


2 Answers

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

like image 171
sheldonh Avatar answered Nov 02 '22 19:11

sheldonh


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
like image 24
Marek Příhoda Avatar answered Nov 02 '22 20:11

Marek Příhoda