如何在 Mojolicious 应用程序的单元测试中伪造客户端 IP 地址? [英] How do I fake the client IP address in a unit test for a Mojolicious app?

查看:43
本文介绍了如何在 Mojolicious 应用程序的单元测试中伪造客户端 IP 地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Mojolicious 应用程序中,我需要使用客户端的 IP 地址 ($c->tx->remote_address) 来限制服务的速率.这很好用.

In my Mojolicious app I need to use the client's IP address ($c->tx->remote_address) for rate limiting of a service. This works well.

我现在正在尝试为此功能构建单元测试,但我在测试中无法伪造客户端的 IP.

I am now trying to build a unit test for this feature, but I am having trouble faking the client's IP in my tests.

首先我认为 local_address 在 Mojo::UserAgent 可能会做我想做的事,但这就是用户代理在本地绑定应用程序的地方,更改它会破坏一切,因为它再也找不到应用程序了.

First I thought that local_address in Mojo::UserAgent might do what I want, but that's where the user agent binds the application locally, and changing it breaks everything because it cannot find the app any more.

然后我尝试使用 Sub::Override 来替换 remote_address in Mojo::Transaction,但是当我这样做时,这已经适用于客户端$t->post_ok,它尝试向不存在的 IP 发送请求,因为客户端的远程地址是服务器的地址,而我遇到了等待阻塞请求永远不会成功,因为它想要的服务器不存在.

I then tried using Sub::Override to replace the remote_address in Mojo::Transaction, but that already applies to the client when I do $t->post_ok, it tries sending a request to the IP that doesn't exist because the remote address on the client side is the server's address, and I am stuck with a waiting blocking request that will never succeed because the server it wants does not exist.

您可以使用以下 MCVE 进行尝试.预期结果是测试通过.

You can use the following MCVE to try around. The expected result is for the tests to pass.

use strict;
use warnings;
use Test::More;
use Test::Mojo;
use Mojolicious::Lite;

get '/foo' => sub { my $c = shift; $c->render( text => $c->tx->remote_address ) };

my $t = Test::Mojo->new;
$t->get_ok('/foo')->content_like(qr/\Q127.0.0.1/);

# TODO change client IP address to 10.1.1.1
# in a way that the /foo route sees it
$t->get_ok('/foo')->content_like(qr/\Q10.1.1.1/);

done_testing;

我知道如何使用 Catalyst 和 Dancer(或其他基于 Test::Plack 的系统)来做到这一点,但这些方法在这里不起作用.

I know how to do this with Catalyst and Dancer (or other Test::Plack based systems), but those approaches don't work here.

推荐答案

Mojolicious 的作者在 IRC 上指出要查看 Mojo dist 中针对 X-Forwarded-For 标头的单元测试实施,所以我做到了.

The author of Mojolicious pointed out on IRC to look at the unit tests in the Mojo dist for the X-Forwarded-For header implementation, so I did.

我们需要在单元测试中将 $ENV{MOJO_REVERSE_PROXY} 设置为真值并重新启动服务器,然后发送一个 X-Forwarded-For 标头新的 IP 地址和一切都会正常工作.

We need to set the $ENV{MOJO_REVERSE_PROXY} to a true value in the unit test and restart the server, then send an X-Forwarded-For header with the new IP address and things will just work.

use strict;
use warnings;
use Test::More;
use Test::Mojo;
use Mojolicious::Lite;

get '/foo' => sub { my $c = shift; $c->render( text => $c->tx->remote_address ) };

my $t = Test::Mojo->new;
$t->get_ok('/foo')->content_like(qr/\Q127.0.0.1/);

{
    local $ENV{MOJO_REVERSE_PROXY} = 1;
    $t->ua->server->restart;
    $t->get_ok( '/foo' => { 'X-Forwarded-For' => '10.1.1.1' } )->content_like(qr/\Q10.1.1.1/);
}

done_testing;

现在测试通过了.

ok 1 - GET /foo
ok 2 - content is similar
ok 3 - GET /foo
ok 4 - content is similar
1..4

这篇关于如何在 Mojolicious 应用程序的单元测试中伪造客户端 IP 地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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