Skip to content

Commit 8ab89ce

Browse files
committed
minor symfony#25720 [HttpKernel] Add tests for request collector and cookie redirection (sroze)
This PR was merged into the 3.4 branch. Discussion ---------- [HttpKernel] Add tests for request collector and cookie redirection | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes (symfony#25719) | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | ø Not that I felt bad doing a PR without tests (symfony#25719) but this one adds tests to be sure we stabilize this cookie-based redirection. Commits ------- 7b4f5a1 Add tests for the HttpKernel request collector and redirection via cookies
2 parents 93755ab + 7b4f5a1 commit 8ab89ce

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

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

Lines 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(array(
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->assertTrue($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(array(
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)