在 Rails 中测试时如何设置 HTTP_REFERER?

我试图测试一个控制器,我得到了这个错误。我知道这个错误,但不知道如何修正。

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"].

具体在哪里? 我试过这个:

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

但也出现了同样的错误。

具体用什么来指定它? 我想我希望它返回的视图的 URI 是:

'/sessions/new'

是这个意思吗?


好吧,原来他们的意思是这样做:

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

有人能告诉我哪里有记录吗? 我想了解一下这些信息的背景。

如果域不是“ localhost: 3000”怎么办? 如果域是“ localhost: 3001”或其他什么怎么办? 有什么办法可以预测到吗?

为什么这个不起作用:

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

Rails 对 明确地说进行文档记录,这就是设置标题的方法。

35271 次浏览

Their recommendation translates to the following:

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

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.

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

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

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'}

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