OCSControllerTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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>OK</status>\n" .
  58. " <statuscode>400</statuscode>\n" .
  59. " <message>OK</message>\n" .
  60. " </meta>\n" .
  61. " <data>\n" .
  62. " <test>hi</test>\n" .
  63. " </data>\n" .
  64. "</ocs>\n";
  65. $params = [
  66. 'data' => [
  67. 'test' => 'hi'
  68. ],
  69. 'statuscode' => 400
  70. ];
  71. $out = $controller->buildResponse($params, 'xml')->render();
  72. $this->assertEquals($expected, $out);
  73. }
  74. public function testXMLDataResponse() {
  75. $controller = new ChildOCSController('app', new Request(
  76. [],
  77. $this->getMock('\OCP\Security\ISecureRandom'),
  78. $this->getMock('\OCP\IConfig')
  79. ));
  80. $expected = "<?xml version=\"1.0\"?>\n" .
  81. "<ocs>\n" .
  82. " <meta>\n" .
  83. " <status>OK</status>\n" .
  84. " <statuscode>400</statuscode>\n" .
  85. " <message>OK</message>\n" .
  86. " </meta>\n" .
  87. " <data>\n" .
  88. " <test>hi</test>\n" .
  89. " </data>\n" .
  90. "</ocs>\n";
  91. $params = new DataResponse([
  92. 'data' => [
  93. 'test' => 'hi'
  94. ],
  95. 'statuscode' => 400
  96. ]);
  97. $out = $controller->buildResponse($params, 'xml')->render();
  98. $this->assertEquals($expected, $out);
  99. }
  100. public function testJSON() {
  101. $controller = new ChildOCSController('app', new Request(
  102. [],
  103. $this->getMock('\OCP\Security\ISecureRandom'),
  104. $this->getMock('\OCP\IConfig')
  105. ));
  106. $expected = '{"status":"OK","statuscode":400,"message":"OK",' .
  107. '"totalitems":"","itemsperpage":"","data":{"test":"hi"}}';
  108. $params = [
  109. 'data' => [
  110. 'test' => 'hi'
  111. ],
  112. 'statuscode' => 400
  113. ];
  114. $out = $controller->buildResponse($params, 'json')->render();
  115. $this->assertEquals($expected, $out);
  116. }
  117. }