ResponseTest.php 3.2 KB

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