No Description

JsonResponseTest.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 testCreate()
  63. {
  64. $response = JsonResponse::create(array('foo' => 'bar'), 204);
  65. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  66. $this->assertEquals('{"foo":"bar"}', $response->getContent());
  67. $this->assertEquals(204, $response->getStatusCode());
  68. }
  69. public function testStaticCreateEmptyJsonObject()
  70. {
  71. $response = JsonResponse::create();
  72. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  73. $this->assertSame('{}', $response->getContent());
  74. }
  75. public function testStaticCreateJsonArray()
  76. {
  77. $response = JsonResponse::create(array(0, 1, 2, 3));
  78. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  79. $this->assertSame('[0,1,2,3]', $response->getContent());
  80. }
  81. public function testStaticCreateJsonObject()
  82. {
  83. $response = JsonResponse::create(array('foo' => 'bar'));
  84. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  85. $this->assertSame('{"foo":"bar"}', $response->getContent());
  86. }
  87. public function testStaticCreateWithSimpleTypes()
  88. {
  89. $response = JsonResponse::create('foo');
  90. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  91. $this->assertSame('"foo"', $response->getContent());
  92. $response = JsonResponse::create(0);
  93. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  94. $this->assertSame('0', $response->getContent());
  95. $response = JsonResponse::create(0.1);
  96. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  97. $this->assertSame('0.1', $response->getContent());
  98. $response = JsonResponse::create(true);
  99. $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
  100. $this->assertSame('true', $response->getContent());
  101. }
  102. public function testStaticCreateWithCustomStatus()
  103. {
  104. $response = JsonResponse::create(array(), 202);
  105. $this->assertSame(202, $response->getStatusCode());
  106. }
  107. public function testStaticCreateAddsContentTypeHeader()
  108. {
  109. $response = JsonResponse::create();
  110. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  111. }
  112. public function testStaticCreateWithCustomHeaders()
  113. {
  114. $response = JsonResponse::create(array(), 200, array('ETag' => 'foo'));
  115. $this->assertSame('application/json', $response->headers->get('Content-Type'));
  116. $this->assertSame('foo', $response->headers->get('ETag'));
  117. }
  118. public function testStaticCreateWithCustomContentType()
  119. {
  120. $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
  121. $response = JsonResponse::create(array(), 200, $headers);
  122. $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
  123. }
  124. public function testSetCallback()
  125. {
  126. $response = JsonResponse::create(array('foo' => 'bar'))->setCallback('callback');
  127. $this->assertEquals('/**/callback({"foo":"bar"});', $response->getContent());
  128. $this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
  129. }
  130. public function testJsonEncodeFlags()
  131. {
  132. $response = new JsonResponse('<>\'&"');
  133. $this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
  134. }
  135. public function testGetEncodingOptions()
  136. {
  137. $response = new JsonResponse();
  138. $this->assertEquals(JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT, $response->getEncodingOptions());
  139. }
  140. public function testSetEncodingOptions()
  141. {
  142. $response = new JsonResponse();
  143. $response->setData(array(array(1, 2, 3)));
  144. $this->assertEquals('[[1,2,3]]', $response->getContent());
  145. $response->setEncodingOptions(JSON_FORCE_OBJECT);
  146. $this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
  147. }
  148. /**
  149. * @expectedException \InvalidArgumentException
  150. */
  151. public function testSetCallbackInvalidIdentifier()
  152. {
  153. $response = new JsonResponse('foo');
  154. $response->setCallback('+invalid');
  155. }
  156. /**
  157. * @expectedException \InvalidArgumentException
  158. */
  159. public function testSetContent()
  160. {
  161. JsonResponse::create("\xB1\x31");
  162. }
  163. /**
  164. * @expectedException Exception
  165. * @expectedExceptionMessage Failed calling Symfony\Component\HttpFoundation\Tests\JsonSerializableObject::jsonSerialize()
  166. * @link http://php.net/manual/en/jsonserializable.jsonserialize.php#114688
  167. */
  168. public function testSetContentJsonSerializeError()
  169. {
  170. if (!interface_exists('JsonSerializable')) {
  171. $this->markTestSkipped('Interface JsonSerializable is available in PHP 5.4+');
  172. }
  173. $serializable = new JsonSerializableObject();
  174. JsonResponse::create($serializable);
  175. }
  176. }
  177. if (interface_exists('JsonSerializable')) {
  178. class JsonSerializableObject implements \JsonSerializable
  179. {
  180. public function jsonSerialize()
  181. {
  182. trigger_error('This error is expected', E_USER_WARNING);
  183. return array();
  184. }
  185. }
  186. }