Skip to content

Commit ace1c3c

Browse files
committed
Expose HTTP client options
1 parent bb899d8 commit ace1c3c

5 files changed

Lines changed: 62 additions & 10 deletions

File tree

src/Factories/HttpClientDecoratorFactory.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,23 @@
1212
*/
1313
class HttpClientDecoratorFactory
1414
{
15-
public function build(?Client $client = null): HttpClientDecorator
15+
/**
16+
* @param \GuzzleHttp\Client|null $client A pre-built client. If given, $httpClientConfig is ignored.
17+
* @param array<string,mixed> $httpClientConfig Guzzle client options merged over the defaults when no
18+
* $client is supplied. See https://docs.guzzlephp.org/en/stable/request-options.html
19+
*/
20+
public function build(?Client $client = null, array $httpClientConfig = []): HttpClientDecorator
1621
{
17-
return is_null($client) ? new HttpClientDecorator() : new HttpClientDecorator($client);
22+
if (!is_null($client)) {
23+
return new HttpClientDecorator($client);
24+
}
25+
26+
if ($httpClientConfig === []) {
27+
return new HttpClientDecorator();
28+
}
29+
30+
return new HttpClientDecorator(
31+
new Client(array_merge(HttpClientDecorator::DEFAULT_HTTP_CLIENT_CONFIG, $httpClientConfig)),
32+
);
1833
}
1934
}

src/Federation.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ class Federation
141141
protected ?EntityCollectionFactory $entityCollectionFactory = null;
142142

143143

144+
/**
145+
* @param array<string,mixed> $httpClientConfig
146+
*/
144147
public function __construct(
145148
protected readonly SupportedAlgorithms $supportedAlgorithms = new SupportedAlgorithms(),
146149
protected readonly SupportedSerializers $supportedSerializers = new SupportedSerializers(),
@@ -154,14 +157,15 @@ public function __construct(
154157
protected readonly TrustMarkStatusEndpointUsagePolicyEnum $defaultTrustMarkStatusEndpointUsagePolicyEnum = TrustMarkStatusEndpointUsagePolicyEnum::NotUsed,
155158
int $maxDiscoveryDepth = 10,
156159
protected ?EntityCollectionStoreInterface $entityCollectionStore = null,
160+
array $httpClientConfig = [],
157161
) {
158162
$this->maxCacheDurationDecorator = $this->dateIntervalDecoratorFactory()->build($maxCacheDuration);
159163
$this->timestampValidationLeewayDecorator = $this->dateIntervalDecoratorFactory()
160164
->build($timestampValidationLeeway);
161165
$this->maxTrustChainDepth = min(20, max(1, $maxTrustChainDepth));
162166
$this->maxDiscoveryDepth = max(1, $maxDiscoveryDepth);
163167
$this->cacheDecorator = is_null($cache) ? null : $this->cacheDecoratorFactory()->build($cache);
164-
$this->httpClientDecorator = $this->httpClientDecoratorFactory()->build($client);
168+
$this->httpClientDecorator = $this->httpClientDecoratorFactory()->build($client, $httpClientConfig);
165169
}
166170

167171

src/Jwks.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ class Jwks
7373
protected ?AlgorithmManagerDecorator $algorithmManagerDecorator = null;
7474

7575

76+
/**
77+
* @param array<string,mixed> $httpClientConfig
78+
*/
7679
public function __construct(
7780
protected readonly SupportedAlgorithms $supportedAlgorithms = new SupportedAlgorithms(),
7881
protected readonly SupportedSerializers $supportedSerializers = new SupportedSerializers(),
@@ -81,12 +84,13 @@ public function __construct(
8184
?CacheInterface $cache = null,
8285
protected readonly ?LoggerInterface $logger = null,
8386
?Client $httpClient = null,
87+
array $httpClientConfig = [],
8488
) {
8589
$this->maxCacheDurationDecorator = $this->dateIntervalDecoratorFactory()->build($maxCacheDuration);
8690
$this->timestampValidationLeewayDecorator = $this->dateIntervalDecoratorFactory()
8791
->build($timestampValidationLeeway);
8892
$this->cacheDecorator = is_null($cache) ? null : $this->cacheDecoratorFactory()->build($cache);
89-
$this->httpClientDecorator = $this->httpClientDecoratorFactory()->build($httpClient);
93+
$this->httpClientDecorator = $this->httpClientDecoratorFactory()->build($httpClient, $httpClientConfig);
9094
}
9195

9296

src/RequestObject.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ class RequestObject
8484
protected ?Helpers $helpers = null;
8585

8686

87+
/**
88+
* @param array<string,mixed> $httpClientConfig
89+
*/
8790
public function __construct(
8891
protected readonly SupportedAlgorithms $supportedAlgorithms = new SupportedAlgorithms(
8992
new SignatureAlgorithmBag(
@@ -98,10 +101,11 @@ public function __construct(
98101
protected ?RequestUriFetcher $requestUriFetcher = null,
99102
?Client $client = null,
100103
?CacheInterface $cache = null,
104+
array $httpClientConfig = [],
101105
) {
102106
$this->timestampValidationLeewayDecorator = $this->dateIntervalDecoratorFactory()
103107
->build($timestampValidationLeeway);
104-
$this->httpClientDecorator = $this->httpClientDecoratorFactory()->build($client);
108+
$this->httpClientDecorator = $this->httpClientDecoratorFactory()->build($client, $httpClientConfig);
105109
$this->cacheDecorator = is_null($cache) ? null : $this->cacheDecoratorFactory()->build($cache);
106110
}
107111

tests/src/Factories/HttpClientDecoratorFactoryTest.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,36 @@ public function testCanCreateInstance(): void
3131
}
3232

3333

34-
public function testCanBuild(): void
34+
public function testBuildWithClient(): void
3535
{
36-
$this->assertInstanceOf(
37-
HttpClientDecorator::class,
38-
$this->sut()->build($this->createStub(\GuzzleHttp\Client::class)),
39-
);
36+
$client = new \GuzzleHttp\Client();
37+
$decorator = $this->sut()->build($client);
38+
$this->assertSame($client, $decorator->client);
39+
}
40+
41+
42+
public function testBuildWithoutClientAndConfig(): void
43+
{
44+
$decorator = $this->sut()->build();
45+
$this->assertInstanceOf(HttpClientDecorator::class, $decorator);
46+
$this->assertNotEmpty($decorator->client->getConfig('allow_redirects'));
47+
}
48+
49+
50+
public function testBuildWithConfig(): void
51+
{
52+
$config = ['timeout' => 10.0, 'connect_timeout' => 5.0];
53+
$decorator = $this->sut()->build(null, $config);
54+
55+
$this->assertEqualsWithDelta(10.0, $decorator->client->getConfig('timeout'), PHP_FLOAT_EPSILON);
56+
$this->assertEqualsWithDelta(5.0, $decorator->client->getConfig('connect_timeout'), PHP_FLOAT_EPSILON);
57+
$this->assertNotEmpty($decorator->client->getConfig('allow_redirects'));
58+
}
59+
60+
61+
public function testBuildWithConfigOverridingDefaults(): void
62+
{
63+
$decorator = $this->sut()->build(null, ['allow_redirects' => false]);
64+
$this->assertFalse($decorator->client->getConfig('allow_redirects'));
4065
}
4166
}

0 commit comments

Comments
 (0)