1
0

OCSControllerTest.php 4.1 KB

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