Skip to content

Commit

Permalink
Add test case for headers with different cases
Browse files Browse the repository at this point in the history
  • Loading branch information
gehrisandro committed Dec 22, 2023
1 parent ec1dde4 commit ce84f54
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Responses/Meta/MetaInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ private function __construct(
public static function from(array $headers): self
{
$headers = array_change_key_case($headers, CASE_LOWER);

$requestId = $headers['x-request-id'][0] ?? null;

$openai = MetaInformationOpenAI::from([
Expand Down
11 changes: 11 additions & 0 deletions tests/Fixtures/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ function metaHeadersFromAzure(): array
];
}

function metaHeadersWithDifferentCases(): array
{
return [
'Openai-Model' => ['gpt-3.5-turbo-instruct'],
'OPENAI-ORGANIZATION' => ['org-1234'],
'openai-processing-ms' => [410],
'openai-version' => ['2020-10-01'],
'x-request-id' => ['3813fa4fa3f17bdf0d7654f0f49ebab4'],
];
}

function meta(): MetaInformation
{
return MetaInformation::from(metaHeaders());
Expand Down
15 changes: 15 additions & 0 deletions tests/Responses/Meta/MetaInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@
->openai->processingMs->toBeNull();
});

test('from response headers in different cases', function () {
$meta = MetaInformation::from((new \GuzzleHttp\Psr7\Response(headers: metaHeadersWithDifferentCases()))->getHeaders());

expect($meta)
->toBeInstanceOf(MetaInformation::class)
->requestId->toBe('3813fa4fa3f17bdf0d7654f0f49ebab4')
->openai->toBeInstanceOf(MetaInformationOpenAI::class)
->openai->model->toBe('gpt-3.5-turbo-instruct')
->openai->organization->toBe('org-1234')
->openai->version->toBe('2020-10-01')
->openai->processingMs->toBe(410)
->requestLimit->toBeNull()
->tokenLimit->toBeNull();
});

test('as array accessible', function () {
$meta = MetaInformation::from(metaHeaders());

Expand Down

0 comments on commit ce84f54

Please sign in to comment.