ResponseTest.php 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\BrowserKit;
  11. use Symfony\Component\BrowserKit\Response;
  12. class ResponseTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testGetUri()
  15. {
  16. $response = new Response('foo');
  17. $this->assertEquals('foo', $response->getContent(), '->getContent() returns the content of the response');
  18. }
  19. public function testGetStatus()
  20. {
  21. $response = new Response('foo', 304);
  22. $this->assertEquals('304', $response->getStatus(), '->getStatus() returns the status of the response');
  23. }
  24. public function testGetHeaders()
  25. {
  26. $response = new Response('foo', 200, array('foo' => 'bar'));
  27. $this->assertEquals(array('foo' => 'bar'), $response->getHeaders(), '->getHeaders() returns the headers of the response');
  28. }
  29. public function testGetHeader()
  30. {
  31. $response = new Response('foo', 200, array(
  32. 'Content-Type' => 'text/html',
  33. 'Set-Cookie' => array('foo=bar', 'bar=foo'),
  34. ));
  35. $this->assertEquals('text/html', $response->getHeader('Content-Type'), '->getHeader() returns a header of the response');
  36. $this->assertEquals('text/html', $response->getHeader('content-type'), '->getHeader() returns a header of the response');
  37. $this->assertEquals('text/html', $response->getHeader('content_type'), '->getHeader() returns a header of the response');
  38. $this->assertEquals('foo=bar', $response->getHeader('Set-Cookie'), '->getHeader() returns the first header value');
  39. $this->assertEquals(array('foo=bar', 'bar=foo'), $response->getHeader('Set-Cookie', false), '->getHeader() returns all header values if first is false');
  40. $this->assertNull($response->getHeader('foo'), '->getHeader() returns null if the header is not defined');
  41. $this->assertEquals(array(), $response->getHeader('foo', false), '->getHeader() returns an empty array if the header is not defined and first is set to false');
  42. }
  43. public function testMagicToString()
  44. {
  45. $response = new Response('foo', 304, array('foo' => 'bar'));
  46. $this->assertEquals("foo: bar\n\nfoo", $response->__toString(), '->__toString() returns the headers and the content as a string');
  47. }
  48. public function testMagicToStringWithMultipleSetCookieHeader()
  49. {
  50. $headers = array(
  51. 'content-type' => 'text/html; charset=utf-8',
  52. 'set-cookie' => array('foo=bar', 'bar=foo')
  53. );
  54. $expected = 'content-type: text/html; charset=utf-8'."\n";
  55. $expected.= 'set-cookie: foo=bar'."\n";
  56. $expected.= 'set-cookie: bar=foo'."\n\n";
  57. $expected.= 'foo';
  58. $response = new Response('foo', 304, $headers);
  59. $this->assertEquals($expected, $response->__toString(), '->__toString() returns the headers and the content as a string');
  60. }
  61. }