DataResponseTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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\DataResponse;
  24. use OCP\AppFramework\Http;
  25. class DataResponseTest extends \Test\TestCase {
  26. /**
  27. * @var DataResponse
  28. */
  29. private $response;
  30. protected function setUp() {
  31. parent::setUp();
  32. $this->response = new DataResponse();
  33. }
  34. public function testSetData() {
  35. $params = array('hi', 'yo');
  36. $this->response->setData($params);
  37. $this->assertEquals(array('hi', 'yo'), $this->response->getData());
  38. }
  39. public function testConstructorAllowsToSetData() {
  40. $data = array('hi');
  41. $code = 300;
  42. $response = new DataResponse($data, $code);
  43. $this->assertEquals($data, $response->getData());
  44. $this->assertEquals($code, $response->getStatus());
  45. }
  46. public function testConstructorAllowsToSetHeaders() {
  47. $data = array('hi');
  48. $code = 300;
  49. $headers = array('test' => 'something');
  50. $response = new DataResponse($data, $code, $headers);
  51. $expectedHeaders = [
  52. 'Cache-Control' => 'no-cache, no-store, must-revalidate',
  53. 'Content-Security-Policy' => "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'",
  54. ];
  55. $expectedHeaders = array_merge($expectedHeaders, $headers);
  56. $this->assertEquals($data, $response->getData());
  57. $this->assertEquals($code, $response->getStatus());
  58. $this->assertEquals($expectedHeaders, $response->getHeaders());
  59. }
  60. public function testChainability() {
  61. $params = array('hi', 'yo');
  62. $this->response->setData($params)
  63. ->setStatus(Http::STATUS_NOT_FOUND);
  64. $this->assertEquals(Http::STATUS_NOT_FOUND, $this->response->getStatus());
  65. $this->assertEquals(array('hi', 'yo'), $this->response->getData());
  66. }
  67. }