Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current URL after a redirect in a Symfony2 WebTestCase?

Using the Symfony2 WebTestCase, I have the following test :

$client->request('GET', '/');
$this->assertTrue($client->getResponse() instanceof RedirectResponse);

$crawler = $client->followRedirect();

I would like to test the new url, after the redirection ends with /login but I have found no way to access the new URL.

like image 830
i.am.michiel Avatar asked Jun 15 '12 08:06

i.am.michiel


4 Answers

You can get the current URL with $client->getResponse()->headers->get('location'), and assert it ends with /login using assertRegExp().

$this->assertRegExp('/\/login$/', $client->getResponse()->headers->get('location'));
like image 78
Samy Dindane Avatar answered Nov 03 '22 09:11

Samy Dindane


Just tried Samy Dindane solution without success (maybe something changed from 2.0)

Anyway, in 2.1, I was able to retrieve current URL in test with:

$client->getRequest()->getUri()

Then, you can use assertRegexp().

like image 25
Massimiliano Arione Avatar answered Nov 03 '22 10:11

Massimiliano Arione


You can also do something like this (Symfony 2.3)

$client->getHistory()->current()->getUri()
like image 14
Gabriel Filipiak Avatar answered Nov 03 '22 08:11

Gabriel Filipiak


You can add this row $client->getResponse()->getTargetUrl() before $client->followRedirect() to get the url where you will be redirected

like image 8
Victor Bredihin Avatar answered Nov 03 '22 10:11

Victor Bredihin