Skip to content

Commit dabd2b9

Browse files
committed
Test Anthropic beta headers behavior
1 parent 3312380 commit dabd2b9

File tree

1 file changed

+100
-26
lines changed

1 file changed

+100
-26
lines changed

src/platform/tests/Bridge/Anthropic/ModelClientTest.php

Lines changed: 100 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,113 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\AI\Platform\Tests\Bridge\Anthropic;
12+
namespace Symfony\AI\Platform\Bridge\Anthropic\Tests;
1313

14+
use PHPUnit\Framework\Attributes\CoversClass;
1415
use PHPUnit\Framework\TestCase;
1516
use Symfony\AI\Platform\Bridge\Anthropic\Claude;
1617
use Symfony\AI\Platform\Bridge\Anthropic\ModelClient;
17-
use Symfony\Contracts\HttpClient\HttpClientInterface;
18-
use Symfony\Contracts\HttpClient\ResponseInterface;
18+
use Symfony\Component\HttpClient\MockHttpClient;
19+
use Symfony\Component\HttpClient\Response\MockResponse;
1920

21+
#[CoversClass(ModelClient::class)]
2022
class ModelClientTest extends TestCase
2123
{
22-
public function testBetaFeaturesOption()
24+
private MockHttpClient $httpClient;
25+
private ModelClient $modelClient;
26+
private Claude $model;
27+
28+
protected function setUp(): void
29+
{
30+
$this->model = new Claude();
31+
}
32+
33+
private function parseHeaders(array $headers): array
34+
{
35+
$parsed = [];
36+
foreach ($headers as $header) {
37+
if (strpos($header, ':') !== false) {
38+
[$key, $value] = explode(':', $header, 2);
39+
$parsed[trim($key)] = trim($value);
40+
}
41+
}
42+
return $parsed;
43+
}
44+
45+
public function testAnthropicBetaHeaderIsSetWithSingleBetaFeature(): void
2346
{
24-
$httpClient = $this->createMock(HttpClientInterface::class);
25-
$response = $this->createMock(ResponseInterface::class);
26-
27-
$httpClient->expects($this->once())
28-
->method('request')
29-
->with('POST', 'https://api.anthropic.com/v1/messages', [
30-
'headers' => [
31-
'x-api-key' => 'test-api-key',
32-
'anthropic-version' => '2023-06-01',
33-
'anthropic-beta' => 'tool-use',
34-
],
35-
'json' => [],
36-
])
37-
->willReturn($response);
38-
39-
// Use the mock HttpClient directly instead of wrapping it
40-
$client = new ModelClient($httpClient, 'test-api-key');
41-
$model = new Claude();
42-
$payload = [];
43-
$options = ['beta_features' => ['tool-use']];
44-
45-
$client->request($model, $payload, $options);
47+
$this->httpClient = new MockHttpClient(function ($method, $url, $options) {
48+
$this->assertEquals('POST', $method);
49+
$this->assertEquals('https://api.anthropic.com/v1/messages', $url);
50+
51+
$headers = $this->parseHeaders($options['headers']);
52+
53+
$this->assertArrayHasKey('anthropic-beta', $headers);
54+
$this->assertEquals('feature-1', $headers['anthropic-beta']);
55+
56+
return new MockResponse('{"success": true}');
57+
});
58+
59+
$this->modelClient = new ModelClient($this->httpClient, 'test-api-key');
60+
61+
$options = ['beta_features' => ['feature-1']];
62+
$result = $this->modelClient->request($this->model, ['message' => 'test'], $options);
63+
64+
$this->assertNotNull($result);
65+
}
66+
67+
public function testAnthropicBetaHeaderIsSetWithMultipleBetaFeatures(): void
68+
{
69+
$this->httpClient = new MockHttpClient(function ($method, $url, $options) {
70+
$headers = $this->parseHeaders($options['headers']);
71+
72+
$this->assertArrayHasKey('anthropic-beta', $headers);
73+
$this->assertEquals('feature-1,feature-2,feature-3', $headers['anthropic-beta']);
74+
75+
return new MockResponse('{"success": true}');
76+
});
77+
78+
$this->modelClient = new ModelClient($this->httpClient, 'test-api-key', '2023-06-01');
79+
80+
$options = ['beta_features' => ['feature-1', 'feature-2', 'feature-3']];
81+
$result = $this->modelClient->request($this->model, ['message' => 'test'], $options);
82+
83+
$this->assertNotNull($result);
84+
}
85+
86+
public function testAnthropicBetaHeaderIsNotSetWhenBetaFeaturesIsEmpty(): void
87+
{
88+
$this->httpClient = new MockHttpClient(function ($method, $url, $options) {
89+
$headers = $this->parseHeaders($options['headers']);
90+
91+
$this->assertArrayNotHasKey('anthropic-beta', $headers);
92+
93+
return new MockResponse('{"success": true}');
94+
});
95+
96+
$this->modelClient = new ModelClient($this->httpClient, 'test-api-key', '2023-06-01');
97+
98+
$options = ['beta_features' => []];
99+
$result = $this->modelClient->request($this->model, ['message' => 'test'], $options);
100+
101+
$this->assertNotNull($result);
102+
}
103+
104+
public function testAnthropicBetaHeaderIsNotSetWhenBetaFeaturesIsNotProvided(): void
105+
{
106+
$this->httpClient = new MockHttpClient(function ($method, $url, $options) {
107+
$headers = $this->parseHeaders($options['headers']);
108+
109+
$this->assertArrayNotHasKey('anthropic-beta', $headers);
110+
111+
return new MockResponse('{"success": true}');
112+
});
113+
114+
$this->modelClient = new ModelClient($this->httpClient, 'test-api-key', '2023-06-01');
115+
116+
$options = ['some_other_option' => 'value'];
117+
$result = $this->modelClient->request($this->model, ['message' => 'test'], $options);
118+
119+
$this->assertNotNull($result);
46120
}
47121
}

0 commit comments

Comments
 (0)