1
0

ControllerTest.php 4.5 KB

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