JsonResponseTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\HttpFoundation\Tests;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. class JsonResponseTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testConstructorEmptyCreatesJsonObject()
  15. {
  16. $response = new JsonResponse();
  17. $this->assertSame('{}', $response->getContent());
  18. }
  19. public function testConstructorWithArrayCreatesJsonArray()
  20. {
  21. $response = new JsonResponse(array(0, 1, 2, 3));
  22. $this->assertSame('[0,1,2,3]', $response->getContent());
  23. }
  24. public function testConstructorWithAssocArrayCreatesJsonObject()
  25. {
  26. $response = new JsonResponse(array('foo' => 'bar'));
  27. $this->assertSame('{"foo":"bar"}', $response->getContent());
  28. }
  29. public function testConstructorWithSimpleTypes()
  30. {
  31. $response = new JsonResponse('foo');
  32. $this->assertSame('"foo"', $response->getContent());
  33. $response = new JsonResponse(0);
  34. $this->assertSame('0', $response->getContent());
  35. $response = new JsonResponse(0.1);
  36. $this->assertSame('0.1', $response->getContent());
  37. $response = new JsonResponse(true);
  38. $this->assertSame('true', $response->getContent());
  39. }
  40. public function testConstructorWithCustomStatus()
  41. {
  42. $response = new JsonResponse(array(), 202);
  43. $this->assertSame(202, $response->getStatusCode());
  44. }
  45. public function testConstructorAddsContentTypeHeader()
  46. {
  47. $response = new JsonResponse();
  48. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  49. }
  50. public function testConstructorWithCustomHeaders()
  51. {
  52. $response = new JsonResponse(array(), 200, array('ETag' => 'foo'));
  53. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  54. $this->assertSame('foo', $response->headers->get('ETag'));
  55. }
  56. public function testConstructorWithCustomContentType()
  57. {
  58. $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
  59. $response = new JsonResponse(array(), 200, $headers);
  60. $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
  61. }
  62. public function testCreate()
  63. {
  64. $response = JsonResponse::create(array('foo' => 'bar'), 204);
  65. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  66. $this->assertEquals('{"foo":"bar"}', $response->getContent());
  67. $this->assertEquals(204, $response->getStatusCode());
  68. }
  69. public function testStaticCreateEmptyJsonObject()
  70. {
  71. $response = JsonResponse::create();
  72. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  73. $this->assertSame('{}', $response->getContent());
  74. }
  75. public function testStaticCreateJsonArray()
  76. {
  77. $response = JsonResponse::create(array(0, 1, 2, 3));
  78. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  79. $this->assertSame('[0,1,2,3]', $response->getContent());
  80. }
  81. public function testStaticCreateJsonObject()
  82. {
  83. $response = JsonResponse::create(array('foo' => 'bar'));
  84. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  85. $this->assertSame('{"foo":"bar"}', $response->getContent());
  86. }
  87. public function testStaticCreateWithSimpleTypes()
  88. {
  89. $response = JsonResponse::create('foo');
  90. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  91. $this->assertSame('"foo"', $response->getContent());
  92. $response = JsonResponse::create(0);
  93. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  94. $this->assertSame('0', $response->getContent());
  95. $response = JsonResponse::create(0.1);
  96. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  97. $this->assertSame('0.1', $response->getContent());
  98. $response = JsonResponse::create(true);
  99. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  100. $this->assertSame('true', $response->getContent());
  101. }
  102. public function testStaticCreateWithCustomStatus()
  103. {
  104. $response = JsonResponse::create(array(), 202);
  105. $this->assertSame(202, $response->getStatusCode());
  106. }
  107. public function testStaticCreateAddsContentTypeHeader()
  108. {
  109. $response = JsonResponse::create();
  110. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  111. }
  112. public function testStaticCreateWithCustomHeaders()
  113. {
  114. $response = JsonResponse::create(array(), 200, array('ETag' => 'foo'));
  115. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  116. $this->assertSame('foo', $response->headers->get('ETag'));
  117. }
  118. public function testStaticCreateWithCustomContentType()
  119. {
  120. $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
  121. $response = JsonResponse::create(array(), 200, $headers);
  122. $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
  123. }
  124. public function testSetCallback()
  125. {
  126. $response = JsonResponse::create(array('foo' => 'bar'))->setCallback('callback');
  127. $this->assertEquals('callback({"foo":"bar"});', $response->getContent());
  128. $this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
  129. }
  130. public function testSetCallbackInvalidIdentifier()
  131. {
  132. $response = new JsonResponse('foo');
  133. $this->setExpectedException('InvalidArgumentException');
  134. $response->setCallback('+invalid');
  135. }
  136. public function testJsonEncodeFlags()
  137. {
  138. $response = new JsonResponse('<>\'&"');
  139. $this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
  140. }
  141. }