|
| 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\HeadSpyResource; |
| 18 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 19 | +use Symfony\Bundle\FrameworkBundle\Controller\ControllerHelper; |
| 20 | +use Symfony\Component\JsonStreamer\JsonStreamWriter; |
| 21 | + |
| 22 | +/** |
| 23 | + * On a HEAD request, API Platform must skip body construction so that the (lazy) |
| 24 | + * collection is never iterated: zero row SELECT. The spy paginator throws on |
| 25 | + * getIterator()/count(); a HEAD that does not throw proves no iteration occurred. |
| 26 | + */ |
| 27 | +final class HeadRequestTest extends ApiTestCase |
| 28 | +{ |
| 29 | + use SetupClassResourcesTrait; |
| 30 | + |
| 31 | + protected static ?bool $alwaysBootKernel = false; |
| 32 | + |
| 33 | + /** |
| 34 | + * @return class-string[] |
| 35 | + */ |
| 36 | + public static function getResources(): array |
| 37 | + { |
| 38 | + return [HeadSpyResource::class]; |
| 39 | + } |
| 40 | + |
| 41 | + public function testHeadDoesNotIterateCollection(): void |
| 42 | + { |
| 43 | + $response = self::createClient()->request('HEAD', '/head_spy_resources', [ |
| 44 | + 'headers' => ['Accept' => 'application/ld+json'], |
| 45 | + ]); |
| 46 | + |
| 47 | + $this->assertResponseStatusCodeSame(200); |
| 48 | + $this->assertEmpty($response->getContent(false)); |
| 49 | + |
| 50 | + $headers = array_change_key_case($response->getHeaders(false)); |
| 51 | + $this->assertArrayHasKey('content-type', $headers); |
| 52 | + $this->assertStringStartsWith('application/ld+json', $headers['content-type'][0]); |
| 53 | + $this->assertArrayHasKey('vary', $headers); |
| 54 | + $this->assertStringContainsString('Accept', $headers['vary'][0]); |
| 55 | + } |
| 56 | + |
| 57 | + public function testGetIteratesCollection(): void |
| 58 | + { |
| 59 | + // Sanity check: the spy throws when iterated, so GET (which builds the body) |
| 60 | + // must surface a 500. Without this, the HEAD assertion above would be vacuous. |
| 61 | + self::createClient()->request('GET', '/head_spy_resources', [ |
| 62 | + 'headers' => ['Accept' => 'application/ld+json'], |
| 63 | + ]); |
| 64 | + |
| 65 | + $this->assertResponseStatusCodeSame(500); |
| 66 | + } |
| 67 | + |
| 68 | + public function testOptionsIsUnaffected(): void |
| 69 | + { |
| 70 | + $response = self::createClient()->request('OPTIONS', '/head_spy_resources', [ |
| 71 | + 'headers' => ['Accept' => 'application/ld+json'], |
| 72 | + ]); |
| 73 | + |
| 74 | + $headers = array_change_key_case($response->getHeaders(false)); |
| 75 | + $this->assertArrayHasKey('allow', $headers); |
| 76 | + $this->assertStringContainsString('GET', $headers['allow'][0]); |
| 77 | + } |
| 78 | + |
| 79 | + public function testHeadDoesNotIterateJsonStreamCollection(): void |
| 80 | + { |
| 81 | + if (false === (class_exists(ControllerHelper::class) && class_exists(JsonStreamWriter::class))) { |
| 82 | + $this->markTestSkipped('JsonStreamer component not installed.'); |
| 83 | + } |
| 84 | + |
| 85 | + $response = self::createClient()->request('HEAD', '/head_spy_stream_resources', [ |
| 86 | + 'headers' => ['Accept' => 'application/ld+json'], |
| 87 | + ]); |
| 88 | + |
| 89 | + $this->assertResponseStatusCodeSame(200); |
| 90 | + $this->assertEmpty($response->getContent(false)); |
| 91 | + |
| 92 | + $headers = array_change_key_case($response->getHeaders(false)); |
| 93 | + $this->assertArrayHasKey('content-type', $headers); |
| 94 | + $this->assertArrayHasKey('vary', $headers); |
| 95 | + $this->assertStringContainsString('Accept', $headers['vary'][0]); |
| 96 | + } |
| 97 | +} |
0 commit comments