|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <dunglas@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Tests\Functional\Parameters; |
| 15 | + |
| 16 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 17 | +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\WithResponseHeaderParameter; |
| 18 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 19 | + |
| 20 | +final class ResponseHeaderParameterTest extends ApiTestCase |
| 21 | +{ |
| 22 | + use SetupClassResourcesTrait; |
| 23 | + |
| 24 | + protected static ?bool $alwaysBootKernel = false; |
| 25 | + |
| 26 | + /** |
| 27 | + * @return class-string[] |
| 28 | + */ |
| 29 | + public static function getResources(): array |
| 30 | + { |
| 31 | + return [WithResponseHeaderParameter::class]; |
| 32 | + } |
| 33 | + |
| 34 | + public function testResponseHeadersAreSet(): void |
| 35 | + { |
| 36 | + self::createClient()->request('GET', 'with_response_headers/1'); |
| 37 | + $this->assertResponseIsSuccessful(); |
| 38 | + $this->assertResponseHeaderSame('ratelimit-limit', '100'); |
| 39 | + $this->assertResponseHeaderSame('ratelimit-remaining', '99'); |
| 40 | + } |
| 41 | + |
| 42 | + public function testProcessorSetsResponseHeaders(): void |
| 43 | + { |
| 44 | + self::createClient()->request('POST', 'with_response_headers', [ |
| 45 | + 'headers' => ['Content-Type' => 'application/ld+json'], |
| 46 | + 'json' => ['id' => '3', 'name' => 'test'], |
| 47 | + ]); |
| 48 | + $this->assertResponseIsSuccessful(); |
| 49 | + $this->assertResponseHeaderSame('ratelimit-limit', '50'); |
| 50 | + $this->assertResponseHeaderSame('ratelimit-remaining', '49'); |
| 51 | + } |
| 52 | + |
| 53 | + public function testOpenApiDocumentsResponseHeaders(): void |
| 54 | + { |
| 55 | + $response = self::createClient()->request('GET', 'docs', ['headers' => ['Accept' => 'application/vnd.openapi+json']]); |
| 56 | + $this->assertResponseIsSuccessful(); |
| 57 | + |
| 58 | + $json = $response->toArray(); |
| 59 | + |
| 60 | + $itemPath = $json['paths']['/with_response_headers/{id}']['get']; |
| 61 | + $this->assertArrayHasKey('responses', $itemPath); |
| 62 | + |
| 63 | + $successResponse = $itemPath['responses']['200'] ?? $itemPath['responses'][200] ?? null; |
| 64 | + $this->assertNotNull($successResponse); |
| 65 | + $this->assertArrayHasKey('headers', $successResponse); |
| 66 | + $this->assertArrayHasKey('RateLimit-Limit', $successResponse['headers']); |
| 67 | + $this->assertArrayHasKey('RateLimit-Remaining', $successResponse['headers']); |
| 68 | + $this->assertSame('integer', $successResponse['headers']['RateLimit-Limit']['schema']['type']); |
| 69 | + $this->assertSame('Maximum number of requests per window', $successResponse['headers']['RateLimit-Limit']['description']); |
| 70 | + |
| 71 | + // Verify headers are NOT in request parameters |
| 72 | + foreach ($itemPath['parameters'] ?? [] as $parameter) { |
| 73 | + $this->assertNotSame('RateLimit-Limit', $parameter['name']); |
| 74 | + $this->assertNotSame('RateLimit-Remaining', $parameter['name']); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments