fixed Client implementation to return the right Response (closes #4475)

This commit is contained in:
Fabien Potencier 2013-04-24 18:19:42 +02:00
parent 902aa4bf22
commit 1005fd1f79
14 changed files with 168 additions and 46 deletions

View File

@ -1,7 +1,8 @@
UPGRADE FROM 2.2 to 2.3 UPGRADE FROM 2.2 to 2.3
======================= =======================
### Form Form
----
* Although this was not officially supported nor documented, it was possible to * Although this was not officially supported nor documented, it was possible to
set the option "validation_groups" to false, resulting in the group "Default" set the option "validation_groups" to false, resulting in the group "Default"
@ -109,7 +110,8 @@
} }
``` ```
### PropertyAccess PropertyAccess
--------------
* PropertyAccessor was changed to continue its search for a property or method * PropertyAccessor was changed to continue its search for a property or method
even if a non-public match was found. This means that the property "author" even if a non-public match was found. This means that the property "author"
@ -161,7 +163,8 @@
} }
``` ```
### DomCrawler DomCrawler
----------
* `Crawler::each()` and `Crawler::reduce()` now return Crawler instances * `Crawler::each()` and `Crawler::reduce()` now return Crawler instances
instead of DomElement instances: instead of DomElement instances:
@ -181,3 +184,31 @@
return $crawler->text(); return $crawler->text();
}); });
``` ```
BrowserKit
----------
* The `Symfony\Component\BrowserKit\Client::getResponse()` method now
**always** returns a `Symfony\Component\BrowserKit\Response` instance. To
get the response from the code that handled the request, use the new
`getOriginResponse()` method instead of `getResponse()`.
* The `Symfony\Component\BrowserKit\Client::getRequest()` method now
**always** returns a `Symfony\Component\BrowserKit\Request` instance. To
get the response from the code that handled the request, use the new
`getOriginRequest()` method instead of `getRequest()`.
HttpKernel
----------
* The `Symfony\Component\HttpKernel\Client::getResponse()` now returns a
`Symfony\Component\BrowserKit\Response` instance instead of a
`Symfony\Component\HttpFoundation\Response` one (because of a change in
BrowserKit -- see above). You can still get the `HttpFoundation` response
by using `getOriginResponse()` instead of `getResponse()`.
* The `Symfony\Component\HttpKernel\Client::getRequest()` now returns a
`Symfony\Component\BrowserKit\Request` instance instead of a
`Symfony\Component\HttpFoundation\Request` one (because of a change in
BrowserKit -- see above). You can still get the `HttpFoundation` response
by using `getOriginRequest()` instead of `getRequest()`.

View File

