How do I set HTTP_REFERER when testing in Rails?

Ruby on-RailsRubyTesting

Ruby on-Rails Problem Overview


I'm trying to test a controller and I got this error. I understand the error, but don't know how to fix it.

test: on CREATE to :user with completely invalid email should respond with 
  redirect
(UsersControllerTest):ActionController::RedirectBackError: 
  No HTTP_REFERER was set in the request to this action, 
  so redirect_to :back could not be called successfully. 
If this is a test, make sure to specify request.env["HTTP_REFERER"].

Specify it where? I tried this:

setup { post :create, { :user => { :email => 'invalid@abc' } }, 
  { 'referer' => '/sessions/new' } }

But got the same error.

Specify it with what, exactly? I guess the URI of the view I want it to go back to:

'/sessions/new'

Is that what they mean?


OK, so it turns out they mean do this:

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

Can someone tell me where that's documented? I'd like to read up on the context of that information.

What if the domain is not "localhost:3000"? What if it's "localhost:3001" or something? Any way to anticipate that?

Why doesn't this work:

setup { post :create, { :user => { :email => 'invalid@abc' } }, 
  { 'referer' => '/sessions/new' } }

The Rails docs specifically say that's how you set the headers.

Ruby on-Rails Solutions


Solution 1 - Ruby on-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

Solution 2 - Ruby on-Rails

The accepted answer doesn't work for integration tests because the @request variable doesn't exist.

According to RailsGuides, you can pass headers to the helpers.

Rails <= 4:

test "blah" do
  get root_path, {}, {'HTTP_REFERER' => 'http://foo.com'}
  ...
end

Rails >= 5:

test "blah" do
  get root_path, params: { id: 12 }, headers: { "HTTP_REFERER" => "http://foo.com" }
  ...
end

Solution 3 - Ruby on-Rails

In response to the question:

> Why doesn't this work:

setup { post :create, { :user => { :email => 'invalid@abc' } }, 
{ 'referer' => '/sessions/new' } }

It doesn't work because the Rails doc you linked to documents a different class than the one you're probably using.

You linked to ActionController::Integration:Session. I'm guessing that you're writing a functional test (if you're using Test::Unit) or a controller test (if you're using Rspec). Either way, you're probably using ActionController::TestCase or a subclass thereof. Which, in turn, includes the module ActionController::TestProcess.

ActionController::TestProcess provides a get method with different parameters than the get method provided by ActionController::Integration:Session. (Annoying, eh?) The method signature is this:

 def get(action, parameters = nil, session = nil, flash = nil)

Sadly, there is no headers parameter. But at least setting @request.env['HTTP_REFERER'] works.

Solution 4 - Ruby on-Rails

In Rails3 I got the same error:
To get rid of this in corresponding controller provided rescue for "redirect_to :back"

Example: redirect_to :back rescue redirect_to required_path

Solution 5 - Ruby on-Rails

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

In Rails 2.2.2, the above block never ran actual test. Saying that

post :create, { :user => { :email => 'invalid@abc' } }

line did not run. You can simply get rid of setup block and use

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

instead. And it should set the referer

Solution 6 - Ruby on-Rails

Most straight forward solution to the above problem is to make the request with associated headers. Rails will automatically read this header.

post '/your-custom-route', {}, {'HTTP_REFERER': 'http://www.your-allowed-domain.com'}

Solution 7 - Ruby on-Rails

Currently, I used Rails 5.2.1, my approach to stub request referer inside controller is like below:

let(:stub_referer) { some_path(some_param) }
before do
  request.headers.merge! referer: stub_referer
  get :some_action, format: :html
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEthanView Question on Stackoverflow
Solution 1 - Ruby on-RailsJames A. RosenView Answer on Stackoverflow
Solution 2 - Ruby on-RailsFotiosView Answer on Stackoverflow
Solution 3 - Ruby on-Railsrlkw1024View Answer on Stackoverflow
Solution 4 - Ruby on-RailsrupsrayView Answer on Stackoverflow
Solution 5 - Ruby on-RailsBikashView Answer on Stackoverflow
Solution 6 - Ruby on-RailsNerveView Answer on Stackoverflow
Solution 7 - Ruby on-RailsAgung PrasetyoView Answer on Stackoverflow