Ruby-on-rails 在 Rails 中测试时如何设置 HTTP_REFERER?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/542856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I set HTTP_REFERER when testing in Rails?
提问by Ethan
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:
用什么来指定它,究竟是什么?我想我希望它返回到的视图的 URI:
'/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?
如果域不是“localhost:3000”怎么办?如果是“localhost:3001”之类的怎么办?有什么办法可以预测吗?
Why doesn't this work:
为什么这不起作用:
setup { post :create, { :user => { :email => 'invalid@abc' } },
{ 'referer' => '/sessions/new' } }
The Rails docs specifically saythat's how you set the headers.
Rails 文档特别说明这就是您设置标题的方式。
回答by James A. Rosen
Their recommendation translates to the following:
他们的建议转化为以下内容:
setup do
@request.env['HTTP_REFERER'] = 'http://test.com/sessions/new'
post :create, { :user => { :email => 'invalid@abc' } }
end
回答by Fotios
The accepted answer doesn't work for integration tests because the @requestvariable doesn't exist.
接受的答案不适用于集成测试,因为该@request变量不存在。
According to RailsGuides, you can pass headers to the helpers.
根据RailsGuides,您可以将标头传递给助手。
Rails <= 4:
导轨 <= 4:
test "blah" do
get root_path, {}, {'HTTP_REFERER' => 'http://foo.com'}
...
end
Rails >= 5:
导轨 >= 5:
test "blah" do
get root_path, params: { id: 12 }, headers: { "HTTP_REFERER" => "http://foo.com" }
...
end
回答by rlkw1024
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 todocuments a different class than the one you're probably using.
它不起作用,因为您链接到的 Rails 文档记录的类与您可能正在使用的类不同。
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::TestCaseor a subclass thereof. Which, in turn, includes the module ActionController::TestProcess.
您链接到ActionController::Integration:Session. 我猜您正在编写功能测试(如果您使用 Test::Unit)或控制器测试(如果您使用 Rspec)。无论哪种方式,您可能都在使用ActionController::TestCase或其子类。反过来,其中包括模块ActionController::TestProcess.
ActionController::TestProcessprovides a getmethod with different parameters than the getmethod provided by ActionController::Integration:Session. (Annoying, eh?) The method signature is this:
ActionController::TestProcess提供了一个get具有不同参数比方法get通过提供的方法ActionController::Integration:Session。(烦人,嗯?)方法签名是这样的:
def get(action, parameters = nil, session = nil, flash = nil)
Sadly, there is no headers parameter. But at least setting @request.env['HTTP_REFERER']works.
遗憾的是,没有 headers 参数。但至少设置@request.env['HTTP_REFERER']有效。
回答by rupsray
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
在 Rails3 中,我遇到了同样的错误:
在相应的控制器中摆脱这个为“redirect_to :back”提供了救援
示例:redirect_to :back rescue redirect_to required_path
回答by Nerve
Most straight forward solution to the above problem is to make the request with associated headers. Rails will automatically read this header.
上述问题最直接的解决方案是使用关联的标头发出请求。Rails 会自动读取这个头文件。
post '/your-custom-route', {}, {'HTTP_REFERER': 'http://www.your-allowed-domain.com'}
回答by Agung Prasetyo
Currently, I used Rails 5.2.1, my approach to stub request referer inside controller is like below:
目前,我使用 Rails 5.2.1,我在控制器内部存根请求引用的方法如下:
let(:stub_referer) { some_path(some_param) }
before do
request.headers.merge! referer: stub_referer
get :some_action, format: :html
end
回答by Agung Prasetyo
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
在 Rails 2.2.2 中,上述块从未运行过实际测试。这么说
post :create, { :user => { :email => 'invalid@abc' } }
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
反而。它应该设置引用者

