在 Rails 中测试时如何设置 HTTP_REFERER? [英] How do I set HTTP_REFERER when testing in Rails?

查看:48
本文介绍了在 Rails 中测试时如何设置 HTTP_REFERER?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试控制器,但出现此错误.我了解错误,但不知道如何解决.

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

但得到了同样的错误.

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

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

'/sessions/new'

这是他们的意思吗?

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

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.

如果域不是localhost:3000"怎么办?如果是localhost:3001"之类的怎么办?有什么办法可以预测吗?

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

为什么这不起作用:

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

Rails 文档特别说明这就是您设置标头的方式.>

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

推荐答案

他们的建议转化为以下内容:

Their recommendation translates to the following:

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

这篇关于在 Rails 中测试时如何设置 HTTP_REFERER?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