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 testCreate()
81 $response = JsonResponse::create(array('foo' => 'bar'), 204);
83 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
84 $this->assertEquals('{"foo":"bar"}', $response->getContent());
85 $this->assertEquals(204, $response->getStatusCode());
88 public function testStaticCreateEmptyJsonObject()
90 $response = JsonResponse::create();
91 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
92 $this->assertSame('{}', $response->getContent());
95 public function testStaticCreateJsonArray()
97 $response = JsonResponse::create(array(0, 1, 2, 3));
98 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
99 $this->assertSame('[0,1,2,3]', $response->getContent());
102 public function testStaticCreateJsonObject()
104 $response = JsonResponse::create(array('foo' => 'bar'));
105 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
106 $this->assertSame('{"foo":"bar"}', $response->getContent());
109 public function testStaticCreateWithSimpleTypes()
111 $response = JsonResponse::create('foo');
112 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
113 $this->assertSame('"foo"', $response->getContent());
115 $response = JsonResponse::create(0);
116 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
117 $this->assertSame('0', $response->getContent());
119 $response = JsonResponse::create(0.1);
120 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
121 $this->assertSame('0.1', $response->getContent());
123 $response = JsonResponse::create(true);
124 $this->assertInstanceOf('Symfony\Component\HttpFoundation\JsonResponse', $response);
125 $this->assertSame('true', $response->getContent());
128 public function testStaticCreateWithCustomStatus()
130 $response = JsonResponse::create(array(), 202);
131 $this->assertSame(202, $response->getStatusCode());
134 public function testStaticCreateAddsContentTypeHeader()
136 $response = JsonResponse::create();
137 $this->assertSame('application/json', $response->headers->get('Content-Type'));
140 public function testStaticCreateWithCustomHeaders()
142 $response = JsonResponse::create(array(), 200, array('ETag' => 'foo'));
143 $this->assertSame('application/json', $response->headers->get('Content-Type'));
144 $this->assertSame('foo', $response->headers->get('ETag'));
147 public function testStaticCreateWithCustomContentType()
149 $headers = array('Content-Type' => 'application/vnd.acme.blog-v1+json');
151 $response = JsonResponse::create(array(), 200, $headers);
152 $this->assertSame('application/vnd.acme.blog-v1+json', $response->headers->get('Content-Type'));
155 public function testSetCallback()
157 $response = JsonResponse::create(array('foo' => 'bar'))->setCallback('callback');
159 $this->assertEquals('/**/callback({"foo":"bar"});', $response->getContent());
160 $this->assertEquals('text/javascript', $response->headers->get('Content-Type'));
163 public function testJsonEncodeFlags()
165 $response = new JsonResponse('<>\'&"');
167 $this->assertEquals('"\u003C\u003E\u0027\u0026\u0022"', $response->getContent());
170 public function testGetEncodingOptions()
172 $response = new JsonResponse();
174 $this->assertEquals(JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT, $response->getEncodingOptions());
177 public function testSetEncodingOptions()
179 $response = new JsonResponse();
180 $response->setData(array(array(1, 2, 3)));
182 $this->assertEquals('[[1,2,3]]', $response->getContent());
184 $response->setEncodingOptions(JSON_FORCE_OBJECT);
186 $this->assertEquals('{"0":{"0":1,"1":2,"2":3}}', $response->getContent());
190 * @expectedException \InvalidArgumentException
192 public function testSetCallbackInvalidIdentifier()
194 $response = new JsonResponse('foo');
195 $response->setCallback('+invalid');
199 * @expectedException \InvalidArgumentException
201 public function testSetContent()
203 JsonResponse::create("\xB1\x31");
207 * @expectedException \Exception
208 * @expectedExceptionMessage This error is expected
211 public function testSetContentJsonSerializeError()
213 $serializable = new JsonSerializableObject();
215 JsonResponse::create($serializable);
218 public function testSetComplexCallback()
220 $response = JsonResponse::create(array('foo' => 'bar'));
221 $response->setCallback('ಠ_ಠ["foo"].bar[0]');
223 $this->assertEquals('/**/ಠ_ಠ["foo"].bar[0]({"foo":"bar"});', $response->getContent());
227 if (interface_exists('JsonSerializable')) {
228 class JsonSerializableObject implements \JsonSerializable
230 public function jsonSerialize()
232 throw new \Exception('This error is expected');