Skip to content
Merged
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
4 changes: 3 additions & 1 deletion src/Flagsmith.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Flagsmith\Utils\AnalyticsProcessor;
use Flagsmith\Utils\IdentitiesGenerator;
use Flagsmith\Utils\Retry;
use Flagsmith\Utils\UserAgent;
use JsonException;
use ValueError;
use Psr\Http\Client\ClientInterface;
Expand Down Expand Up @@ -596,7 +597,8 @@ private function call(string $method, string $uri, array $body = [])
->createRequest($method, rtrim($this->host, '/') . '/' . $uri)
->withHeader('Accept', 'application/json')
->withHeader('Content-Type', 'application/json')
->withHeader('X-Environment-Key', $this->apiKey);
->withHeader('X-Environment-Key', $this->apiKey)
->withHeader('User-Agent', UserAgent::get());

if (!empty($this->customHeaders)) {
foreach ($this->customHeaders as $name => $value) {
Expand Down
1 change: 1 addition & 0 deletions src/Utils/AnalyticsProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function flush()
->withHeader('Accept', 'application/json')
->withHeader('Content-Type', 'application/json')
->withHeader('X-Environment-Key', $this->environment_key)
->withHeader('User-Agent', UserAgent::get())
->withBody($stream);

try {
Expand Down
32 changes: 32 additions & 0 deletions src/Utils/UserAgent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Flagsmith\Utils;

/**
* UserAgent utility class for generating SDK user agent strings.
*/
class UserAgent
{
/**
* Get the user agent string for the SDK.
*
* @return string The user agent string in the format "flagsmith-php-sdk/{version}"
*/
public static function get(): string
{
try {
$composerPath = __DIR__ . '/../../composer.json';
$content = file_get_contents($composerPath);
$data = json_decode($content, true);
$version = $data['version'] ?? null;

if ($version) {
return "flagsmith-php-sdk/{$version}";
}
} catch (\Exception $e) {
// Silently fall through to default
}

return 'flagsmith-php-sdk/unknown';
}
}
15 changes: 14 additions & 1 deletion tests/AnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,27 @@ public function testAnalyticsProcessorFlushClearsAnalyticsData()

public function testAnalyticsProcessorFlushPostRequestDataMatchAnanlyticsData()
{
$capturedRequest = null;
$client = $this->createMock(ClientInterface::class);

$analyticsProcessor = ClientFixtures::analyticsProcessor($client);
$client->expects($this->once())
->method('sendRequest');
->method('sendRequest')
->with($this->callback(function ($request) use (&$capturedRequest) {
$capturedRequest = $request;
return true;
}));

$analyticsProcessor->trackFeature('my_feature');
$analyticsProcessor->flush();

$this->assertNotNull($capturedRequest);
$this->assertTrue($capturedRequest->hasHeader('User-Agent'));
$userAgent = $capturedRequest->getHeaderLine('User-Agent');
$composerData = json_decode(file_get_contents(__DIR__ . '/../composer.json'), true);
$expectedVersion = $composerData['version'] ?? 'unknown';
$expectedUserAgent = "flagsmith-php-sdk/{$expectedVersion}";
$this->assertEquals($expectedUserAgent, $userAgent);
}

public function testAnalyticsProcessorFlushEarlyExitIfAnalyticsDataIsEmpty()
Expand Down
29 changes: 29 additions & 0 deletions tests/FlagsmithClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use FlagsmithTest\Offline\FakeOfflineHandler;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\StreamFactoryInterface;

class FlagsmithClientTest extends TestCase
Expand Down Expand Up @@ -441,4 +442,32 @@ public function testOfflineHandlerUsedAsFallbackForLocalEvaluation()
$this->assertEquals($identityFlags->getFlag('some_feature')->enabled, true);
$this->assertEquals($identityFlags->getFlag('some_feature')->value, 'some-value');
}

public function testApiRequestsIncludeUserAgentHeader()
{
$capturedRequest = null;
$mockClient = $this->createMock(ClientInterface::class);
$mockClient->expects($this->once())
->method('sendRequest')
->with($this->callback(function ($request) use (&$capturedRequest) {
$capturedRequest = $request;
return true;
}))
->willReturn(
new Response(200, ['Content-Type' => 'application/json'], file_get_contents(__DIR__ . '/Data/flags.json'))
);

$flagsmith = (new Flagsmith('api_key'))
->withClient($mockClient);

$flagsmith->getEnvironmentFlags();

$this->assertNotNull($capturedRequest);
$this->assertTrue($capturedRequest->hasHeader('User-Agent'));
$userAgent = $capturedRequest->getHeaderLine('User-Agent');
$composerData = json_decode(file_get_contents(__DIR__ . '/../composer.json'), true);
$expectedVersion = $composerData['version'] ?? 'unknown';
$expectedUserAgent = "flagsmith-php-sdk/{$expectedVersion}";
$this->assertEquals($expectedUserAgent, $userAgent);
}
}
18 changes: 18 additions & 0 deletions tests/UserAgentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use PHPUnit\Framework\TestCase;
use Flagsmith\Utils\UserAgent;

class UserAgentTest extends TestCase
{
public function testGetUserAgentReturnsVersionFromComposerJson()
{
$userAgent = UserAgent::get();

$composerPath = __DIR__ . '/../composer.json';
$composerData = json_decode(file_get_contents($composerPath), true);
$expectedVersion = $composerData['version'] ?? 'unknown';

$this->assertEquals("flagsmith-php-sdk/{$expectedVersion}", $userAgent);
}
}