Skip to content

Commit

Permalink
Merge pull request #40162 from nextcloud/backport/40108/stable27
Browse files Browse the repository at this point in the history
  • Loading branch information
kesselb committed Aug 30, 2023
2 parents 8b42fb0 + 4338d07 commit 03da441
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/private/Http/Client/ClientService.php
Expand Up @@ -27,8 +27,8 @@
namespace OC\Http\Client;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use OCP\Diagnostics\IEventLogger;
use OCP\Http\Client\IClient;
Expand Down Expand Up @@ -75,7 +75,9 @@ public function __construct(
public function newClient(): IClient {
$handler = new CurlHandler();
$stack = HandlerStack::create($handler);
$stack->push($this->dnsPinMiddleware->addDnsPinning());
if ($this->config->getSystemValueBool('dns_pinning', true)) {
$stack->push($this->dnsPinMiddleware->addDnsPinning());
}
$stack->push(Middleware::tap(function (RequestInterface $request) {
$this->eventLogger->start('http:request', $request->getMethod() . " request to " . $request->getRequestTarget());
}, function () {
Expand Down
53 changes: 52 additions & 1 deletion tests/lib/Http/Client/ClientServiceTest.php
Expand Up @@ -12,8 +12,8 @@
namespace Test\Http\Client;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use OC\Http\Client\Client;
use OC\Http\Client\ClientService;
Expand All @@ -32,6 +32,9 @@ class ClientServiceTest extends \Test\TestCase {
public function testNewClient(): void {
/** @var IConfig $config */
$config = $this->createMock(IConfig::class);
$config->method('getSystemValueBool')
->with('dns_pinning', true)
->willReturn(true);
/** @var ICertificateManager $certificateManager */
$certificateManager = $this->createMock(ICertificateManager::class);
$dnsPinMiddleware = $this->createMock(DnsPinMiddleware::class);
Expand Down Expand Up @@ -74,4 +77,52 @@ public function testNewClient(): void {
$clientService->newClient()
);
}

public function testDisableDnsPinning(): void {
/** @var IConfig $config */
$config = $this->createMock(IConfig::class);
$config->method('getSystemValueBool')
->with('dns_pinning', true)
->willReturn(false);
/** @var ICertificateManager $certificateManager */
$certificateManager = $this->createMock(ICertificateManager::class);
$dnsPinMiddleware = $this->createMock(DnsPinMiddleware::class);
$dnsPinMiddleware
->expects($this->never())
->method('addDnsPinning')
->willReturn(function () {
});
$remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
$eventLogger = $this->createMock(IEventLogger::class);
$logger = $this->createMock(LoggerInterface::class);

$clientService = new ClientService(
$config,
$certificateManager,
$dnsPinMiddleware,
$remoteHostValidator,
$eventLogger,
$logger,
);

$handler = new CurlHandler();
$stack = HandlerStack::create($handler);
$stack->push(Middleware::tap(function (RequestInterface $request) use ($eventLogger) {
$eventLogger->start('http:request', $request->getMethod() . " request to " . $request->getRequestTarget());
}, function () use ($eventLogger) {
$eventLogger->end('http:request');
}), 'event logger');
$guzzleClient = new GuzzleClient(['handler' => $stack]);

$this->assertEquals(
new Client(
$config,
$certificateManager,
$guzzleClient,
$remoteHostValidator,
$logger,
),
$clientService->newClient()
);
}
}

0 comments on commit 03da441

Please sign in to comment.