ClientServiceTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 OC\Http\Client\Client;
  14. use OC\Http\Client\ClientService;
  15. use OC\Http\Client\DnsPinMiddleware;
  16. use OCP\ICertificateManager;
  17. use OCP\IConfig;
  18. use OCP\Security\IRemoteHostValidator;
  19. /**
  20. * Class ClientServiceTest
  21. */
  22. class ClientServiceTest extends \Test\TestCase {
  23. public function testNewClient(): void {
  24. /** @var IConfig $config */
  25. $config = $this->createMock(IConfig::class);
  26. $config->method('getSystemValueBool')
  27. ->with('dns_pinning', true)
  28. ->willReturn(true);
  29. /** @var ICertificateManager $certificateManager */
  30. $certificateManager = $this->createMock(ICertificateManager::class);
  31. $dnsPinMiddleware = $this->createMock(DnsPinMiddleware::class);
  32. $dnsPinMiddleware
  33. ->expects($this->atLeastOnce())
  34. ->method('addDnsPinning')
  35. ->willReturn(function () {
  36. });
  37. $remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
  38. $clientService = new ClientService(
  39. $config,
  40. $certificateManager,
  41. $dnsPinMiddleware,
  42. $remoteHostValidator
  43. );
  44. $handler = new CurlHandler();
  45. $stack = HandlerStack::create($handler);
  46. $stack->push($dnsPinMiddleware->addDnsPinning());
  47. $guzzleClient = new GuzzleClient(['handler' => $stack]);
  48. $this->assertEquals(
  49. new Client(
  50. $config,
  51. $certificateManager,
  52. $guzzleClient,
  53. $remoteHostValidator,
  54. ),
  55. $clientService->newClient()
  56. );
  57. }
  58. public function testDisableDnsPinning(): void {
  59. /** @var IConfig $config */
  60. $config = $this->createMock(IConfig::class);
  61. $config->method('getSystemValueBool')
  62. ->with('dns_pinning', true)
  63. ->willReturn(false);
  64. /** @var ICertificateManager $certificateManager */
  65. $certificateManager = $this->createMock(ICertificateManager::class);
  66. $dnsPinMiddleware = $this->createMock(DnsPinMiddleware::class);
  67. $dnsPinMiddleware
  68. ->expects($this->never())
  69. ->method('addDnsPinning')
  70. ->willReturn(function () {
  71. });
  72. $remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
  73. $clientService = new ClientService(
  74. $config,
  75. $certificateManager,
  76. $dnsPinMiddleware,
  77. $remoteHostValidator,
  78. );
  79. $handler = new CurlHandler();
  80. $stack = HandlerStack::create($handler);
  81. $guzzleClient = new GuzzleClient(['handler' => $stack]);
  82. $this->assertEquals(
  83. new Client(
  84. $config,
  85. $certificateManager,
  86. $guzzleClient,
  87. $remoteHostValidator
  88. ),
  89. $clientService->newClient()
  90. );
  91. }
  92. }