1
0

ClientServiceTest.php 3.7 KB

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