1
0

ControllerTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 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\Controller;
  25. use OCP\AppFramework\Http\DataResponse;
  26. use OCP\AppFramework\Http\JSONResponse;
  27. use OCP\IConfig;
  28. class ChildController extends Controller {
  29. public function __construct($appName, $request) {
  30. parent::__construct($appName, $request);
  31. $this->registerResponder('tom', function ($respone) {
  32. return 'hi';
  33. });
  34. }
  35. public function custom($in) {
  36. $this->registerResponder('json', function ($response) {
  37. return new JSONResponse([strlen($response)]);
  38. });
  39. return $in;
  40. }
  41. public function customDataResponse($in) {
  42. $response = new DataResponse($in, 300);
  43. $response->addHeader('test', 'something');
  44. return $response;
  45. }
  46. };
  47. class ControllerTest extends \Test\TestCase {
  48. /**
  49. * @var Controller
  50. */
  51. private $controller;
  52. private $app;
  53. protected function setUp(): void {
  54. parent::setUp();
  55. $request = new Request(
  56. [
  57. 'get' => ['name' => 'John Q. Public', 'nickname' => 'Joey'],
  58. 'post' => ['name' => 'Jane Doe', 'nickname' => 'Janey'],
  59. 'urlParams' => ['name' => 'Johnny Weissmüller'],
  60. 'files' => ['file' => 'filevalue'],
  61. 'env' => ['PATH' => 'daheim'],
  62. 'session' => ['sezession' => 'kein'],
  63. 'method' => 'hi',
  64. ],
  65. $this->getMockBuilder('\OCP\Security\ISecureRandom')
  66. ->disableOriginalConstructor()
  67. ->getMock(),
  68. $this->getMockBuilder(IConfig::class)
  69. ->disableOriginalConstructor()
  70. ->getMock()
  71. );
  72. $this->app = $this->getMockBuilder('OC\AppFramework\DependencyInjection\DIContainer')
  73. ->setMethods(['getAppName'])
  74. ->setConstructorArgs(['test'])
  75. ->getMock();
  76. $this->app->expects($this->any())
  77. ->method('getAppName')
  78. ->willReturn('apptemplate_advanced');
  79. $this->controller = new ChildController($this->app, $request);
  80. }
  81. public function testFormatResonseInvalidFormat() {
  82. $this->expectException(\DomainException::class);
  83. $this->controller->buildResponse(null, 'test');
  84. }
  85. public function testFormat() {
  86. $response = $this->controller->buildResponse(['hi'], 'json');
  87. $this->assertEquals(['hi'], $response->getData());
  88. }
  89. public function testFormatDataResponseJSON() {
  90. $expectedHeaders = [
  91. 'test' => 'something',
  92. 'Cache-Control' => 'no-cache, no-store, must-revalidate',
  93. 'Content-Type' => 'application/json; charset=utf-8',
  94. 'Content-Security-Policy' => "default-src 'none';base-uri 'none';manifest-src 'self';frame-ancestors 'none'",
  95. 'Feature-Policy' => "autoplay 'none';camera 'none';fullscreen 'none';geolocation 'none';microphone 'none';payment 'none'",
  96. 'X-Robots-Tag' => 'none',
  97. ];
  98. $response = $this->controller->customDataResponse(['hi']);
  99. $response = $this->controller->buildResponse($response, 'json');
  100. $this->assertEquals(['hi'], $response->getData());
  101. $this->assertEquals(300, $response->getStatus());
  102. $this->assertEquals($expectedHeaders, $response->getHeaders());
  103. }
  104. public function testCustomFormatter() {
  105. $response = $this->controller->custom('hi');
  106. $response = $this->controller->buildResponse($response, 'json');
  107. $this->assertEquals([2], $response->getData());
  108. }
  109. public function testDefaultResponderToJSON() {
  110. $responder = $this->controller->getResponderByHTTPHeader('*/*');
  111. $this->assertEquals('json', $responder);
  112. }
  113. public function testResponderAcceptHeaderParsed() {
  114. $responder = $this->controller->getResponderByHTTPHeader(
  115. '*/*, application/tom, application/json'
  116. );
  117. $this->assertEquals('tom', $responder);
  118. }
  119. public function testResponderAcceptHeaderParsedUpperCase() {
  120. $responder = $this->controller->getResponderByHTTPHeader(
  121. '*/*, apPlication/ToM, application/json'
  122. );
  123. $this->assertEquals('tom', $responder);
  124. }
  125. }