67 lines
2.2 KiB
PHP
67 lines
2.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\Api\V1;
|
|
|
|
use Presentation\Api\V1\Http\Middlewares\EnsureApiV1AcceptsJson;
|
|
use Presentation\Api\V1\Http\Middlewares\SetApiV1Etag;
|
|
|
|
covers(
|
|
EnsureApiV1AcceptsJson::class,
|
|
SetApiV1Etag::class
|
|
);
|
|
|
|
describe('API V1 Middlewares Feature Tests', function (): void {
|
|
it('returns ETag header on 200 GET requests and 304 Not Modified when If-None-Match matches', function (): void {
|
|
// GIVEN
|
|
/** @var \Tests\TestCase $this */
|
|
$response = $this->getJson('/api/v1/employees');
|
|
|
|
// THEN
|
|
$response->assertStatus(200);
|
|
$etag = $response->headers->get('ETag');
|
|
expect($etag)->toBeString()->and($etag)->not->toBeEmpty();
|
|
|
|
// WHEN: Sending conditional GET with matching ETag
|
|
$conditionalResponse = $this->withHeaders([
|
|
'If-None-Match' => (string) $etag,
|
|
])->getJson('/api/v1/employees');
|
|
|
|
// THEN: Returns 304 Not Modified
|
|
$conditionalResponse->assertStatus(304);
|
|
expect($conditionalResponse->getContent())->toBeEmpty();
|
|
});
|
|
|
|
it('returns 406 Not Acceptable when Accept header explicitly excludes JSON', function (): void {
|
|
// GIVEN
|
|
/** @var \Tests\TestCase $this */
|
|
$response = $this->withHeaders([
|
|
'Accept' => 'text/html',
|
|
])->get('/api/v1/employees');
|
|
|
|
// THEN
|
|
$response->assertStatus(406)
|
|
->assertHeader('Content-Type', 'application/problem+json');
|
|
|
|
/** @var array<string, mixed> $data */
|
|
$data = json_decode((string) $response->getContent(), true);
|
|
expect($data['status'])->toBe(406)
|
|
->and($data['title'])->toBe('Not Acceptable');
|
|
});
|
|
|
|
it('allows GET /api/v1/openapi route to return YAML spec without forcing JSON or returning 406', function (): void {
|
|
// GIVEN
|
|
/** @var \Tests\TestCase $this */
|
|
$response = $this->withHeaders([
|
|
'Accept' => 'application/yaml',
|
|
])->get('/api/v1/openapi');
|
|
|
|
// THEN
|
|
$response->assertStatus(200)
|
|
->assertHeader('Content-Type', 'application/yaml');
|
|
|
|
$etag = $response->headers->get('ETag');
|
|
expect($etag)->toBeString()->and($etag)->not->toBeEmpty();
|
|
});
|
|
});
|