Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit8ab89ce

Browse files
committed
minor#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 (#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 (#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 parents93755ab +7b4f5a1 commit8ab89ce

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
useSymfony\Component\HttpFoundation\Session\Session;
1818
useSymfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
1919
useSymfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
20+
useSymfony\Component\HttpKernel\Event\FilterResponseEvent;
2021
useSymfony\Component\HttpKernel\HttpKernel;
2122
useSymfony\Component\HttpKernel\HttpKernelInterface;
2223
useSymfony\Component\HttpKernel\DataCollector\RequestDataCollector;
@@ -195,6 +196,56 @@ public function testItIgnoresInvalidCallables()
195196
$this->assertSame('n/a',$c->getController());
196197
}
197198

199+
publicfunctiontestItAddsRedirectedAttributesWhenRequestContainsSpecificCookie()
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 =newRequestDataCollector();
209+
$c->onKernelResponse(newFilterResponseEvent($kernel,$request, HttpKernelInterface::MASTER_REQUEST,$this->createResponse()));
210+
211+
$this->assertTrue($request->attributes->get('_redirected'));
212+
}
213+
214+
publicfunctiontestItSetsARedirectCookieIfTheResponseIsARedirection()
215+
{
216+
$c =newRequestDataCollector();
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+
publicfunctiontestItCollectsTheRedirectionAndClearTheCookie()
231+
{
232+
$c =newRequestDataCollector();
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
protectedfunctioncreateRequest($routeParams =array('name' =>'foo'))
199250
{
200251
$request = Request::create('http://test.com/foo?bar=baz');
@@ -269,4 +320,15 @@ public function __invoke()
269320
{
270321
thrownew \LogicException('Unexpected method call');
271322
}
323+
324+
privatefunctiongetCookieByName(Response$response,$name)
325+
{
326+
foreach ($response->headers->getCookies()as$cookie) {
327+
if ($cookie->getName() ==$name) {
328+
return$cookie;
329+
}
330+
}
331+
332+
thrownew \InvalidArgumentException(sprintf('Cookie named "%s" is not in response',$name));
333+
}
272334
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp