JsonResponseTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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->assertEquals(0.1, $response->getContent());
  38. $this->assertIsString($response->getContent());
  39. $response = new JsonResponse(true);
  40. $this->assertSame('true', $response->getContent());
  41. }
  42. public function testConstructorWithCustomStatus()
  43. {
  44. $response = new JsonResponse([], 202);
  45. $this->assertSame(202, $response->getStatusCode());
  46. }
  47. public function testConstructorAddsContentTypeHeader()
  48. {
  49. $response = new JsonResponse();
  50. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  51. }
  52. public function testConstructorWithCustomHeaders()
  53. {
  54. $response = new JsonResponse([], 200, ['ETag' => 'foo']);
  55. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  56. $this->assertSame('foo', $response->headers->get('ETag'));
  57. }
  58. public function testConstructorWithCustomContentType()
  59. {
  60. $headers = ['Content-Type' => 'application/vnd.acme.blog-v1+json'];
  61. $response = new JsonResponse([], 200, $headers);
  62. $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
  63. }
  64. public function testSetJson()
  65. {
  66. $response = new JsonResponse('1', 200, [], true);
  67. $this->assertEquals('1', $response->getContent());
  68. $response = new JsonResponse('[1]', 200, [], true);
  69. $this->assertEquals('[1]', $response->getContent());
  70. $response = new JsonResponse(null, 200, []);
  71. $response->setJson('true');
  72. $this->assertEquals('true', $response->getContent());
  73. }
  74. /**
  75. * @group legacy
  76. */
  77. public function testCreate()
  78. {
  79. $response = JsonResponse::create(['foo' => 'bar'], 204);
  80. $this->assertInstanceOf(JsonResponse::class, $response);
  81. $this->assertEquals('{"foo":"bar"}', $response->getContent());
  82. $this->assertEquals(204, $response->getStatusCode());
  83. }
  84. /**
  85. * @group legacy
  86. */
  87. public function testStaticCreateEmptyJsonObject()
  88. {
  89. $response = JsonResponse::create();
  90. $this->assertInstanceOf(JsonResponse::class, $response);
  91. $this->assertSame('{}', $response->getContent());
  92. }
  93. /**
  94. * @group legacy
  95. */
  96. public function testStaticCreateJsonArray()
  97. {
  98. $response = JsonResponse::create([0, 1, 2, 3]);
  99. $this->assertInstanceOf(JsonResponse::class, $response);
  100. $this->assertSame('[0,1,2,3]', $response->getContent());
  101. }
  102. /**
  103. * @group legacy
  104. */
  105. public function testStaticCreateJsonObject()
  106. {
  107. $response = JsonResponse::create(['foo' => 'bar']);
  108. $this->assertInstanceOf(JsonResponse::class, $response);
  109. $this->assertSame('{"foo":"bar"}', $response->getContent());
  110. }
  111. /**
  112. * @group legacy
  113. */
  114. public function testStaticCreateWithSimpleTypes()
  115. {
  116. $response = JsonResponse::create('foo');
  117. $this->assertInstanceOf(JsonResponse::class, $response);
  118. $this->assertSame('"foo"', $response->getContent());
  119. $response = JsonResponse::create(0);
  120. $this->assertInstanceOf(JsonResponse::class, $response);
  121. $this->assertSame('0', $response->getContent());
  122. $response = JsonResponse::create(0.1);
  123. $this->assertInstanceOf(JsonResponse::class, $response);
  124. $this->assertEquals(0.1, $response->getContent());
  125. $this->assertIsString($response->getContent());
  126. $response = JsonResponse::create(true);
  127. $this->assertInstanceOf(JsonResponse::class, $response);
  128. $this->assertSame('true', $response->getContent());
  129. }
  130. /**
  131. * @group legacy
  132. */
  133. public function testStaticCreateWithCustomStatus()
  134. {
  135. $response = JsonResponse::create([], 202);
  136. $this->assertSame(202, $response->getStatusCode());
  137. }
  138. /**
  139. * @group legacy
  140. */
  141. public function testStaticCreateAddsContentTypeHeader()
  142. {
  143. $response = JsonResponse::create();
  144. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  145. }
  146. /**
  147. * @group legacy
  148. */
  149. public function testStaticCreateWithCustomHeaders()
  150. {
  151. $response = JsonResponse::create([], 200, ['ETag' => 'foo']);
  152. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  153. $this->assertSame('foo', $response->headers->get('ETag'));
  154. }
  155. /**
  156. * @group legacy
  157. */
  158. public function testStaticCreateWithCustomContentType()
  159. {
  160. $headers = ['Content-Type' => 'application/vnd.acme.blog-v1+json'];
  161. $response = JsonResponse::create([], 200, $headers);
  162. $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
  163. }
  164. public function testSetCallback()
  165. {
  166. $response = (new JsonResponse(['foo' => 'bar']))->setCallback('callback');
  167. $this->assertEquals('/**/callback({"foo":"bar"});', $response->getContent());
  168. $this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
  169. }
  170. public function testJsonEncodeFlags()
  171. {
  172. $response = new JsonResponse('<>\'&"');
  173. $this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
  174. }
  175. public function testGetEncodingOptions()
  176. {
  177. $response = new JsonResponse();
  178. $this->assertEquals(\JSON_HEX_TAG | \JSON_HEX_APOS | \JSON_HEX_AMP | \JSON_HEX_QUOT, $response->getEncodingOptions());
  179. }
  180. public function testSetEncodingOptions()
  181. {
  182. $response = new JsonResponse();
  183. $response->setData([[1, 2, 3]]);
  184. $this->assertEquals('[[1,2,3]]', $response->getContent());
  185. $response->setEncodingOptions(\JSON_FORCE_OBJECT);
  186. $this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
  187. }
  188. public function testItAcceptsJsonAsString()
  189. {
  190. $response = JsonResponse::fromJsonString('{"foo":"bar"}');
  191. $this->assertSame('{"foo":"bar"}', $response->getContent());
  192. }
  193. public function testSetCallbackInvalidIdentifier()
  194. {
  195. $this->expectException(\InvalidArgumentException::class);
  196. $response = new JsonResponse('foo');
  197. $response->setCallback('+invalid');
  198. }
  199. public function testSetContent()
  200. {
  201. $this->expectException(\InvalidArgumentException::class);
  202. new JsonResponse("\xB1\x31");
  203. }
  204. public function testSetContentJsonSerializeError()
  205. {
  206. $this->expectException(\Exception::class);
  207. $this->expectExceptionMessage('This error is expected');
  208. $serializable = new JsonSerializableObject();
  209. new JsonResponse($serializable);
  210. }
  211. public function testSetComplexCallback()
  212. {
  213. $response = new JsonResponse(['foo' => 'bar']);
  214. $response->setCallback('ಠ_ಠ["foo"].bar[0]');
  215. $this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
  216. }
  217. public function testConstructorWithNullAsDataThrowsAnUnexpectedValueException()
  218. {
  219. $this->expectException(\TypeError::class);
  220. $this->expectExceptionMessage('If $json is set to true, argument $data must be a string or object implementing __toString(), "null" given.');
  221. new JsonResponse(null, 200, [], true);
  222. }
  223. public function testConstructorWithObjectWithToStringMethod()
  224. {
  225. $class = new class() {
  226. public function __toString(): string
  227. {
  228. return '{}';
  229. }
  230. };
  231. $response = new JsonResponse($class, 200, [], true);
  232. $this->assertSame('{}', $response->getContent());
  233. }
  234. public function testConstructorWithObjectWithoutToStringMethodThrowsAnException()
  235. {
  236. $this->expectException(\TypeError::class);
  237. $this->expectExceptionMessage('If $json is set to true, argument $data must be a string or object implementing __toString(), "stdClass" given.');
  238. new JsonResponse(new \stdClass(), 200, [], true);
  239. }
  240. }
  241. class JsonSerializableObject implements \JsonSerializable
  242. {
  243. public function jsonSerialize(): array
  244. {
  245. throw new \Exception('This error is expected');
  246. }
  247. }