17
17
use Symfony \Component \HttpFoundation \Session \Session ;
18
18
use Symfony \Component \HttpFoundation \Session \Storage \MockArraySessionStorage ;
19
19
use Symfony \Component \HttpKernel \Controller \ArgumentResolverInterface ;
20
+ use Symfony \Component \HttpKernel \Event \FilterResponseEvent ;
20
21
use Symfony \Component \HttpKernel \HttpKernel ;
21
22
use Symfony \Component \HttpKernel \HttpKernelInterface ;
22
23
use Symfony \Component \HttpKernel \DataCollector \RequestDataCollector ;
@@ -195,6 +196,56 @@ public function testItIgnoresInvalidCallables()
195
196
$ this ->assertSame ('n/a ' , $ c ->getController ());
196
197
}
197
198
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
+
198
249
protected function createRequest ($ routeParams = array ('name ' => 'foo ' ))
199
250
{
200
251
$ request = Request::create ('http://test.com/foo?bar=baz ' );
@@ -269,4 +320,15 @@ public function __invoke()
269
320
{
270
321
throw new \LogicException ('Unexpected method call ' );
271
322
}
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
+ }
272
334
}
0 commit comments