Skip to content

Commit

Permalink
Merge pull request #418 from FriendsOfSymfony/test-matcher
Browse files Browse the repository at this point in the history
add test for anonymous request matcher
  • Loading branch information
dbu authored Mar 27, 2018
2 parents 6c2c567 + 9606059 commit 9f310cb
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions tests/Unit/UserContext/AnonymousRequestMatcherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/*
* This file is part of the FOSHttpCache package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\HttpCache\Tests\Unit\UserContext;

use FOS\HttpCache\UserContext\AnonymousRequestMatcher;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;

class AnonymousRequestMatcherTest extends TestCase
{
use MockeryPHPUnitIntegration;

public function testMatchAnonymousRequest()
{
$request = new Request();

$requestMatcher = new AnonymousRequestMatcher([
'user_identifier_headers' => ['Cookie', 'Authorization'],
'session_name_prefix' => false,
]);

$this->assertTrue($requestMatcher->matches($request));
}

public function testNoMatchIfCookie()
{
$request = new Request();
$request->headers->set('Cookie', 'foo=bar');
$request->cookies->set('foo', 'bar');

$requestMatcher = new AnonymousRequestMatcher([
'user_identifier_headers' => ['Cookie', 'Authorization'],
'session_name_prefix' => false,
]);

$this->assertFalse($requestMatcher->matches($request));
}

public function testNoMatchIfSession()
{
$request = new Request();
$request->headers->set('Cookie', 'PHPSESSID7e476fc9f29f69d2ad6f11dbcd663b42=25f6d9c5a843e3c948cd26902385a527');
$request->cookies->set('PHPSESSID7e476fc9f29f69d2ad6f11dbcd663b42', '25f6d9c5a843e3c948cd26902385a527');

$requestMatcher = new AnonymousRequestMatcher([
'user_identifier_headers' => ['Authorization'],
'session_name_prefix' => 'PHPSESSID',
]);

$this->assertFalse($requestMatcher->matches($request));
}

public function testMatchIfNoSessionCookie()
{
$request = new Request();
$request->headers->set('Cookie', 'foo=bar');
$request->cookies->set('foo', 'bar');

$requestMatcher = new AnonymousRequestMatcher([
'user_identifier_headers' => ['Authorization'],
'session_name_prefix' => 'PHPSESSID',
]);

$this->assertTrue($requestMatcher->matches($request));
}

public function testNoMatchIfAuthenticationHeader()
{
$request = new Request();
$request->headers->set('Authorization', 'foo: bar');

$requestMatcher = new AnonymousRequestMatcher([
'user_identifier_headers' => ['Cookie', 'Authorization'],
'session_name_prefix' => false,
]);

$this->assertFalse($requestMatcher->matches($request));
}
}

0 comments on commit 9f310cb

Please sign in to comment.