Aucune description

JsonResponseTest.php 8.3KB

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