RequestManagerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Http\WellKnown;
  8. use OC\AppFramework\Bootstrap\Coordinator;
  9. use OC\AppFramework\Bootstrap\RegistrationContext;
  10. use OC\AppFramework\Bootstrap\ServiceRegistration;
  11. use OC\Http\WellKnown\RequestManager;
  12. use OCP\AppFramework\QueryException;
  13. use OCP\Http\WellKnown\IHandler;
  14. use OCP\Http\WellKnown\IRequestContext;
  15. use OCP\Http\WellKnown\IResponse;
  16. use OCP\Http\WellKnown\JrdResponse;
  17. use OCP\IRequest;
  18. use OCP\IServerContainer;
  19. use PHPUnit\Framework\MockObject\MockObject;
  20. use Psr\Log\LoggerInterface;
  21. use RuntimeException;
  22. use Test\TestCase;
  23. use function get_class;
  24. class RequestManagerTest extends TestCase {
  25. /** @var Coordinator|MockObject */
  26. private $coordinator;
  27. /** @var IServerContainer|MockObject */
  28. private $container;
  29. /** @var MockObject|LoggerInterface */
  30. private $logger;
  31. /** @var RequestManager */
  32. private $manager;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->coordinator = $this->createMock(Coordinator::class);
  36. $this->container = $this->createMock(IServerContainer::class);
  37. $this->logger = $this->createMock(LoggerInterface::class);
  38. $this->manager = new RequestManager(
  39. $this->coordinator,
  40. $this->container,
  41. $this->logger,
  42. );
  43. }
  44. public function testProcessAppsNotRegistered(): void {
  45. $request = $this->createMock(IRequest::class);
  46. $this->expectException(RuntimeException::class);
  47. $this->manager->process("webfinger", $request);
  48. }
  49. public function testProcessNoHandlersRegistered(): void {
  50. $request = $this->createMock(IRequest::class);
  51. $registrationContext = $this->createMock(RegistrationContext::class);
  52. $this->coordinator->expects(self::once())
  53. ->method('getRegistrationContext')
  54. ->willReturn($registrationContext);
  55. $registrationContext->expects(self::once())
  56. ->method('getWellKnownHandlers')
  57. ->willReturn([]);
  58. $response = $this->manager->process("webfinger", $request);
  59. self::assertNull($response);
  60. }
  61. public function testProcessHandlerNotLoadable(): void {
  62. $request = $this->createMock(IRequest::class);
  63. $registrationContext = $this->createMock(RegistrationContext::class);
  64. $this->coordinator->expects(self::once())
  65. ->method('getRegistrationContext')
  66. ->willReturn($registrationContext);
  67. $handler = new class {
  68. };
  69. $registrationContext->expects(self::once())
  70. ->method('getWellKnownHandlers')
  71. ->willReturn([
  72. new ServiceRegistration('test', get_class($handler)),
  73. ]);
  74. $this->container->expects(self::once())
  75. ->method('get')
  76. ->with(get_class($handler))
  77. ->willThrowException(new QueryException(""));
  78. $this->logger->expects(self::once())
  79. ->method('error');
  80. $response = $this->manager->process("webfinger", $request);
  81. self::assertNull($response);
  82. }
  83. public function testProcessHandlerOfWrongType(): void {
  84. $request = $this->createMock(IRequest::class);
  85. $registrationContext = $this->createMock(RegistrationContext::class);
  86. $this->coordinator->expects(self::once())
  87. ->method('getRegistrationContext')
  88. ->willReturn($registrationContext);
  89. $handler = new class {
  90. };
  91. $registrationContext->expects(self::once())
  92. ->method('getWellKnownHandlers')
  93. ->willReturn([
  94. new ServiceRegistration('test', get_class($handler)),
  95. ]);
  96. $this->container->expects(self::once())
  97. ->method('get')
  98. ->with(get_class($handler))
  99. ->willReturn($handler);
  100. $this->logger->expects(self::once())
  101. ->method('error');
  102. $response = $this->manager->process("webfinger", $request);
  103. self::assertNull($response);
  104. }
  105. public function testProcess(): void {
  106. $request = $this->createMock(IRequest::class);
  107. $registrationContext = $this->createMock(RegistrationContext::class);
  108. $this->coordinator->expects(self::once())
  109. ->method('getRegistrationContext')
  110. ->willReturn($registrationContext);
  111. $handler = new class implements IHandler {
  112. public function handle(string $service, IRequestContext $context, ?IResponse $previousResponse): ?IResponse {
  113. return (new JrdResponse($service))->addAlias('alias');
  114. }
  115. };
  116. $registrationContext->expects(self::once())
  117. ->method('getWellKnownHandlers')
  118. ->willReturn([
  119. new ServiceRegistration('test', get_class($handler)),
  120. ]);
  121. $this->container->expects(self::once())
  122. ->method('get')
  123. ->with(get_class($handler))
  124. ->willReturn($handler);
  125. $response = $this->manager->process("webfinger", $request);
  126. self::assertNotNull($response);
  127. self::assertInstanceOf(JrdResponse::class, $response);
  128. }
  129. }