|
| 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; |
| 15 | + |
| 16 | +use ApiPlatform\Symfony\Bundle\Test\ApiTestCase; |
| 17 | +use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\JsonLd\PostNoOutputResource; |
| 18 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 19 | + |
| 20 | +/** |
| 21 | + * RFC 9110 §10.2.1: the Allow header must advertise only methods that are actually |
| 22 | + * valid for the target resource. HEAD is defined as GET-without-body (§9.3.2), so a |
| 23 | + * resource that declares no GET operation does not support HEAD — a real HEAD request |
| 24 | + * returns 405. The advertised Allow header must therefore not claim HEAD either. |
| 25 | + */ |
| 26 | +final class HeadAllowWithoutGetTest extends ApiTestCase |
| 27 | +{ |
| 28 | + use SetupClassResourcesTrait; |
| 29 | + |
| 30 | + protected static ?bool $alwaysBootKernel = false; |
| 31 | + |
| 32 | + /** |
| 33 | + * @return class-string[] |
| 34 | + */ |
| 35 | + public static function getResources(): array |
| 36 | + { |
| 37 | + return [PostNoOutputResource::class]; |
| 38 | + } |
| 39 | + |
| 40 | + public function testHeadIsNotAdvertisedWithoutGetOperation(): void |
| 41 | + { |
| 42 | + $client = self::createClient(); |
| 43 | + |
| 44 | + $client->request('HEAD', '/jsonld_post_no_output', ['headers' => ['Accept' => 'application/ld+json']]); |
| 45 | + $this->assertResponseStatusCodeSame(405); |
| 46 | + |
| 47 | + $response = $client->request('POST', '/jsonld_post_no_output', [ |
| 48 | + 'headers' => ['Content-Type' => 'application/ld+json'], |
| 49 | + 'json' => ['lorem' => 'x'], |
| 50 | + ]); |
| 51 | + |
| 52 | + $headers = array_change_key_case($response->getHeaders(false)); |
| 53 | + $this->assertArrayHasKey('allow', $headers); |
| 54 | + $this->assertStringNotContainsString('HEAD', $headers['allow'][0]); |
| 55 | + } |
| 56 | +} |
0 commit comments