RequestManagerTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace Test\Http\WellKnown;
  24. use OC\AppFramework\Bootstrap\Coordinator;
  25. use OC\AppFramework\Bootstrap\RegistrationContext;
  26. use OC\AppFramework\Bootstrap\ServiceRegistration;
  27. use OC\Http\WellKnown\RequestManager;
  28. use OCP\AppFramework\QueryException;
  29. use OCP\Http\WellKnown\IHandler;
  30. use OCP\Http\WellKnown\IRequestContext;
  31. use OCP\Http\WellKnown\IResponse;
  32. use OCP\Http\WellKnown\JrdResponse;
  33. use OCP\IRequest;
  34. use OCP\IServerContainer;
  35. use PHPUnit\Framework\MockObject\MockObject;
  36. use Psr\Log\LoggerInterface;
  37. use RuntimeException;
  38. use Test\TestCase;
  39. use function get_class;
  40. class RequestManagerTest extends TestCase {
  41. /** @var Coordinator|MockObject */
  42. private $coordinator;
  43. /** @var IServerContainer|MockObject */
  44. private $container;
  45. /** @var MockObject|LoggerInterface */
  46. private $logger;
  47. /** @var RequestManager */
  48. private $manager;
  49. protected function setUp(): void {
  50. parent::setUp();
  51. $this->coordinator = $this->createMock(Coordinator::class);
  52. $this->container = $this->createMock(IServerContainer::class);
  53. $this->logger = $this->createMock(LoggerInterface::class);
  54. $this->manager = new RequestManager(
  55. $this->coordinator,
  56. $this->container,
  57. $this->logger,
  58. );
  59. }
  60. public function testProcessAppsNotRegistered(): void {
  61. $request = $this->createMock(IRequest::class);
  62. $this->expectException(RuntimeException::class);
  63. $this->manager->process("webfinger", $request);
  64. }
  65. public function testProcessNoHandlersRegistered(): void {
  66. $request = $this->createMock(IRequest::class);
  67. $registrationContext = $this->createMock(RegistrationContext::class);
  68. $this->coordinator->expects(self::once())
  69. ->method('getRegistrationContext')
  70. ->willReturn($registrationContext);
  71. $registrationContext->expects(self::once())
  72. ->method('getWellKnownHandlers')
  73. ->willReturn([]);
  74. $response = $this->manager->process("webfinger", $request);
  75. self::assertNull($response);
  76. }
  77. public function testProcessHandlerNotLoadable(): void {
  78. $request = $this->createMock(IRequest::class);
  79. $registrationContext = $this->createMock(RegistrationContext::class);
  80. $this->coordinator->expects(self::once())
  81. ->method('getRegistrationContext')
  82. ->willReturn($registrationContext);
  83. $handler = new class {
  84. };
  85. $registrationContext->expects(self::once())
  86. ->method('getWellKnownHandlers')
  87. ->willReturn([
  88. new ServiceRegistration('test', get_class($handler)),
  89. ]);
  90. $this->container->expects(self::once())
  91. ->method('get')
  92. ->with(get_class($handler))
  93. ->willThrowException(new QueryException(""));
  94. $this->logger->expects(self::once())
  95. ->method('error');
  96. $response = $this->manager->process("webfinger", $request);
  97. self::assertNull($response);
  98. }
  99. public function testProcessHandlerOfWrongType(): void {
  100. $request = $this->createMock(IRequest::class);
  101. $registrationContext = $this->createMock(RegistrationContext::class);
  102. $this->coordinator->expects(self::once())
  103. ->method('getRegistrationContext')
  104. ->willReturn($registrationContext);
  105. $handler = new class {
  106. };
  107. $registrationContext->expects(self::once())
  108. ->method('getWellKnownHandlers')
  109. ->willReturn([
  110. new ServiceRegistration('test', get_class($handler)),
  111. ]);
  112. $this->container->expects(self::once())
  113. ->method('get')
  114. ->with(get_class($handler))
  115. ->willReturn($handler);
  116. $this->logger->expects(self::once())
  117. ->method('error');
  118. $response = $this->manager->process("webfinger", $request);
  119. self::assertNull($response);
  120. }
  121. public function testProcess(): void {
  122. $request = $this->createMock(IRequest::class);
  123. $registrationContext = $this->createMock(RegistrationContext::class);
  124. $this->coordinator->expects(self::once())
  125. ->method('getRegistrationContext')
  126. ->willReturn($registrationContext);
  127. $handler = new class implements IHandler {
  128. public function handle(string $service, IRequestContext $context, ?IResponse $previousResponse): ?IResponse {
  129. return (new JrdResponse($service))->addAlias('alias');
  130. }
  131. };
  132. $registrationContext->expects(self::once())
  133. ->method('getWellKnownHandlers')
  134. ->willReturn([
  135. new ServiceRegistration('test', get_class($handler)),
  136. ]);
  137. $this->container->expects(self::once())
  138. ->method('get')
  139. ->with(get_class($handler))
  140. ->willReturn($handler);
  141. $response = $this->manager->process("webfinger", $request);
  142. self::assertNotNull($response);
  143. self::assertInstanceOf(JrdResponse::class, $response);
  144. }
  145. }