-
-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HttpKernel] Fix session handling: decouple "save" from setting respo…
…nse "private"
- Loading branch information
1 parent
f95ac4f
commit f8727b8
Showing
8 changed files
with
130 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/Symfony/Component/HttpKernel/Tests/EventListener/SessionListenerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Tests\EventListener; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\DependencyInjection\Container; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Session\Session; | ||
use Symfony\Component\HttpKernel\Event\GetResponseEvent; | ||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener; | ||
use Symfony\Component\HttpKernel\EventListener\SessionListener; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
|
||
class SessionListenerTest extends TestCase | ||
{ | ||
public function testOnlyTriggeredOnMasterRequest() | ||
{ | ||
$listener = $this->getMockForAbstractClass(AbstractSessionListener::class); | ||
$event = $this->getMockBuilder(GetResponseEvent::class)->disableOriginalConstructor()->getMock(); | ||
$event->expects($this->once())->method('isMasterRequest')->willReturn(false); | ||
$event->expects($this->never())->method('getRequest'); | ||
|
||
// sub request | ||
$listener->onKernelRequest($event); | ||
} | ||
|
||
public function testSessionIsSet() | ||
{ | ||
$session = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock(); | ||
|
||
$container = new Container(); | ||
$container->set('session', $session); | ||
|
||
$request = new Request(); | ||
$listener = new SessionListener($container); | ||
|
||
$event = $this->getMockBuilder(GetResponseEvent::class)->disableOriginalConstructor()->getMock(); | ||
$event->expects($this->once())->method('isMasterRequest')->willReturn(true); | ||
$event->expects($this->once())->method('getRequest')->willReturn($request); | ||
|
||
$listener->onKernelRequest($event); | ||
|
||
$this->assertTrue($request->hasSession()); | ||
$this->assertSame($session, $request->getSession()); | ||
} | ||
|
||
public function testResponseIsPrivate() | ||
{ | ||
$session = $this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock(); | ||
$session->expects($this->once())->method('isStarted')->willReturn(false); | ||
$session->expects($this->once())->method('hasBeenStarted')->willReturn(true); | ||
|
||
$container = new Container(); | ||
$container->set('session', $session); | ||
|
||
$listener = new SessionListener($container); | ||
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->disableOriginalConstructor()->getMock(); | ||
|
||
$request = new Request(); | ||
$response = new Response(); | ||
$listener->onKernelRequest(new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST)); | ||
$listener->onKernelResponse(new FilterResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $response)); | ||
|
||
$this->assertTrue($response->headers->hasCacheControlDirective('private')); | ||
$this->assertTrue($response->headers->hasCacheControlDirective('must-revalidate')); | ||
$this->assertSame('0', $response->headers->getCacheControlDirective('max-age')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters