Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/private/Http/Client/ClientService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(
) {
}

public function newClient(): IClient {
public function newClient(array $baseConfig = []): IClient {
$handler = new CurlHandler();
$stack = HandlerStack::create($handler);
if ($this->config->getSystemValueBool('dns_pinning', true)) {
Expand All @@ -51,7 +51,8 @@ public function newClient(): IClient {
$this->eventLogger->end('http:request');
}), 'event logger');

$client = new GuzzleClient(['handler' => $stack]);
$config = array_merge($baseConfig, ['handler' => $stack]);
$client = new GuzzleClient($config);

return new Client(
$this->config,
Expand Down
4 changes: 3 additions & 1 deletion lib/public/Http/Client/IClientService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
* @since 8.1.0
*/
interface IClientService {

/**
* @param array $baseConfig default configuration for the client
* @return IClient
* @since 8.1.0
*/
public function newClient(): IClient;
public function newClient(array $baseConfig = []): IClient;
}
52 changes: 52 additions & 0 deletions tests/lib/Http/Client/ClientServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,58 @@ public function testNewClient(): void {
);
}

public function testNewClientWithConfig(): 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);
$dnsPinMiddleware
->expects($this->atLeastOnce())
->method('addDnsPinning')
->willReturn(function (): void {
});
$remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
$eventLogger = $this->createMock(IEventLogger::class);
$logger = $this->createMock(LoggerInterface::class);
$serverVersion = $this->createMock(ServerVersion::class);

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

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

$this->assertEquals(
new Client(
$config,
$certificateManager,
$guzzleClient,
$remoteHostValidator,
$logger,
$serverVersion,
),
$clientService->newClient(['timeout' => 2.0])
);
}

public function testDisableDnsPinning(): void {
/** @var IConfig $config */
$config = $this->createMock(IConfig::class);
Expand Down
Loading