JsonResponseTest.php 8.5 KB

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