ResponseTest.php 3.0 KB

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