Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b82aa6f

Browse filesBrowse files
committed
Add tests for the HttpKernel request collector and redirection via cookies
1 parent 83f2579 commit b82aa6f
Copy full SHA for b82aa6f

File tree

1 file changed

+62
-0
lines changed
Filter options

1 file changed

+62
-0
lines changed

‎src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/DataCollector/RequestDataCollectorTest.php
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\HttpFoundation\Session\Session;
1818
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
1919
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
20+
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
2021
use Symfony\Component\HttpKernel\HttpKernel;
2122
use Symfony\Component\HttpKernel\HttpKernelInterface;
2223
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
@@ -195,6 +196,56 @@ public function testItIgnoresInvalidCallables()
195196
$this->assertSame('n/a', $c->getController());
196197
}
197198

199+
public function testItAddsRedirectedAttributesWhenRequestContainsSpecificCookie()
200+
{
201+
$request = $this->createRequest();
202+
$request->cookies->add([
203+
'sf_redirect' => '{}',
204+
]);
205+
206+
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
207+
208+
$c = new RequestDataCollector();
209+
$c->onKernelResponse(new FilterResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $this->createResponse()));
210+
211+
$this->assertEquals(true, $request->attributes->get('_redirected'));
212+
}
213+
214+
public function testItSetsARedirectCookieIfTheResponseIsARedirection()
215+
{
216+
$c = new RequestDataCollector();
217+
218+
$response = $this->createResponse();
219+
$response->setStatusCode(302);
220+
$response->headers->set('Location', '/somewhere-else');
221+
222+
$c->collect($request = $this->createRequest(), $response);
223+
$c->lateCollect();
224+
225+
$cookie = $this->getCookieByName($response, 'sf_redirect');
226+
227+
$this->assertNotEmpty($cookie->getValue());
228+
}
229+
230+
public function testItCollectsTheRedirectionAndClearTheCookie()
231+
{
232+
$c = new RequestDataCollector();
233+
234+
$request = $this->createRequest();
235+
$request->attributes->set('_redirected', true);
236+
$request->cookies->add([
237+
'sf_redirect' => '{"method": "POST"}',
238+
]);
239+
240+
$c->collect($request, $response = $this->createResponse());
241+
$c->lateCollect();
242+
243+
$this->assertEquals('POST', $c->getRedirect()['method']);
244+
245+
$cookie = $this->getCookieByName($response, 'sf_redirect');
246+
$this->assertNull($cookie->getValue());
247+
}
248+
198249
protected function createRequest($routeParams = array('name' => 'foo'))
199250
{
200251
$request = Request::create('http://test.com/foo?bar=baz');
@@ -269,4 +320,15 @@ public function __invoke()
269320
{
270321
throw new \LogicException('Unexpected method call');
271322
}
323+
324+
private function getCookieByName(Response $response, $name)
325+
{
326+
foreach ($response->headers->getCookies() as $cookie) {
327+
if ($cookie->getName() == $name) {
328+
return $cookie;
329+
}
330+
}
331+
332+
throw new \InvalidArgumentException(sprintf('Cookie named "%s" is not in response', $name));
333+
}
272334
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.