2013-08-17 05:16:48 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2013-08-17 05:16:48 -04:00
|
|
|
*/
|
|
|
|
|
|
2016-05-18 12:40:34 -04:00
|
|
|
namespace Test\AppFramework\Http;
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2014-03-09 18:01:16 -04:00
|
|
|
use OCP\AppFramework\Http;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2013-08-20 18:41:20 -04:00
|
|
|
|
2014-11-10 17:30:38 -05:00
|
|
|
class JSONResponseTest extends \Test\TestCase {
|
2013-08-17 05:16:48 -04:00
|
|
|
/**
|
|
|
|
|
* @var JSONResponse
|
|
|
|
|
*/
|
|
|
|
|
private $json;
|
|
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2014-11-10 17:30:38 -05:00
|
|
|
parent::setUp();
|
2013-08-17 05:16:48 -04:00
|
|
|
$this->json = new JSONResponse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testHeader(): void {
|
2013-08-17 05:16:48 -04:00
|
|
|
$headers = $this->json->getHeaders();
|
2014-11-05 06:04:56 -05:00
|
|
|
$this->assertEquals('application/json; charset=utf-8', $headers['Content-Type']);
|
2013-08-17 05:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetData(): void {
|
2020-03-26 04:30:18 -04:00
|
|
|
$params = ['hi', 'yo'];
|
2013-08-17 05:16:48 -04:00
|
|
|
$this->json->setData($params);
|
|
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->assertEquals(['hi', 'yo'], $this->json->getData());
|
2013-08-17 05:16:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetRender(): void {
|
2020-03-26 04:30:18 -04:00
|
|
|
$params = ['test' => 'hi'];
|
2013-08-17 05:16:48 -04:00
|
|
|
$this->json->setData($params);
|
|
|
|
|
|
|
|
|
|
$expected = '{"test":"hi"}';
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $this->json->render());
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-12 11:18:04 -04:00
|
|
|
public static function renderDataProvider(): array {
|
2015-09-02 18:44:46 -04:00
|
|
|
return [
|
|
|
|
|
[
|
|
|
|
|
['test' => 'hi'], '{"test":"hi"}',
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
['<h1>test' => '<h1>hi'], '{"\u003Ch1\u003Etest":"\u003Ch1\u003Ehi"}',
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
2013-08-17 05:16:48 -04:00
|
|
|
|
2015-09-02 18:44:46 -04:00
|
|
|
/**
|
|
|
|
|
* @param array $input
|
|
|
|
|
* @param string $expected
|
|
|
|
|
*/
|
2025-06-30 10:56:59 -04:00
|
|
|
#[\PHPUnit\Framework\Attributes\DataProvider('renderDataProvider')]
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRender(array $input, $expected): void {
|
2015-09-02 18:44:46 -04:00
|
|
|
$this->json->setData($input);
|
2013-08-17 05:16:48 -04:00
|
|
|
$this->assertEquals($expected, $this->json->render());
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-27 08:57:22 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRenderWithNonUtf8Encoding(): void {
|
2022-05-27 08:57:22 -04:00
|
|
|
$this->expectException(\JsonException::class);
|
|
|
|
|
$this->expectExceptionMessage('Malformed UTF-8 characters, possibly incorrectly encoded');
|
2019-11-27 09:27:18 -05:00
|
|
|
|
2015-07-01 14:47:04 -04:00
|
|
|
$params = ['test' => hex2bin('e9')];
|
|
|
|
|
$this->json->setData($params);
|
|
|
|
|
$this->json->render();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testConstructorAllowsToSetData(): void {
|
2020-03-26 04:30:18 -04:00
|
|
|
$data = ['hi'];
|
2013-08-17 05:16:48 -04:00
|
|
|
$code = 300;
|
|
|
|
|
$response = new JSONResponse($data, $code);
|
|
|
|
|
|
|
|
|
|
$expected = '["hi"]';
|
|
|
|
|
$this->assertEquals($expected, $response->render());
|
|
|
|
|
$this->assertEquals($code, $response->getStatus());
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testChainability(): void {
|
2020-03-26 04:30:18 -04:00
|
|
|
$params = ['hi', 'yo'];
|
2014-03-09 18:01:16 -04:00
|
|
|
$this->json->setData($params)
|
|
|
|
|
->setStatus(Http::STATUS_NOT_FOUND);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(Http::STATUS_NOT_FOUND, $this->json->getStatus());
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->assertEquals(['hi', 'yo'], $this->json->getData());
|
2014-03-09 18:01:16 -04:00
|
|
|
}
|
2013-08-17 05:16:48 -04:00
|
|
|
}
|