Dashboard sipadu mbip
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

JsonResponseTest.php 8.4KB

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