在 Ruby on Rails 中更改 request.remote_ip 的值

发布于 2024-08-17 05:22:33 字数 135 浏览 7 评论 0原文

出于测试目的,我想更改 request.remote_ip 的返回值。在我的开发机器上时,它总是返回 127.0.0.1,因为它应该是这样,但我想给自己提供不同的假 IP 来测试我的应用程序的正确行为,而无需先将其部署到实时服务器!

谢谢。

For test purposes I want to change the return value of request.remote_ip. While being on my development machine it returns always 127.0.0.1 as it should but I would like to give myself different fake IPs to test the correct behavior of my app without deploying it to an live server first!

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

夏九 2024-08-24 05:22:33

如果您希望在整个应用程序中使用此功能,那么覆盖 app/helpers/application_helper.rb 中的remote_ip 方法可能会更好/更容易:

class ActionDispatch::Request #rails 2: ActionController::Request
  def remote_ip
    '1.2.3.4'
  end
end

并且 1.2.3.4 地址随处可用

If you want this functionality in your whole application, it might be better/easier to override the remote_ip method in your app/helpers/application_helper.rb:

class ActionDispatch::Request #rails 2: ActionController::Request
  def remote_ip
    '1.2.3.4'
  end
end

And the 1.2.3.4 address is available everywhere

梦晓ヶ微光ヅ倾城 2024-08-24 05:22:33

对于集成测试,这适用于 Rails 5:

get "/path", params: { }, headers: { "REMOTE_ADDR" => "1.2.3.4" }

For integration tests, this works with rails 5:

get "/path", params: { }, headers: { "REMOTE_ADDR" => "1.2.3.4" }
静待花开 2024-08-24 05:22:33

您可以使用以下方法修改请求对象:

request = ActionController::Request.new('REMOTE_ADDR' => '1.2.3.4')

request.remote_ip 现在返回 1.2.3.4

You can modify the request object using:

request = ActionController::Request.new('REMOTE_ADDR' => '1.2.3.4')

request.remote_ip now returns 1.2.3.4

遇见了你 2024-08-24 05:22:33

您可以通过在测试环境中为通常未定义的remote_ip 值创建一个变元来进行一些欺骗。

例如,使用以下内容更改 test/test_helper.rb 内部的类:

class ActionController::TestRequest
  def remote_ip=(value)
    @env['REMOTE_ADDR'] = value.to_s
  end
end

然后,在测试期间,您可以根据需要重新分配:

def test_something
  @request.remote_ip = '1.2.3.4'
end

这可以在单独的测试中完成,也可以在您的设置例程中完成,只要合适即可。

我之前在编写验证 IP 禁止、地理位置等的功能测试时必须使用它。

You can cheat a bit by making a mutator for the remote_ip value in the test environment which is normally not defined.

For instance, alter the class inside of test/test_helper.rb with the following:

class ActionController::TestRequest
  def remote_ip=(value)
    @env['REMOTE_ADDR'] = value.to_s
  end
end

Then, during your testing you can reassign as required:

def test_something
  @request.remote_ip = '1.2.3.4'
end

This can be done either in the individual test, or within your setup routine, wherever is appropriate.

I have had to use this before when writing functional tests that verify IP banning, geolocation, etc.

榆西 2024-08-24 05:22:33

Rails 4.0.1 RC。经过一个小时的搜索,在挖掘代码时发现了这个简单的解决方案:)

get '/', {}, { 'REMOTE_ADDR' => '1.2.3.4' }

rails 4.0.1 rc. After hour of searching found this simple solution while digging to code :)

get '/', {}, { 'REMOTE_ADDR' => '1.2.3.4' }
‘画卷フ 2024-08-24 05:22:33

我现在最终要做的就是将此代码放在 config/environments/development.rb 文件的末尾,以确保它仅在开发过程中执行,

# fake IP for manuel testing
class ActionController::Request
  def remote_ip
    "1.2.3.4"
  end
end

因此,当服务器启动。每次更改该值都必须重新启动服务器!

What I ended up doing now was to put this code in the end of the config/environments/development.rb file to make sure it's only executed while in development

# fake IP for manuel testing
class ActionController::Request
  def remote_ip
    "1.2.3.4"
  end
end

So this sets remote_ip to 1.2.3.4 when the server starts. Everytime you change the value you have to restart the server!

白昼 2024-08-24 05:22:33

这个答案仅适用于rails3(我在尝试回答rails 3的类似问题时发现了这个答案),

所以我将把它发布在这里,以防有人试图在Rails3 env

class ActionDispatch::Request
  def remote_ip
    '1.2.3.4'
  end
end

HTH中做同样的事情

This answer is only works for rails3 (I found this answer when trying to answer a similar question for rails 3),

So I will post it here in case if someone is trying to do the same thing in Rails3 env

class ActionDispatch::Request
  def remote_ip
    '1.2.3.4'
  end
end

HTH

银河中√捞星星 2024-08-24 05:22:33

使用带有 Rspec 的 Rails7,我可以通过将以下 before 部分添加到我的测试请求:

  before :each do
    allow_any_instance_of(ActionDispatch::Request).to receive(:remote_addr).and_return('1.2.3.4')
  end

为了在上下文中更多地了解这一点,整个请求变为:

RSpec.describe "Foos", type: :request do
  before :each do
    allow_any_instance_of(ActionDispatch::Request).to receive(:remote_addr).and_return('1.2.3.4')
  end
  describe "GET /status" do
    it "returns http success" do
      get "/foo/status"
      expect(response).to have_http_status(:success)
    end
  end
end

然后,在我的 Foo 控制器 status 方法内,request.remote_ip 返回测试中分配的任何值。

Using Rails7 with Rspec, I was able to change the value of request.remote_ip to return 1.2.3.4 by adding the following before section to my test request:

  before :each do
    allow_any_instance_of(ActionDispatch::Request).to receive(:remote_addr).and_return('1.2.3.4')
  end

To see this more in context, the whole request becomes:

RSpec.describe "Foos", type: :request do
  before :each do
    allow_any_instance_of(ActionDispatch::Request).to receive(:remote_addr).and_return('1.2.3.4')
  end
  describe "GET /status" do
    it "returns http success" do
      get "/foo/status"
      expect(response).to have_http_status(:success)
    end
  end
end

Then, inside of my Foo controllers status method, request.remote_ip returns whatever value assigned in the test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文