You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OCSControllerTest.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2015 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\Controller;
  23. use OC\AppFramework\Http\Request;
  24. use OCP\AppFramework\Http\DataResponse;
  25. use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
  26. use OCP\AppFramework\OCSController;
  27. use OCP\IConfig;
  28. use OCP\IRequestId;
  29. class ChildOCSController extends OCSController {
  30. }
  31. class OCSControllerTest extends \Test\TestCase {
  32. public function testCors() {
  33. $request = new Request(
  34. [
  35. 'server' => [
  36. 'HTTP_ORIGIN' => 'test',
  37. ],
  38. ],
  39. $this->createMock(IRequestId::class),
  40. $this->createMock(IConfig::class)
  41. );
  42. $controller = new ChildOCSController('app', $request, 'verbs',
  43. 'headers', 100);
  44. $response = $controller->preflightedCors();
  45. $headers = $response->getHeaders();
  46. $this->assertEquals('test', $headers['Access-Control-Allow-Origin']);
  47. $this->assertEquals('verbs', $headers['Access-Control-Allow-Methods']);
  48. $this->assertEquals('headers', $headers['Access-Control-Allow-Headers']);
  49. $this->assertEquals('false', $headers['Access-Control-Allow-Credentials']);
  50. $this->assertEquals(100, $headers['Access-Control-Max-Age']);
  51. }
  52. public function testXML() {
  53. $controller = new ChildOCSController('app', new Request(
  54. [],
  55. $this->createMock(IRequestId::class),
  56. $this->createMock(IConfig::class)
  57. ));
  58. $controller->setOCSVersion(1);
  59. $expected = "<?xml version=\"1.0\"?>\n" .
  60. "<ocs>\n" .
  61. " <meta>\n" .
  62. " <status>ok</status>\n" .
  63. " <statuscode>100</statuscode>\n" .
  64. " <message>OK</message>\n" .
  65. " <totalitems></totalitems>\n" .
  66. " <itemsperpage></itemsperpage>\n" .
  67. " </meta>\n" .
  68. " <data>\n" .
  69. " <test>hi</test>\n" .
  70. " </data>\n" .
  71. "</ocs>\n";
  72. $params = new DataResponse(['test' => 'hi']);
  73. $response = $controller->buildResponse($params, 'xml');
  74. $this->assertSame(EmptyContentSecurityPolicy::class, get_class($response->getContentSecurityPolicy()));
  75. $this->assertEquals($expected, $response->render());
  76. }
  77. public function testJSON() {
  78. $controller = new ChildOCSController('app', new Request(
  79. [],
  80. $this->createMock(IRequestId::class),
  81. $this->createMock(IConfig::class)
  82. ));
  83. $controller->setOCSVersion(1);
  84. $expected = '{"ocs":{"meta":{"status":"ok","statuscode":100,"message":"OK",' .
  85. '"totalitems":"","itemsperpage":""},"data":{"test":"hi"}}}';
  86. $params = new DataResponse(['test' => 'hi']);
  87. $response = $controller->buildResponse($params, 'json');
  88. $this->assertSame(EmptyContentSecurityPolicy::class, get_class($response->getContentSecurityPolicy()));
  89. $this->assertEquals($expected, $response->render());
  90. $this->assertEquals($expected, $response->render());
  91. }
  92. public function testXMLV2() {
  93. $controller = new ChildOCSController('app', new Request(
  94. [],
  95. $this->createMock(IRequestId::class),
  96. $this->createMock(IConfig::class)
  97. ));
  98. $controller->setOCSVersion(2);
  99. $expected = "<?xml version=\"1.0\"?>\n" .
  100. "<ocs>\n" .
  101. " <meta>\n" .
  102. " <status>ok</status>\n" .
  103. " <statuscode>200</statuscode>\n" .
  104. " <message>OK</message>\n" .
  105. " </meta>\n" .
  106. " <data>\n" .
  107. " <test>hi</test>\n" .
  108. " </data>\n" .
  109. "</ocs>\n";
  110. $params = new DataResponse(['test' => 'hi']);
  111. $response = $controller->buildResponse($params, 'xml');
  112. $this->assertSame(EmptyContentSecurityPolicy::class, get_class($response->getContentSecurityPolicy()));
  113. $this->assertEquals($expected, $response->render());
  114. }
  115. public function testJSONV2() {
  116. $controller = new ChildOCSController('app', new Request(
  117. [],
  118. $this->createMock(IRequestId::class),
  119. $this->createMock(IConfig::class)
  120. ));
  121. $controller->setOCSVersion(2);
  122. $expected = '{"ocs":{"meta":{"status":"ok","statuscode":200,"message":"OK"},"data":{"test":"hi"}}}';
  123. $params = new DataResponse(['test' => 'hi']);
  124. $response = $controller->buildResponse($params, 'json');
  125. $this->assertSame(EmptyContentSecurityPolicy::class, get_class($response->getContentSecurityPolicy()));
  126. $this->assertEquals($expected, $response->render());
  127. }
  128. }