Skip to content

Commit 8e202db

Browse files
gjtorikianclaude
andauthored
fix: add default User-Agent header and optional override (#352)
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c6e2d5d commit 8e202db

4 files changed

Lines changed: 56 additions & 1 deletion

File tree

lib/HttpClient.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function __construct(
3737
private readonly int $timeout,
3838
private readonly int $maxRetries,
3939
?\GuzzleHttp\HandlerStack $handler = null,
40+
private readonly ?string $userAgent = null,
4041
) {
4142
$this->client = new Client([
4243
'handler' => $handler,
@@ -190,6 +191,9 @@ private function buildRequestOptions(
190191
$headers['Idempotency-Key'] = $options->idempotencyKey;
191192
}
192193

194+
// Always set User-Agent last so callers cannot override it via extraHeaders.
195+
$headers['User-Agent'] = $this->userAgent ?? sprintf('%s/%s', Version::SDK_IDENTIFIER, Version::SDK_VERSION);
196+
193197
$requestOptions = [
194198
'headers' => $headers,
195199
'http_errors' => false,

lib/WorkOS.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ public function __construct(
8080
int $timeout = 60,
8181
int $maxRetries = 3,
8282
?\GuzzleHttp\HandlerStack $handler = null,
83+
?string $userAgent = null,
8384
) {
8485
$apiKey ??= getenv('WORKOS_API_KEY') ?: self::$apiKey ?? '';
8586
$clientId ??= getenv('WORKOS_CLIENT_ID') ?: self::$clientId;
86-
$this->httpClient = new HttpClient($apiKey, $clientId, $baseUrl, $timeout, $maxRetries, $handler);
87+
$this->httpClient = new HttpClient($apiKey, $clientId, $baseUrl, $timeout, $maxRetries, $handler, $userAgent);
8788
}
8889

8990
public function apiKeys(): ApiKeys

tests/Service/RuntimeBehaviorTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use WorkOS\Exception\RateLimitExceededException;
1010
use WorkOS\RequestOptions;
1111
use WorkOS\TestHelper;
12+
use WorkOS\Version;
1213

1314
class RuntimeBehaviorTest extends TestCase
1415
{
@@ -125,6 +126,53 @@ public function testAuthenticationErrorsAreMapped(): void
125126
}
126127
}
127128

129+
public function testDefaultUserAgentIdentifiesTheSdk(): void
130+
{
131+
$client = $this->createMockClient([
132+
['status' => 200, 'body' => $this->loadFixture('organization')],
133+
]);
134+
135+
$client->organizations()->getOrganization('org_123');
136+
137+
$this->assertSame(
138+
sprintf('%s/%s', Version::SDK_IDENTIFIER, Version::SDK_VERSION),
139+
$this->getLastRequest()->getHeaderLine('User-Agent'),
140+
);
141+
}
142+
143+
public function testConstructorUserAgentOverridesDefault(): void
144+
{
145+
$client = $this->createMockClient(
146+
[['status' => 200, 'body' => $this->loadFixture('organization')]],
147+
userAgent: 'WorkOS PHP Laravel/5.1.0',
148+
);
149+
150+
$client->organizations()->getOrganization('org_123');
151+
152+
$this->assertSame(
153+
'WorkOS PHP Laravel/5.1.0',
154+
$this->getLastRequest()->getHeaderLine('User-Agent'),
155+
);
156+
}
157+
158+
public function testPerRequestExtraHeadersCannotOverrideUserAgent(): void
159+
{
160+
$client = $this->createMockClient(
161+
[['status' => 200, 'body' => $this->loadFixture('organization')]],
162+
userAgent: 'WorkOS PHP Laravel/5.1.0',
163+
);
164+
165+
$client->organizations()->getOrganization(
166+
'org_123',
167+
new RequestOptions(extraHeaders: ['User-Agent' => 'Custom/9.9.9']),
168+
);
169+
170+
$this->assertSame(
171+
'WorkOS PHP Laravel/5.1.0',
172+
$this->getLastRequest()->getHeaderLine('User-Agent'),
173+
);
174+
}
175+
128176
public function testRateLimitErrorsExposeRetryAfter(): void
129177
{
130178
$client = $this->createMockClient([

tests/TestHelper.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ protected function createMockClient(
3030
?string $clientId = 'test_client_id',
3131
string $baseUrl = 'https://api.workos.com',
3232
int $maxRetries = 3,
33+
?string $userAgent = null,
3334
): WorkOS {
3435
$mockResponses = array_map(
3536
fn (array $response) => new Response(
@@ -51,6 +52,7 @@ protected function createMockClient(
5152
baseUrl: $baseUrl,
5253
maxRetries: $maxRetries,
5354
handler: $handler,
55+
userAgent: $userAgent,
5456
);
5557
}
5658

0 commit comments

Comments
 (0)