4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation\Tests;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\JsonResponse;
17 class JsonResponseTest extends TestCase
19 protected function setUp()
23 if (!\defined('HHVM_VERSION')) {
24 $this->iniSet('serialize_precision', 14);
28 public function testConstructorEmptyCreatesJsonObject()
30 $response = new JsonResponse();
31 $this->assertSame('{}', $response->getContent());
34 public function testConstructorWithArrayCreatesJsonArray()
36 $response = new JsonResponse(array(0, 1, 2, 3));
37 $this->assertSame('[0,1,2,3]', $response->getContent());
40 public function testConstructorWithAssocArrayCreatesJsonObject()
42 $response = new JsonResponse(array('foo' => 'bar'));
43 $this->assertSame('{"foo":"bar"}', $response->getContent());
46 public function testConstructorWithSimpleTypes()
48 $response = new JsonResponse('foo');
49 $this->assertSame('"foo"', $response->getContent());
51 $response = new JsonResponse(0);
52 $this->assertSame('0', $response->getContent());
54 $response = new JsonResponse(0.1);
55 $this->assertSame('0.1', $response->getContent());
57 $response = new JsonResponse(true);
58 $this->assertSame('true', $response->getContent());
61 public function testConstructorWithCustomStatus()
63 $response = new JsonResponse(array(), 202);
64 $this->assertSame(202, $response->getStatusCode());
67 public function testConstructorAddsContentTypeHeader()
69 $response = new JsonResponse();
70 $this->assertSame('application/json', $response->headers->get('Content-Type'));
73 public function testConstructorWithCustomHeaders()
75 $response = new JsonResponse(array(), 200, array('ETag' => 'foo'));
76 $this->assertSame('application/json', $response->headers->get('Content-Type'));
77 $this->assertSame('foo', $response->headers->get('ETag'));
80 public function testConstructorWithCustomContentType()
82 $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
84 $response = new JsonResponse(array(), 200, $headers);
85 $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
88 public function testSetJson()
90 $response = new JsonResponse('1', 200, array(), true);
91 $this->assertEquals('1', $response->getContent());
93 $response = new JsonResponse('[1]', 200, array(), true);
94 $this->assertEquals('[1]', $response->getContent());
96 $response = new JsonResponse(null, 200, array());
97 $response->setJson('true');
98 $this->assertEquals('true', $response->getContent());
101 public function testCreate()
103 $response = JsonResponse::create(array('foo' => 'bar'), 204);
105 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
106 $this->assertEquals('{"foo":"bar"}', $response->getContent());
107 $this->assertEquals(204, $response->getStatusCode());
110 public function testStaticCreateEmptyJsonObject()
112 $response = JsonResponse::create();
113 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
114 $this->assertSame('{}', $response->getContent());
117 public function testStaticCreateJsonArray()
119 $response = JsonResponse::create(array(0, 1, 2, 3));
120 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
121 $this->assertSame('[0,1,2,3]', $response->getContent());
124 public function testStaticCreateJsonObject()
126 $response = JsonResponse::create(array('foo' => 'bar'));
127 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
128 $this->assertSame('{"foo":"bar"}', $response->getContent());
131 public function testStaticCreateWithSimpleTypes()
133 $response = JsonResponse::create('foo');
134 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
135 $this->assertSame('"foo"', $response->getContent());
137 $response = JsonResponse::create(0);
138 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
139 $this->assertSame('0', $response->getContent());
141 $response = JsonResponse::create(0.1);
142 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
143 $this->assertSame('0.1', $response->getContent());
145 $response = JsonResponse::create(true);
146 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
147 $this->assertSame('true', $response->getContent());
150 public function testStaticCreateWithCustomStatus()
152 $response = JsonResponse::create(array(), 202);
153 $this->assertSame(202, $response->getStatusCode());
156 public function testStaticCreateAddsContentTypeHeader()
158 $response = JsonResponse::create();
159 $this->assertSame('application/json', $response->headers->get('Content-Type'));
162 public function testStaticCreateWithCustomHeaders()
164 $response = JsonResponse::create(array(), 200, array('ETag' => 'foo'));
165 $this->assertSame('application/json', $response->headers->get('Content-Type'));
166 $this->assertSame('foo', $response->headers->get('ETag'));
169 public function testStaticCreateWithCustomContentType()
171 $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
173 $response = JsonResponse::create(array(), 200, $headers);
174 $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
177 public function testSetCallback()
179 $response = JsonResponse::create(array('foo' => 'bar'))->setCallback('callback');
181 $this->assertEquals('/**/callback({"foo":"bar"});', $response->getContent());
182 $this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
185 public function testJsonEncodeFlags()
187 $response = new JsonResponse('<>\'&"');
189 $this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
192 public function testGetEncodingOptions()
194 $response = new JsonResponse();
196 $this->assertEquals(JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT, $response->getEncodingOptions());
199 public function testSetEncodingOptions()
201 $response = new JsonResponse();
202 $response->setData(array(array(1, 2, 3)));
204 $this->assertEquals('[[1,2,3]]', $response->getContent());
206 $response->setEncodingOptions(JSON_FORCE_OBJECT);
208 $this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
211 public function testItAcceptsJsonAsString()
213 $response = JsonResponse::fromJsonString('{"foo":"bar"}');
214 $this->assertSame('{"foo":"bar"}', $response->getContent());
218 * @expectedException \InvalidArgumentException
220 public function testSetCallbackInvalidIdentifier()
222 $response = new JsonResponse('foo');
223 $response->setCallback('+invalid');
227 * @expectedException \InvalidArgumentException
229 public function testSetContent()
231 JsonResponse::create("\xB1\x31");
235 * @expectedException \Exception
236 * @expectedExceptionMessage This error is expected
238 public function testSetContentJsonSerializeError()
240 if (!interface_exists('JsonSerializable', false)) {
241 $this->markTestSkipped('JsonSerializable is required.');
244 $serializable = new JsonSerializableObject();
246 JsonResponse::create($serializable);
249 public function testSetComplexCallback()
251 $response = JsonResponse::create(array('foo' => 'bar'));
252 $response->setCallback('ಠ_ಠ["foo"].bar[0]');
254 $this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
258 if (interface_exists('JsonSerializable', false)) {
259 class JsonSerializableObject implements \JsonSerializable
261 public function jsonSerialize()
263 throw new \Exception('This error is expected');