JsonResponseTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 PHPUnit\Framework\TestCase;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. class JsonResponseTest extends TestCase
  14. {
  15. protected function setUp()
  16. {
  17. parent::setUp();
  18. if (!\defined('HHVM_VERSION')) {
  19. $this->iniSet('serialize_precision', 14);
  20. }
  21. }
  22. public function testConstructorEmptyCreatesJsonObject()
  23. {
  24. $response = new JsonResponse();
  25. $this->assertSame('{}', $response->getContent());
  26. }
  27. public function testConstructorWithArrayCreatesJsonArray()
  28. {
  29. $response = new JsonResponse(array(0, 1, 2, 3));
  30. $this->assertSame('[0,1,2,3]', $response->getContent());
  31. }
  32. public function testConstructorWithAssocArrayCreatesJsonObject()
  33. {
  34. $response = new JsonResponse(array('foo' => 'bar'));
  35. $this->assertSame('{"foo":"bar"}', $response->getContent());
  36. }
  37. public function testConstructorWithSimpleTypes()
  38. {
  39. $response = new JsonResponse('foo');
  40. $this->assertSame('"foo"', $response->getContent());
  41. $response = new JsonResponse(0);
  42. $this->assertSame('0', $response->getContent());
  43. $response = new JsonResponse(0.1);
  44. $this->assertSame('0.1', $response->getContent());
  45. $response = new JsonResponse(true);
  46. $this->assertSame('true', $response->getContent());
  47. }
  48. public function testConstructorWithCustomStatus()
  49. {
  50. $response = new JsonResponse(array(), 202);
  51. $this->assertSame(202, $response->getStatusCode());
  52. }
  53. public function testConstructorAddsContentTypeHeader()
  54. {
  55. $response = new JsonResponse();
  56. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  57. }
  58. public function testConstructorWithCustomHeaders()
  59. {
  60. $response = new JsonResponse(array(), 200, array('ETag' => 'foo'));
  61. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  62. $this->assertSame('foo', $response->headers->get('ETag'));
  63. }
  64. public function testConstructorWithCustomContentType()
  65. {
  66. $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
  67. $response = new JsonResponse(array(), 200, $headers);
  68. $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
  69. }
  70. public function testCreate()
  71. {
  72. $response = JsonResponse::create(array('foo' => 'bar'), 204);
  73. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  74. $this->assertEquals('{"foo":"bar"}', $response->getContent());
  75. $this->assertEquals(204, $response->getStatusCode());
  76. }
  77. public function testStaticCreateEmptyJsonObject()
  78. {
  79. $response = JsonResponse::create();
  80. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  81. $this->assertSame('{}', $response->getContent());
  82. }
  83. public function testStaticCreateJsonArray()
  84. {
  85. $response = JsonResponse::create(array(0, 1, 2, 3));
  86. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  87. $this->assertSame('[0,1,2,3]', $response->getContent());
  88. }
  89. public function testStaticCreateJsonObject()
  90. {
  91. $response = JsonResponse::create(array('foo' => 'bar'));
  92. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  93. $this->assertSame('{"foo":"bar"}', $response->getContent());
  94. }
  95. public function testStaticCreateWithSimpleTypes()
  96. {
  97. $response = JsonResponse::create('foo');
  98. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  99. $this->assertSame('"foo"', $response->getContent());
  100. $response = JsonResponse::create(0);
  101. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  102. $this->assertSame('0', $response->getContent());
  103. $response = JsonResponse::create(0.1);
  104. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  105. $this->assertSame('0.1', $response->getContent());
  106. $response = JsonResponse::create(true);
  107. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  108. $this->assertSame('true', $response->getContent());
  109. }
  110. public function testStaticCreateWithCustomStatus()
  111. {
  112. $response = JsonResponse::create(array(), 202);
  113. $this->assertSame(202, $response->getStatusCode());
  114. }
  115. public function testStaticCreateAddsContentTypeHeader()
  116. {
  117. $response = JsonResponse::create();
  118. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  119. }
  120. public function testStaticCreateWithCustomHeaders()
  121. {
  122. $response = JsonResponse::create(array(), 200, array('ETag' => 'foo'));
  123. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  124. $this->assertSame('foo', $response->headers->get('ETag'));
  125. }
  126. public function testStaticCreateWithCustomContentType()
  127. {
  128. $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
  129. $response = JsonResponse::create(array(), 200, $headers);
  130. $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
  131. }
  132. public function testSetCallback()
  133. {
  134. $response = JsonResponse::create(array('foo' => 'bar'))->setCallback('callback');
  135. $this->assertEquals('/**/callback({"foo":"bar"});', $response->getContent());
  136. $this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
  137. }
  138. public function testJsonEncodeFlags()
  139. {
  140. $response = new JsonResponse('<>\'&"');
  141. $this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
  142. }
  143. public function testGetEncodingOptions()
  144. {
  145. $response = new JsonResponse();
  146. $this->assertEquals(JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT, $response->getEncodingOptions());
  147. }
  148. public function testSetEncodingOptions()
  149. {
  150. $response = new JsonResponse();
  151. $response->setData(array(array(1, 2, 3)));
  152. $this->assertEquals('[[1,2,3]]', $response->getContent());
  153. $response->setEncodingOptions(JSON_FORCE_OBJECT);
  154. $this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
  155. }
  156. /**
  157. * @expectedException \InvalidArgumentException
  158. */
  159. public function testSetCallbackInvalidIdentifier()
  160. {
  161. $response = new JsonResponse('foo');
  162. $response->setCallback('+invalid');
  163. }
  164. /**
  165. * @expectedException \InvalidArgumentException
  166. */
  167. public function testSetContent()
  168. {
  169. JsonResponse::create("\xB1\x31");
  170. }
  171. /**
  172. * @expectedException \Exception
  173. * @expectedExceptionMessage This error is expected
  174. */
  175. public function testSetContentJsonSerializeError()
  176. {
  177. if (!interface_exists('JsonSerializable', false)) {
  178. $this->markTestSkipped('JsonSerializable is required.');
  179. }
  180. $serializable = new JsonSerializableObject();
  181. JsonResponse::create($serializable);
  182. }
  183. public function testSetComplexCallback()
  184. {
  185. $response = JsonResponse::create(array('foo' => 'bar'));
  186. $response->setCallback('ಠ_ಠ["foo"].bar[0]');
  187. $this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
  188. }
  189. }
  190. if (interface_exists('JsonSerializable', false)) {
  191. class JsonSerializableObject implements \JsonSerializable
  192. {
  193. public function jsonSerialize()
  194. {
  195. throw new \Exception('This error is expected');
  196. }
  197. }
  198. }