1
0

ControllerTest.php 3.9 KB

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