How to Set a Request.Referrer Inside My Rspec

How do i set a request.referrer inside my RSpec?

Mock it:

specify 'foo' do
controller.request.should_receive(:referer).and_return('http://example.com')
# get whatever
end

Or if you don't care if it doesn't get called, stub it:

controller.request.stub referer: 'http://example.com'

How to set the referrer in an _integration_ test

Capybara.current_session.driver.header 'Referer', 'http://example.com'

It looks like you're using Capybara, you can use Capybara to explicitly set the referer. You would have to update it whenever you wanted it to change, and if you needed to remove it you could set it to nil.

Maybe a bit cleaner:

referer = 'http://example.com'
Capybara.current_session.driver.header 'Referer', referer

Rails using request.referer in testing

Two things:

First, this answers your main question:

How do I set HTTP_REFERER when testing in Rails?

Second, it's not a given that request.referer will be set. Most browsers supply the header when you navigate from a previous page; most don't when you hand-enter a URL. HTTP clients can't be assumed to do so overall, and you have to be prepared to get nil from that attribute.

How do I set HTTP_REFERER when testing in Rails?

Their recommendation translates to the following:

setup do
@request.env['HTTP_REFERER'] = 'http://test.com/sessions/new'
post :create, { :user => { :email => 'invalid@abc' } }
end

How to set HTTP_REFERER in cucumber+capybara step definitions?

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



Related Topics



Leave a reply



Submit