OCSControllerTest.php 3.7 KB

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