You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

263 lines
8.6 KiB

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