2014-10-28 11:34:04 -04:00
|
|
|
<?php
|
|
|
|
|
|
2025-09-29 06:34:49 -04:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2014-10-28 11:34:04 -04:00
|
|
|
/**
|
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
|
2014-10-28 11:34:04 -04:00
|
|
|
*/
|
|
|
|
|
|
2016-05-18 12:40:34 -04:00
|
|
|
namespace Test\AppFramework\Http;
|
2014-10-28 11:34:04 -04:00
|
|
|
|
|
|
|
|
use OCP\AppFramework\Http;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
2022-01-31 09:01:58 -05:00
|
|
|
use OCP\IRequest;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCP\Server;
|
2014-10-28 11:34:04 -04:00
|
|
|
|
2014-11-10 17:30:38 -05:00
|
|
|
class DataResponseTest extends \Test\TestCase {
|
2025-09-29 06:34:49 -04:00
|
|
|
private DataResponse $response;
|
2014-10-28 11:34:04 -04:00
|
|
|
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2014-11-10 17:30:38 -05:00
|
|
|
parent::setUp();
|
2014-10-28 11:34:04 -04:00
|
|
|
$this->response = new DataResponse();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSetData(): void {
|
2020-03-26 04:30:18 -04:00
|
|
|
$params = ['hi', 'yo'];
|
2014-10-28 11:34:04 -04:00
|
|
|
$this->response->setData($params);
|
|
|
|
|
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->assertEquals(['hi', 'yo'], $this->response->getData());
|
2014-10-28 11:34:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testConstructorAllowsToSetData(): void {
|
2020-03-26 04:30:18 -04:00
|
|
|
$data = ['hi'];
|
2014-10-28 11:34:04 -04:00
|
|
|
$code = 300;
|
|
|
|
|
$response = new DataResponse($data, $code);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($data, $response->getData());
|
|
|
|
|
$this->assertEquals($code, $response->getStatus());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testConstructorAllowsToSetHeaders(): void {
|
2020-03-26 04:30:18 -04:00
|
|
|
$data = ['hi'];
|
2014-10-28 11:34:04 -04:00
|
|
|
$code = 300;
|
2020-03-26 04:30:18 -04:00
|
|
|
$headers = ['test' => 'something'];
|
2014-10-28 11:34:04 -04:00
|
|
|
$response = new DataResponse($data, $code, $headers);
|
|
|
|
|
|
2015-02-09 10:30:01 -05:00
|
|
|
$expectedHeaders = [
|
2017-01-10 04:13:08 -05:00
|
|
|
'Cache-Control' => 'no-cache, no-store, must-revalidate',
|
2020-11-17 10:19:03 -05:00
|
|
|
'Content-Security-Policy' => "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'",
|
2019-07-27 09:33:55 -04:00
|
|
|
'Feature-Policy' => "autoplay 'none';camera 'none';fullscreen 'none';geolocation 'none';microphone 'none';payment 'none'",
|
2023-02-13 08:09:13 -05:00
|
|
|
'X-Robots-Tag' => 'noindex, nofollow',
|
2025-06-12 12:31:58 -04:00
|
|
|
'X-Request-Id' => Server::get(IRequest::class)->getId(),
|
2015-02-09 10:30:01 -05:00
|
|
|
];
|
2014-10-28 11:34:04 -04:00
|
|
|
$expectedHeaders = array_merge($expectedHeaders, $headers);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($data, $response->getData());
|
|
|
|
|
$this->assertEquals($code, $response->getStatus());
|
|
|
|
|
$this->assertEquals($expectedHeaders, $response->getHeaders());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testChainability(): void {
|
2020-03-26 04:30:18 -04:00
|
|
|
$params = ['hi', 'yo'];
|
2014-10-28 11:34:04 -04:00
|
|
|
$this->response->setData($params)
|
|
|
|
|
->setStatus(Http::STATUS_NOT_FOUND);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(Http::STATUS_NOT_FOUND, $this->response->getStatus());
|
2020-03-26 04:30:18 -04:00
|
|
|
$this->assertEquals(['hi', 'yo'], $this->response->getData());
|
2014-10-28 11:34:04 -04:00
|
|
|
}
|
|
|
|
|
}
|