DataResponseTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\AppFramework\Http;
  8. use OCP\AppFramework\Http;
  9. use OCP\AppFramework\Http\DataResponse;
  10. use OCP\IRequest;
  11. class DataResponseTest extends \Test\TestCase {
  12. /**
  13. * @var DataResponse
  14. */
  15. private $response;
  16. protected function setUp(): void {
  17. parent::setUp();
  18. $this->response = new DataResponse();
  19. }
  20. public function testSetData(): void {
  21. $params = ['hi', 'yo'];
  22. $this->response->setData($params);
  23. $this->assertEquals(['hi', 'yo'], $this->response->getData());
  24. }
  25. public function testConstructorAllowsToSetData(): void {
  26. $data = ['hi'];
  27. $code = 300;
  28. $response = new DataResponse($data, $code);
  29. $this->assertEquals($data, $response->getData());
  30. $this->assertEquals($code, $response->getStatus());
  31. }
  32. public function testConstructorAllowsToSetHeaders(): void {
  33. $data = ['hi'];
  34. $code = 300;
  35. $headers = ['test' => 'something'];
  36. $response = new DataResponse($data, $code, $headers);
  37. $expectedHeaders = [
  38. 'Cache-Control' => 'no-cache, no-store, must-revalidate',
  39. 'Content-Security-Policy' => "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'",
  40. 'Feature-Policy' => "autoplay 'none';camera 'none';fullscreen 'none';geolocation 'none';microphone 'none';payment 'none'",
  41. 'X-Robots-Tag' => 'noindex, nofollow',
  42. 'X-Request-Id' => \OC::$server->get(IRequest::class)->getId(),
  43. ];
  44. $expectedHeaders = array_merge($expectedHeaders, $headers);
  45. $this->assertEquals($data, $response->getData());
  46. $this->assertEquals($code, $response->getStatus());
  47. $this->assertEquals($expectedHeaders, $response->getHeaders());
  48. }
  49. public function testChainability(): void {
  50. $params = ['hi', 'yo'];
  51. $this->response->setData($params)
  52. ->setStatus(Http::STATUS_NOT_FOUND);
  53. $this->assertEquals(Http::STATUS_NOT_FOUND, $this->response->getStatus());
  54. $this->assertEquals(['hi', 'yo'], $this->response->getData());
  55. }
  56. }