@ -69,7 +69,7 @@ class Client extends BaseClient
return false; return false;
} }
return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->response); return $this->kernel->getContainer()->get('profiler')->loadProfileFromResponse($this->originResponse);
} }
/** /**

View File

@ -21,6 +21,6 @@ class SubRequestsTest extends WebTestCase
$client = $this->createClient(array('test_case' => 'Session', 'root_config' => 'config.yml')); $client = $this->createClient(array('test_case' => 'Session', 'root_config' => 'config.yml'));
$client->request('GET', 'https://localhost/subrequest/en'); $client->request('GET', 'https://localhost/subrequest/en');
$this->assertEquals('--fr/json--en/html--fr/json--http://localhost/subrequest/fragment/en', $client->getResponse()->getContent()); $this->assertEquals('--fr/json--en/html--fr/json--http://localhost/subrequest/fragment/en', $client->getOriginResponse()->getContent());
} }
} }

View File

@ -19,6 +19,6 @@ class AuthenticationCommencingTest extends WebTestCase
$client->insulate(); $client->insulate();
$client->request('GET', '/secure-but-not-covered-by-access-control'); $client->request('GET', '/secure-but-not-covered-by-access-control');
$this->assertRedirect($client->getResponse(), '/login'); $this->assertRedirect($client->getOriginResponse(), '/login');
} }
} }

View File

@ -29,7 +29,7 @@ class CsrfFormLoginTest extends WebTestCase
$form['user_login[password]'] = 'test'; $form['user_login[password]'] = 'test';
$client->submit($form); $client->submit($form);
$this->assertRedirect($client->getResponse(), '/profile'); $this->assertRedirect($client->getOriginResponse(), '/profile');
$crawler = $client->followRedirect(); $crawler = $client->followRedirect();
@ -44,7 +44,7 @@ class CsrfFormLoginTest extends WebTestCase
$client->click($logoutLinks[0]); $client->click($logoutLinks[0]);
$this->assertRedirect($client->getResponse(), '/'); $this->assertRedirect($client->getOriginResponse(), '/');
} }
/** /**
@ -59,7 +59,7 @@ class CsrfFormLoginTest extends WebTestCase
$form['user_login[_token]'] = ''; $form['user_login[_token]'] = '';
$client->submit($form); $client->submit($form);
$this->assertRedirect($client->getResponse(), '/login'); $this->assertRedirect($client->getOriginResponse(), '/login');
$text = $client->followRedirect()->text(); $text = $client->followRedirect()->text();
$this->assertContains('Invalid CSRF token.', $text); $this->assertContains('Invalid CSRF token.', $text);
@ -79,7 +79,7 @@ class CsrfFormLoginTest extends WebTestCase
$form['user_login[_target_path]'] = '/foo'; $form['user_login[_target_path]'] = '/foo';
$client->submit($form); $client->submit($form);
$this->assertRedirect($client->getResponse(), '/foo'); $this->assertRedirect($client->getOriginResponse(), '/foo');
$text = $client->followRedirect()->text(); $text = $client->followRedirect()->text();
$this->assertContains('Hello johannes!', $text); $this->assertContains('Hello johannes!', $text);
@ -95,13 +95,13 @@ class CsrfFormLoginTest extends WebTestCase
$client->insulate(); $client->insulate();
$client->request('GET', '/protected-resource'); $client->request('GET', '/protected-resource');
$this->assertRedirect($client->getResponse(), '/login'); $this->assertRedirect($client->getOriginResponse(), '/login');
$form = $client->followRedirect()->selectButton('login')->form(); $form = $client->followRedirect()->selectButton('login')->form();
$form['user_login[username]'] = 'johannes'; $form['user_login[username]'] = 'johannes';
$form['user_login[password]'] = 'test'; $form['user_login[password]'] = 'test';
$client->submit($form); $client->submit($form);
$this->assertRedirect($client->getResponse(), '/protected-resource'); $this->assertRedirect($client->getOriginResponse(), '/protected-resource');
$text = $client->followRedirect()->text(); $text = $client->followRedirect()->text();
$this->assertContains('Hello johannes!', $text); $this->assertContains('Hello johannes!', $text);

View File

@ -29,7 +29,7 @@ class FormLoginTest extends WebTestCase
$form['_password'] = 'test'; $form['_password'] = 'test';
$client->submit($form); $client->submit($form);
$this->assertRedirect($client->getResponse(), '/profile'); $this->assertRedirect($client->getOriginResponse(), '/profile');
$text = $client->followRedirect()->text(); $text = $client->followRedirect()->text();
$this->assertContains('Hello johannes!', $text); $this->assertContains('Hello johannes!', $text);
@ -50,7 +50,7 @@ class FormLoginTest extends WebTestCase
$form['_target_path'] = '/foo'; $form['_target_path'] = '/foo';
$client->submit($form); $client->submit($form);
$this->assertRedirect($client->getResponse(), '/foo'); $this->assertRedirect($client->getOriginResponse(), '/foo');
$text = $client->followRedirect()->text(); $text = $client->followRedirect()->text();
$this->assertContains('Hello johannes!', $text); $this->assertContains('Hello johannes!', $text);
@ -66,13 +66,13 @@ class FormLoginTest extends WebTestCase
$client->insulate(); $client->insulate();
$client->request('GET', '/protected_resource'); $client->request('GET', '/protected_resource');
$this->assertRedirect($client->getResponse(), '/login'); $this->assertRedirect($client->getOriginResponse(), '/login');
$form = $client->followRedirect()->selectButton('login')->form(); $form = $client->followRedirect()->selectButton('login')->form();
$form['_username'] = 'johannes'; $form['_username'] = 'johannes';
$form['_password'] = 'test'; $form['_password'] = 'test';
$client->submit($form); $client->submit($form);
$this->assertRedirect($client->getResponse(), '/protected_resource'); $this->assertRedirect($client->getOriginResponse(), '/protected_resource');
$text = $client->followRedirect()->text(); $text = $client->followRedirect()->text();
$this->assertContains('Hello johannes!', $text); $this->assertContains('Hello johannes!', $text);

View File

@ -27,11 +27,11 @@ class LocalizedRoutesAsPathTest extends WebTestCase
$form['_password'] = 'test'; $form['_password'] = 'test';
$client->submit($form); $client->submit($form);
$this->assertRedirect($client->getResponse(), '/'.$locale.'/profile'); $this->assertRedirect($client->getOriginResponse(), '/'.$locale.'/profile');
$this->assertEquals('Profile', $client->followRedirect()->text()); $this->assertEquals('Profile', $client->followRedirect()->text());
$client->request('GET', '/'.$locale.'/logout'); $client->request('GET', '/'.$locale.'/logout');
$this->assertRedirect($client->getResponse(), '/'.$locale.'/'); $this->assertRedirect($client->getOriginResponse(), '/'.$locale.'/');
$this->assertEquals('Homepage', $client->followRedirect()->text()); $this->assertEquals('Homepage', $client->followRedirect()->text());
} }
@ -49,7 +49,7 @@ class LocalizedRoutesAsPathTest extends WebTestCase
$form['_password'] = 'foobar'; $form['_password'] = 'foobar';
$client->submit($form); $client->submit($form);
$this->assertRedirect($client->getResponse(), '/'.$locale.'/login'); $this->assertRedirect($client->getOriginResponse(), '/'.$locale.'/login');
} }
/** /**
@ -61,7 +61,7 @@ class LocalizedRoutesAsPathTest extends WebTestCase
$client->insulate(); $client->insulate();
$client->request('GET', '/'.$locale.'/secure/'); $client->request('GET', '/'.$locale.'/secure/');
$this->assertRedirect($client->getResponse(), '/'.$locale.'/login'); $this->assertRedirect($client->getOriginResponse(), '/'.$locale.'/login');
} }
/** /**
@ -73,7 +73,7 @@ class LocalizedRoutesAsPathTest extends WebTestCase
$client->insulate(); $client->insulate();
$crawler = $client->request('GET', '/'.$locale.'/secure/'); $crawler = $client->request('GET', '/'.$locale.'/secure/');
$this->assertCount(1, $crawler->selectButton('login'), (string) $client->getResponse()); $this->assertCount(1, $crawler->selectButton('login'), (string) $client->getOriginResponse());
} }
public function getLocales() public function getLocales()

View File

@ -22,7 +22,7 @@ class SecurityRoutingIntegrationTest extends WebTestCase
$client->insulate(); $client->insulate();
$client->request('GET', '/protected_resource'); $client->request('GET', '/protected_resource');
$this->assertRedirect($client->getResponse(), '/login'); $this->assertRedirect($client->getOriginResponse(), '/login');
} }
/** /**
@ -38,7 +38,7 @@ class SecurityRoutingIntegrationTest extends WebTestCase
$client->insulate(); $client->insulate();
$client->request('GET', '/unprotected_resource'); $client->request('GET', '/unprotected_resource');
$this->assertEquals(404, $client->getResponse()->getStatusCode(), (string) $client->getResponse()); $this->assertEquals(404, $client->getOriginResponse()->getStatusCode(), (string) $client->getOriginResponse());
} }
/** /**
@ -60,7 +60,7 @@ class SecurityRoutingIntegrationTest extends WebTestCase
$client->request('GET', '/highly_protected_resource'); $client->request('GET', '/highly_protected_resource');
$this->assertNotEquals(404, $client->getResponse()->getStatusCode()); $this->assertNotEquals(404, $client->getOriginResponse()->getStatusCode());
} }
/** /**
@ -93,12 +93,12 @@ class SecurityRoutingIntegrationTest extends WebTestCase
private function assertAllowed($client, $path) { private function assertAllowed($client, $path) {
$client->request('GET', $path); $client->request('GET', $path);
$this->assertEquals(404, $client->getResponse()->getStatusCode()); $this->assertEquals(404, $client->getOriginResponse()->getStatusCode());
} }
private function assertRestricted($client, $path) { private function assertRestricted($client, $path) {
$client->request('GET', $path); $client->request('GET', $path);
$this->assertEquals(302, $client->getResponse()->getStatusCode()); $this->assertEquals(302, $client->getOriginResponse()->getStatusCode());
} }
public function getConfigs() public function getConfigs()

View File

@ -25,7 +25,7 @@ class SwitchUserTest extends WebTestCase
$client->request('GET', '/profile?_switch_user='.$targetUser); $client->request('GET', '/profile?_switch_user='.$targetUser);
$this->assertEquals($expectedStatus, $client->getResponse()->getStatusCode()); $this->assertEquals($expectedStatus, $client->getOriginResponse()->getStatusCode());
$this->assertEquals($expectedUser, $client->getProfile()->getCollector('security')->getUser()); $this->assertEquals($expectedUser, $client->getProfile()->getCollector('security')->getUser());
} }
@ -36,7 +36,7 @@ class SwitchUserTest extends WebTestCase
$client->request('GET', '/profile?_switch_user=user_cannot_switch_1'); $client->request('GET', '/profile?_switch_user=user_cannot_switch_1');
$client->request('GET', '/profile?_switch_user=user_cannot_switch_2'); $client->request('GET', '/profile?_switch_user=user_cannot_switch_2');
$this->assertEquals(500, $client->getResponse()->getStatusCode()); $this->assertEquals(500, $client->getOriginResponse()->getStatusCode());
$this->assertEquals('user_cannot_switch_1', $client->getProfile()->getCollector('security')->getUser()); $this->assertEquals('user_cannot_switch_1', $client->getProfile()->getCollector('security')->getUser());
} }
@ -47,7 +47,7 @@ class SwitchUserTest extends WebTestCase
$client->request('GET', '/profile?_switch_user=user_cannot_switch_1'); $client->request('GET', '/profile?_switch_user=user_cannot_switch_1');
$client->request('GET', '/profile?_switch_user=_exit'); $client->request('GET', '/profile?_switch_user=_exit');
$this->assertEquals(200, $client->getResponse()->getStatusCode()); $this->assertEquals(200, $client->getOriginResponse()->getStatusCode());
$this->assertEquals('user_can_switch', $client->getProfile()->getCollector('security')->getUser()); $this->assertEquals('user_can_switch', $client->getProfile()->getCollector('security')->getUser());
} }

View File

@ -1,6 +1,16 @@
CHANGELOG CHANGELOG
========= =========
2.3.0
-----
* added `Client::getOriginRequest()` and `Client::getOriginResponse()` to
have access to the origin request and response objects
* [BC BREAK] The `Symfony\Component\HttpKernel\Client::getRequest()` method now
returns a `Symfony\Component\BrowserKit\Request` instance
* [BC BREAK] The `Symfony\Component\HttpKernel\Client::request()` method now
always returns a `Symfony\Component\BrowserKit\Response` instance
2.1.0 2.1.0
----- -----

View File

@ -33,7 +33,9 @@ abstract class Client
protected $history; protected $history;
protected $cookieJar; protected $cookieJar;
protected $server; protected $server;
protected $originRequest;
protected $request; protected $request;
protected $originResponse;
protected $response; protected $response;
protected $crawler; protected $crawler;
protected $insulated; protected $insulated;
@ -177,6 +179,19 @@ abstract class Client
return $this->response; return $this->response;
} }
/**
* Returns the origin response instance.
*
* The origin response is the response instance that is returned
* by the code that handles requests.
*
* @return object A response instance
*/
public function getOriginResponse()
{
return $this->originResponse;
}
/** /**
* Returns the current Request instance. * Returns the current Request instance.
* *
@ -189,6 +204,19 @@ abstract class Client
return $this->request; return $this->request;
} }
/**
* Returns the origin Request instance.
*
* The origin request is the request instance that is sent
* to the code that handles requests.
*
* @return object A Request instance
*/
public function getOriginRequest()
{
return $this->originRequest;
}
/** /**
* Clicks on a given link. * Clicks on a given link.
* *
@ -250,39 +278,39 @@ abstract class Client
$server['HTTP_HOST'] = parse_url($uri, PHP_URL_HOST); $server['HTTP_HOST'] = parse_url($uri, PHP_URL_HOST);
$server['HTTPS'] = 'https' == parse_url($uri, PHP_URL_SCHEME); $server['HTTPS'] = 'https' == parse_url($uri, PHP_URL_SCHEME);
$request = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content); $this->request = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content);
$this->request = $this->filterRequest($request); $this->originRequest = $this->filterRequest($this->request);
if (true === $changeHistory) { if (true === $changeHistory) {
$this->history->add($request); $this->history->add($this->request);
} }
if ($this->insulated) { if ($this->insulated) {
$this->response = $this->doRequestInProcess($this->request); $this->originResponse = $this->doRequestInProcess($this->originRequest);
} else { } else {
$this->response = $this->doRequest($this->request); $this->originResponse = $this->doRequest($this->originRequest);
} }
$response = $this->filterResponse($this->response); $this->response = $this->filterResponse($this->originResponse);
$this->cookieJar->updateFromResponse($response, $uri); $this->cookieJar->updateFromResponse($this->response, $uri);
$this->redirect = $response->getHeader('Location'); $this->redirect = $this->response->getHeader('Location');
if ($this->followRedirects && $this->redirect) { if ($this->followRedirects && $this->redirect) {
return $this->crawler = $this->followRedirect(); return $this->crawler = $this->followRedirect();
} }
return $this->crawler = $this->createCrawlerFromContent($request->getUri(), $response->getContent(), $response->getHeader('Content-Type')); return $this->crawler = $this->createCrawlerFromContent($this->request->getUri(), $this->response->getContent(), $this->response->getHeader('Content-Type'));
} }
/** /**
* Makes a request in another process. * Makes a request in another process.
* *
* @param Request $request A Request instance * @param object $request An origin request instance
* *
* @return Response A Response instance * @return object An origin response instance
* *
* @throws \RuntimeException When processing returns exit code * @throws \RuntimeException When processing returns exit code
*/ */
@ -302,9 +330,9 @@ abstract class Client
/** /**
* Makes a request. * Makes a request.
* *
* @param Request $request A Request instance * @param object $request An origin request instance
* *
* @return Response A Response instance * @return object An origin response instance
*/ */
abstract protected function doRequest($request); abstract protected function doRequest($request);

