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 public function testConstructorEmptyCreatesJsonObject()
21 $response = new JsonResponse();
22 $this->assertSame('{}', $response->getContent());
25 public function testConstructorWithArrayCreatesJsonArray()
27 $response = new JsonResponse(array(0, 1, 2, 3));
28 $this->assertSame('[0,1,2,3]', $response->getContent());
31 public function testConstructorWithAssocArrayCreatesJsonObject()
33 $response = new JsonResponse(array('foo' => 'bar'));
34 $this->assertSame('{"foo":"bar"}', $response->getContent());
37 public function testConstructorWithSimpleTypes()
39 $response = new JsonResponse('foo');
40 $this->assertSame('"foo"', $response->getContent());
42 $response = new JsonResponse(0);
43 $this->assertSame('0', $response->getContent());
45 $response = new JsonResponse(0.1);
46 $this->assertSame('0.1', $response->getContent());
48 $response = new JsonResponse(true);
49 $this->assertSame('true', $response->getContent());
52 public function testConstructorWithCustomStatus()
54 $response = new JsonResponse(array(), 202);
55 $this->assertSame(202, $response->getStatusCode());
58 public function testConstructorAddsContentTypeHeader()
60 $response = new JsonResponse();
61 $this->assertSame('application/json', $response->headers->get('Content-Type'));
64 public function testConstructorWithCustomHeaders()
66 $response = new JsonResponse(array(), 200, array('ETag' => 'foo'));
67 $this->assertSame('application/json', $response->headers->get('Content-Type'));
68 $this->assertSame('foo', $response->headers->get('ETag'));
71 public function testConstructorWithCustomContentType()
73 $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
75 $response = new JsonResponse(array(), 200, $headers);
76 $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
79 public function testSetJson()
81 $response = new JsonResponse('1', 200, array(), true);
82 $this->assertEquals('1', $response->getContent());
84 $response = new JsonResponse('[1]', 200, array(), true);
85 $this->assertEquals('[1]', $response->getContent());
87 $response = new JsonResponse(null, 200, array());
88 $response->setJson('true');
89 $this->assertEquals('true', $response->getContent());
92 public function testCreate()
94 $response = JsonResponse::create(array('foo' => 'bar'), 204);
96 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
97 $this->assertEquals('{"foo":"bar"}', $response->getContent());
98 $this->assertEquals(204, $response->getStatusCode());
101 public function testStaticCreateEmptyJsonObject()
103 $response = JsonResponse::create();
104 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
105 $this->assertSame('{}', $response->getContent());
108 public function testStaticCreateJsonArray()
110 $response = JsonResponse::create(array(0, 1, 2, 3));
111 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
112 $this->assertSame('[0,1,2,3]', $response->getContent());
115 public function testStaticCreateJsonObject()
117 $response = JsonResponse::create(array('foo' => 'bar'));
118 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
119 $this->assertSame('{"foo":"bar"}', $response->getContent());
122 public function testStaticCreateWithSimpleTypes()
124 $response = JsonResponse::create('foo');
125 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
126 $this->assertSame('"foo"', $response->getContent());
128 $response = JsonResponse::create(0);
129 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
130 $this->assertSame('0', $response->getContent());
132 $response = JsonResponse::create(0.1);
133 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
134 $this->assertSame('0.1', $response->getContent());
136 $response = JsonResponse::create(true);
137 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
138 $this->assertSame('true', $response->getContent());
141 public function testStaticCreateWithCustomStatus()
143 $response = JsonResponse::create(array(), 202);
144 $this->assertSame(202, $response->getStatusCode());
147 public function testStaticCreateAddsContentTypeHeader()
149 $response = JsonResponse::create();
150 $this->assertSame('application/json', $response->headers->get('Content-Type'));
153 public function testStaticCreateWithCustomHeaders()
155 $response = JsonResponse::create(array(), 200, array('ETag' => 'foo'));
156 $this->assertSame('application/json', $response->headers->get('Content-Type'));
157 $this->assertSame('foo', $response->headers->get('ETag'));
160 public function testStaticCreateWithCustomContentType()
162 $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
164 $response = JsonResponse::create(array(), 200, $headers);
165 $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
168 public function testSetCallback()
170 $response = JsonResponse::create(array('foo' => 'bar'))->setCallback('callback');
172 $this->assertEquals('/**/callback({"foo":"bar"});', $response->getContent());
173 $this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
176 public function testJsonEncodeFlags()
178 $response = new JsonResponse('<>\'&"');
180 $this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
183 public function testGetEncodingOptions()
185 $response = new JsonResponse();
187 $this->assertEquals(JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT, $response->getEncodingOptions());
190 public function testSetEncodingOptions()
192 $response = new JsonResponse();
193 $response->setData(array(array(1, 2, 3)));
195 $this->assertEquals('[[1,2,3]]', $response->getContent());
197 $response->setEncodingOptions(JSON_FORCE_OBJECT);
199 $this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
202 public function testItAcceptsJsonAsString()
204 $response = JsonResponse::fromJsonString('{"foo":"bar"}');
205 $this->assertSame('{"foo":"bar"}', $response->getContent());
209 * @expectedException \InvalidArgumentException
211 public function testSetCallbackInvalidIdentifier()
213 $response = new JsonResponse('foo');
214 $response->setCallback('+invalid');
218 * @expectedException \InvalidArgumentException
220 public function testSetContent()
222 JsonResponse::create("\xB1\x31");
226 * @expectedException \Exception
227 * @expectedExceptionMessage This error is expected
229 public function testSetContentJsonSerializeError()
231 $serializable = new JsonSerializableObject();
233 JsonResponse::create($serializable);
236 public function testSetComplexCallback()
238 $response = JsonResponse::create(array('foo' => 'bar'));
239 $response->setCallback('ಠ_ಠ["foo"].bar[0]');
241 $this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
245 if (interface_exists('JsonSerializable')) {
246 class JsonSerializableObject implements \JsonSerializable
248 public function jsonSerialize()
250 throw new \Exception('This error is expected');