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

[HttpKernel] Add tests for request collector and cookie redirection #25720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
Expand Down Expand Up @@ -195,6 +196,56 @@ public function testItIgnoresInvalidCallables()
$this->assertSame('n/a', $c->getController());
}

public function testItAddsRedirectedAttributesWhenRequestContainsSpecificCookie()
{
$request = $this->createRequest();
$request->cookies->add(array(
'sf_redirect' => '{}',
));

$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();

$c = new RequestDataCollector();
$c->onKernelResponse(new FilterResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST, $this->createResponse()));

$this->assertTrue($request->attributes->get('_redirected'));
}

public function testItSetsARedirectCookieIfTheResponseIsARedirection()
{
$c = new RequestDataCollector();

$response = $this->createResponse();
$response->setStatusCode(302);
$response->headers->set('Location', '/somewhere-else');

$c->collect($request = $this->createRequest(), $response);
$c->lateCollect();

$cookie = $this->getCookieByName($response, 'sf_redirect');

$this->assertNotEmpty($cookie->getValue());
}

public function testItCollectsTheRedirectionAndClearTheCookie()
{
$c = new RequestDataCollector();

$request = $this->createRequest();
$request->attributes->set('_redirected', true);
$request->cookies->add(array(
'sf_redirect' => '{"method": "POST"}',
));

$c->collect($request, $response = $this->createResponse());
$c->lateCollect();

$this->assertEquals('POST', $c->getRedirect()['method']);

$cookie = $this->getCookieByName($response, 'sf_redirect');
$this->assertNull($cookie->getValue());
}

protected function createRequest($routeParams = array('name' => 'foo'))
{
$request = Request::create('http://test.com/foo?bar=baz');
Expand Down Expand Up @@ -269,4 +320,15 @@ public function __invoke()
{
throw new \LogicException('Unexpected method call');
}

private function getCookieByName(Response $response, $name)
{
foreach ($response->headers->getCookies() as $cookie) {
if ($cookie->getName() == $name) {
return $cookie;
}
}

throw new \InvalidArgumentException(sprintf('Cookie named "%s" is not in response', $name));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.