Skip to content

Commit 082c137

Browse files
committed
feat: implement config for newClient
Signed-off-by: Sean Molenaar <sean@seanmolenaar.eu>
1 parent 5d7a02b commit 082c137

3 files changed

Lines changed: 61 additions & 7 deletions

File tree

lib/private/Http/Client/ClientService.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(
3939
) {
4040
}
4141

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

54-
$client = new GuzzleClient(['handler' => $stack]);
54+
$config = array_merge($baseConfig, ['handler' => $stack]);
55+
$client = new GuzzleClient($config);
5556

5657
return new Client(
5758
$this->config,

lib/public/Http/Client/IClientService.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
* @since 8.1.0
1515
*/
1616
interface IClientService {
17-
/**
18-
* @param array $baseConfig default configuration for the client
19-
* @return IClient
20-
* @since 8.1.0
21-
*/
17+
18+
/**
19+
* @param array $baseConfig default configuration for the client
20+
* @return IClient
21+
* @since 8.1.0
22+
*/
2223
public function newClient(array $baseConfig = []): IClient;
2324
}

tests/lib/Http/Client/ClientServiceTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,58 @@ public function testNewClient(): void {
8181
);
8282
}
8383

84+
public function testNewClientWithConfig(): void {
85+
/** @var IConfig $config */
86+
$config = $this->createMock(IConfig::class);
87+
$config->method('getSystemValueBool')
88+
->with('dns_pinning', true)
89+
->willReturn(true);
90+
/** @var ICertificateManager $certificateManager */
91+
$certificateManager = $this->createMock(ICertificateManager::class);
92+
$dnsPinMiddleware = $this->createMock(DnsPinMiddleware::class);
93+
$dnsPinMiddleware
94+
->expects($this->atLeastOnce())
95+
->method('addDnsPinning')
96+
->willReturn(function (): void {
97+
});
98+
$remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
99+
$eventLogger = $this->createMock(IEventLogger::class);
100+
$logger = $this->createMock(LoggerInterface::class);
101+
$serverVersion = $this->createMock(ServerVersion::class);
102+
103+
$clientService = new ClientService(
104+
$config,
105+
$certificateManager,
106+
$dnsPinMiddleware,
107+
$remoteHostValidator,
108+
$eventLogger,
109+
$logger,
110+
$serverVersion,
111+
);
112+
113+
$handler = new CurlHandler();
114+
$stack = HandlerStack::create($handler);
115+
$stack->push($dnsPinMiddleware->addDnsPinning());
116+
$stack->push(Middleware::tap(function (RequestInterface $request) use ($eventLogger): void {
117+
$eventLogger->start('http:request', $request->getMethod() . ' request to ' . $request->getRequestTarget());
118+
}, function () use ($eventLogger): void {
119+
$eventLogger->end('http:request');
120+
}), 'event logger');
121+
$guzzleClient = new GuzzleClient(['handler' => $stack, 'timeout' => 2.0]);
122+
123+
$this->assertEquals(
124+
new Client(
125+
$config,
126+
$certificateManager,
127+
$guzzleClient,
128+
$remoteHostValidator,
129+
$logger,
130+
$serverVersion,
131+
),
132+
$clientService->newClient(['timeout' => 2.0])
133+
);
134+
}
135+
84136
public function testDisableDnsPinning(): void {
85137
/** @var IConfig $config */
86138
$config = $this->createMock(IConfig::class);

0 commit comments

Comments
 (0)