JsonResponseTest.php 8.3 KB

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