mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Fixed VisitsTracker to take into account the X-Forwarded-For header in case the server is behind a load balabncer or proxy
This commit is contained in:
@@ -45,6 +45,29 @@ class VisitsTrackerTest extends TestCase
|
||||
$this->visitsTracker->track($shortCode, ServerRequestFactory::fromGlobals());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function trackUsesForwardedForHeaderIfPresent()
|
||||
{
|
||||
$shortCode = '123ABC';
|
||||
$test = $this;
|
||||
$repo = $this->prophesize(EntityRepository::class);
|
||||
$repo->findOneBy(['shortCode' => $shortCode])->willReturn(new ShortUrl());
|
||||
|
||||
$this->em->getRepository(ShortUrl::class)->willReturn($repo->reveal())->shouldBeCalledTimes(1);
|
||||
$this->em->persist(Argument::any())->will(function ($args) use ($test) {
|
||||
/** @var Visit $visit */
|
||||
$visit = $args[0];
|
||||
$test->assertEquals('4.3.2.1', $visit->getRemoteAddr());
|
||||
})->shouldBeCalledTimes(1);
|
||||
$this->em->flush()->shouldBeCalledTimes(1);
|
||||
|
||||
$this->visitsTracker->track($shortCode, ServerRequestFactory::fromGlobals(
|
||||
['REMOTE_ADDR' => '1.2.3.4']
|
||||
)->withHeader('X-Forwarded-For', '4.3.2.1,99.99.99.99'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user