1
0

DataResponseTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2014 Bernhard Posselt <dev@bernhard-posselt.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\AppFramework\Http;
  23. use OCP\AppFramework\Http;
  24. use OCP\AppFramework\Http\DataResponse;
  25. use OCP\IRequest;
  26. class DataResponseTest extends \Test\TestCase {
  27. /**
  28. * @var DataResponse
  29. */
  30. private $response;
  31. protected function setUp(): void {
  32. parent::setUp();
  33. $this->response = new DataResponse();
  34. }
  35. public function testSetData() {
  36. $params = ['hi', 'yo'];
  37. $this->response->setData($params);
  38. $this->assertEquals(['hi', 'yo'], $this->response->getData());
  39. }
  40. public function testConstructorAllowsToSetData() {
  41. $data = ['hi'];
  42. $code = 300;
  43. $response = new DataResponse($data, $code);
  44. $this->assertEquals($data, $response->getData());
  45. $this->assertEquals($code, $response->getStatus());
  46. }
  47. public function testConstructorAllowsToSetHeaders() {
  48. $data = ['hi'];
  49. $code = 300;
  50. $headers = ['test' => 'something'];
  51. $response = new DataResponse($data, $code, $headers);
  52. $expectedHeaders = [
  53. 'Cache-Control' => 'no-cache, no-store, must-revalidate',
  54. 'Content-Security-Policy' => "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'",
  55. 'Feature-Policy' => "autoplay 'none';camera 'none';fullscreen 'none';geolocation 'none';microphone 'none';payment 'none'",
  56. 'X-Robots-Tag' => 'noindex, nofollow',
  57. 'X-Request-Id' => \OC::$server->get(IRequest::class)->getId(),
  58. ];
  59. $expectedHeaders = array_merge($expectedHeaders, $headers);
  60. $this->assertEquals($data, $response->getData());
  61. $this->assertEquals($code, $response->getStatus());
  62. $this->assertEquals($expectedHeaders, $response->getHeaders());
  63. }
  64. public function testChainability() {
  65. $params = ['hi', 'yo'];
  66. $this->response->setData($params)
  67. ->setStatus(Http::STATUS_NOT_FOUND);
  68. $this->assertEquals(Http::STATUS_NOT_FOUND, $this->response->getStatus());
  69. $this->assertEquals(['hi', 'yo'], $this->response->getData());
  70. }
  71. }