View File

@ -17,6 +17,10 @@ use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\BrowserKit\Request; use Symfony\Component\BrowserKit\Request;
use Symfony\Component\BrowserKit\Response; use Symfony\Component\BrowserKit\Response;
class SpecialResponse extends Response
{
}
class TestClient extends Client class TestClient extends Client
{ {
protected $nextResponse = null; protected $nextResponse = null;
@ -44,6 +48,15 @@ class TestClient extends Client
return $response; return $response;
} }
protected function filterResponse($response)
{
if ($response instanceof SpecialResponse) {
return new Response($response->getContent(), $response->getStatus(), $response->getHeaders());
}
return $response;
}
protected function getScript($request) protected function getScript($request)
{ {
$r = new \ReflectionClass('Symfony\Component\BrowserKit\Response'); $r = new \ReflectionClass('Symfony\Component\BrowserKit\Response');
@ -100,6 +113,21 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$client->request('GET', 'http://example.com/'); $client->request('GET', 'http://example.com/');
$this->assertEquals('foo', $client->getResponse()->getContent(), '->getCrawler() returns the Response of the last request'); $this->assertEquals('foo', $client->getResponse()->getContent(), '->getCrawler() returns the Response of the last request');
$this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getResponse(), '->getCrawler() returns the Response of the last request');
}
/**
* @covers Symfony\Component\BrowserKit\Client::getResponse
*/
public function testgetOriginResponse()
{
$client = new TestClient();
$client->setNextResponse(new SpecialResponse('foo'));
$client->request('GET', 'http://example.com/');
$this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getResponse());
$this->assertNotInstanceOf('Symfony\Component\BrowserKit\Tests\SpecialResponse', $client->getResponse());
$this->assertInstanceOf('Symfony\Component\BrowserKit\Tests\SpecialResponse', $client->getOriginResponse());
} }
public function testGetContent() public function testGetContent()

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\HttpKernel;
use Symfony\Component\HttpFoundation\File\UploadedFile; use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\BrowserKit\Client as BaseClient; use Symfony\Component\BrowserKit\Client as BaseClient;
use Symfony\Component\BrowserKit\Request as DomRequest; use Symfony\Component\BrowserKit\Request as DomRequest;
use Symfony\Component\BrowserKit\Response as DomResponse; use Symfony\Component\BrowserKit\Response as DomResponse;
@ -49,6 +50,26 @@ class Client extends BaseClient
$this->followRedirects = false; $this->followRedirects = false;
} }
/**
* {@inheritdoc}
*
* @return Request A Request instance
*/
public function getOriginRequest()
{
return parent::getOriginRequest();
}
/**
* {@inheritdoc}
*
* @return Response A Response instance
*/
public function getOriginResponse()
{
return parent::getOriginResponse();
}
/** /**
* Makes a request. * Makes a request.
* *

View File

@ -35,13 +35,17 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$client->request('GET', '/'); $client->request('GET', '/');
$this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request'); $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
$this->assertInstanceOf('Symfony\Component\BrowserKit\Request', $client->getRequest());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $client->getOriginRequest());
$this->assertInstanceOf('Symfony\Component\BrowserKit\Response', $client->getResponse());
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $client->getOriginResponse());
$client->request('GET', 'http://www.example.com/'); $client->request('GET', 'http://www.example.com/');
$this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request'); $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
$this->assertEquals('www.example.com', $client->getRequest()->getHost(), '->doRequest() uses the request handler to make the request'); $this->assertEquals('www.example.com', $client->getOriginRequest()->getHost(), '->doRequest() uses the request handler to make the request');
$client->request('GET', 'http://www.example.com/?parameter=http://google.com'); $client->request('GET', 'http://www.example.com/?parameter=http://google.com');
$this->assertEquals('http://www.example.com/?parameter='.urlencode('http://google.com'), $client->getRequest()->getUri(), '->doRequest() uses the request handler to make the request'); $this->assertEquals('http://www.example.com/?parameter='.urlencode('http://google.com'), $client->getOriginRequest()->getUri(), '->doRequest() uses the request handler to make the request');
} }
public function testGetScript() public function testGetScript()
@ -120,7 +124,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
foreach ($files as $file) { foreach ($files as $file) {
$client->request('POST', '/', array(), array('foo' => $file)); $client->request('POST', '/', array(), array('foo' => $file));
$files = $client->getRequest()->files->all(); $files = $client->getOriginRequest()->files->all();
$this->assertCount(1, $files); $this->assertCount(1, $files);
@ -159,7 +163,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
$client->request('POST', '/', array(), array($file)); $client->request('POST', '/', array(), array($file));
$files = $client->getRequest()->files->all(); $files = $client->getOriginRequest()->files->all();
$this->assertCount(1, $files); $this->assertCount(1, $files);