ClientTest.php 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\HttpKernel;
  11. use Symfony\Component\HttpKernel\Client;
  12. use Symfony\Component\HttpKernel\HttpKernel;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpFoundation\Cookie;
  16. require_once __DIR__.'/TestHttpKernel.php';
  17. class TestClient extends Client
  18. {
  19. protected function getScript($request)
  20. {
  21. $script = parent::getScript($request);
  22. $script = preg_replace('/(\->register\(\);)/', "$0\nrequire_once '".__DIR__."/TestHttpKernel.php';", $script);
  23. return $script;
  24. }
  25. }
  26. class ClientTest extends \PHPUnit_Framework_TestCase
  27. {
  28. public function testDoRequest()
  29. {
  30. $client = new Client(new TestHttpKernel());
  31. $client->request('GET', '/');
  32. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
  33. $client->request('GET', 'http://www.example.com/');
  34. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->doRequest() uses the request handler to make the request');
  35. $this->assertEquals('www.example.com', $client->getRequest()->getHost(), '->doRequest() uses the request handler to make the request');
  36. $client->request('GET', 'http://www.example.com/?parameter=http://google.com');
  37. $this->assertEquals('http://www.example.com/?parameter='.urlencode('http://google.com'), $client->getRequest()->getUri(), '->doRequest() uses the request handler to make the request');
  38. }
  39. public function testGetScript()
  40. {
  41. $client = new TestClient(new TestHttpKernel());
  42. $client->insulate();
  43. $client->request('GET', '/');
  44. $this->assertEquals('Request: /', $client->getResponse()->getContent(), '->getScript() returns a script that uses the request handler to make the request');
  45. }
  46. public function testFilterResponseConvertsCookies()
  47. {
  48. $client = new Client(new TestHttpKernel());
  49. $r = new \ReflectionObject($client);
  50. $m = $r->getMethod('filterResponse');
  51. $m->setAccessible(true);
  52. $response = new Response();
  53. $response->headers->setCookie(new Cookie('foo', 'bar', \DateTime::createFromFormat('j-M-Y H:i:s T', '15-Feb-2009 20:00:00 GMT')->format('U'), '/foo', 'http://example.com', true, true));
  54. $domResponse = $m->invoke($client, $response);
  55. $this->assertEquals('foo=bar; expires=Sun, 15-Feb-2009 20:00:00 GMT; domain=http://example.com; path=/foo; secure; httponly', $domResponse->getHeader('Set-Cookie'));
  56. $response = new Response();
  57. $response->headers->setCookie(new Cookie('foo', 'bar', \DateTime::createFromFormat('j-M-Y H:i:s T', '15-Feb-2009 20:00:00 GMT')->format('U'), '/foo', 'http://example.com', true, true));
  58. $response->headers->setCookie(new Cookie('foo1', 'bar1', \DateTime::createFromFormat('j-M-Y H:i:s T', '15-Feb-2009 20:00:00 GMT')->format('U'), '/foo', 'http://example.com', true, true));
  59. $domResponse = $m->invoke($client, $response);
  60. $this->assertEquals('foo=bar; expires=Sun, 15-Feb-2009 20:00:00 GMT; domain=http://example.com; path=/foo; secure; httponly, foo1=bar1; expires=Sun, 15-Feb-2009 20:00:00 GMT; domain=http://example.com; path=/foo; secure; httponly', $domResponse->getHeader('Set-Cookie'));
  61. }
  62. }