ClientServiceTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  6. * SPDX-License-Identifier: AGPL-3.0-or-later
  7. */
  8. namespace Test\Http\Client;
  9. use GuzzleHttp\Client as GuzzleClient;
  10. use GuzzleHttp\Handler\CurlHandler;
  11. use GuzzleHttp\HandlerStack;
  12. use GuzzleHttp\Middleware;
  13. use OC\Http\Client\Client;
  14. use OC\Http\Client\ClientService;
  15. use OC\Http\Client\DnsPinMiddleware;
  16. use OCP\Diagnostics\IEventLogger;
  17. use OCP\ICertificateManager;
  18. use OCP\IConfig;
  19. use OCP\Security\IRemoteHostValidator;
  20. use Psr\Http\Message\RequestInterface;
  21. use Psr\Log\LoggerInterface;
  22. /**
  23. * Class ClientServiceTest
  24. */
  25. class ClientServiceTest extends \Test\TestCase {
  26. public function testNewClient(): void {
  27. /** @var IConfig $config */
  28. $config = $this->createMock(IConfig::class);
  29. $config->method('getSystemValueBool')
  30. ->with('dns_pinning', true)
  31. ->willReturn(true);
  32. /** @var ICertificateManager $certificateManager */
  33. $certificateManager = $this->createMock(ICertificateManager::class);
  34. $dnsPinMiddleware = $this->createMock(DnsPinMiddleware::class);
  35. $dnsPinMiddleware
  36. ->expects($this->atLeastOnce())
  37. ->method('addDnsPinning')
  38. ->willReturn(function () {
  39. });
  40. $remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
  41. $eventLogger = $this->createMock(IEventLogger::class);
  42. $logger = $this->createMock(LoggerInterface::class);
  43. $clientService = new ClientService(
  44. $config,
  45. $certificateManager,
  46. $dnsPinMiddleware,
  47. $remoteHostValidator,
  48. $eventLogger,
  49. $logger,
  50. );
  51. $handler = new CurlHandler();
  52. $stack = HandlerStack::create($handler);
  53. $stack->push($dnsPinMiddleware->addDnsPinning());
  54. $stack->push(Middleware::tap(function (RequestInterface $request) use ($eventLogger) {
  55. $eventLogger->start('http:request', $request->getMethod() . " request to " . $request->getRequestTarget());
  56. }, function () use ($eventLogger) {
  57. $eventLogger->end('http:request');
  58. }), 'event logger');
  59. $guzzleClient = new GuzzleClient(['handler' => $stack]);
  60. $this->assertEquals(
  61. new Client(
  62. $config,
  63. $certificateManager,
  64. $guzzleClient,
  65. $remoteHostValidator,
  66. $logger,
  67. ),
  68. $clientService->newClient()
  69. );
  70. }
  71. public function testDisableDnsPinning(): void {
  72. /** @var IConfig $config */
  73. $config = $this->createMock(IConfig::class);
  74. $config->method('getSystemValueBool')
  75. ->with('dns_pinning', true)
  76. ->willReturn(false);
  77. /** @var ICertificateManager $certificateManager */
  78. $certificateManager = $this->createMock(ICertificateManager::class);
  79. $dnsPinMiddleware = $this->createMock(DnsPinMiddleware::class);
  80. $dnsPinMiddleware
  81. ->expects($this->never())
  82. ->method('addDnsPinning')
  83. ->willReturn(function () {
  84. });
  85. $remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
  86. $eventLogger = $this->createMock(IEventLogger::class);
  87. $logger = $this->createMock(LoggerInterface::class);
  88. $clientService = new ClientService(
  89. $config,
  90. $certificateManager,
  91. $dnsPinMiddleware,
  92. $remoteHostValidator,
  93. $eventLogger,
  94. $logger,
  95. );
  96. $handler = new CurlHandler();
  97. $stack = HandlerStack::create($handler);
  98. $stack->push(Middleware::tap(function (RequestInterface $request) use ($eventLogger) {
  99. $eventLogger->start('http:request', $request->getMethod() . " request to " . $request->getRequestTarget());
  100. }, function () use ($eventLogger) {
  101. $eventLogger->end('http:request');
  102. }), 'event logger');
  103. $guzzleClient = new GuzzleClient(['handler' => $stack]);
  104. $this->assertEquals(
  105. new Client(
  106. $config,
  107. $certificateManager,
  108. $guzzleClient,
  109. $remoteHostValidator,
  110. $logger,
  111. ),
  112. $clientService->newClient()
  113. );
  114. }
  115. }