|
| 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\MultipleResourceBook; |
| 18 | +use ApiPlatform\Tests\SetupClassResourcesTrait; |
| 19 | + |
| 20 | +/** |
| 21 | + * The entrypoint must only expose hypermedia formats that have a dedicated |
| 22 | + * EntrypointNormalizer (jsonld, jsonhal, jsonapi). Documentation formats |
| 23 | + * (openapi, html) have no such normalizer and must not be served, otherwise |
| 24 | + * the Symfony ObjectNormalizer fallback dumps the public ResourceNameCollection, |
| 25 | + * leaking internal PHP FQCNs. |
| 26 | + * |
| 27 | + * @see https://github.com/api-platform/core/issues/8361 |
| 28 | + */ |
| 29 | +final class EntrypointFormatTest extends ApiTestCase |
| 30 | +{ |
| 31 | + use SetupClassResourcesTrait; |
| 32 | + |
| 33 | + protected static ?bool $alwaysBootKernel = false; |
| 34 | + |
| 35 | + /** |
| 36 | + * @return class-string[] |
| 37 | + */ |
| 38 | + public static function getResources(): array |
| 39 | + { |
| 40 | + return [MultipleResourceBook::class]; |
| 41 | + } |
| 42 | + |
| 43 | + public function testEntrypointRejectsOpenApiAcceptHeader(): void |
| 44 | + { |
| 45 | + $response = self::createClient()->request('GET', '/', [ |
| 46 | + 'headers' => ['accept' => 'application/vnd.openapi+json'], |
| 47 | + ]); |
| 48 | + |
| 49 | + $this->assertResponseStatusCodeSame(406); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * The ".jsonopenapi"/".yamlopenapi" URL suffixes are resolved by routing before |
| 54 | + * content negotiation runs, so an unsupported route format yields a 404 |
| 55 | + * (consistent with any other resource requested with an unsupported format |
| 56 | + * suffix), not a 406. |
| 57 | + */ |
| 58 | + public function testEntrypointRejectsOpenApiFormatSuffix(): void |
| 59 | + { |
| 60 | + $response = self::createClient()->request('GET', '/index.jsonopenapi', [ |
| 61 | + 'headers' => ['accept' => 'application/vnd.openapi+json'], |
| 62 | + ]); |
| 63 | + |
| 64 | + $this->assertResponseStatusCodeSame(404); |
| 65 | + } |
| 66 | + |
| 67 | + public function testEntrypointRejectsYamlOpenApiFormatSuffix(): void |
| 68 | + { |
| 69 | + $response = self::createClient()->request('GET', '/index.yamlopenapi', [ |
| 70 | + 'headers' => ['accept' => 'application/vnd.openapi+yaml'], |
| 71 | + ]); |
| 72 | + |
| 73 | + $this->assertResponseStatusCodeSame(404); |
| 74 | + } |
| 75 | + |
| 76 | + public function testEntrypointStillServesHtmlAsSwaggerUi(): void |
| 77 | + { |
| 78 | + $response = self::createClient()->request('GET', '/index.html', [ |
| 79 | + 'headers' => ['accept' => 'text/html'], |
| 80 | + ]); |
| 81 | + |
| 82 | + $this->assertResponseIsSuccessful(); |
| 83 | + $this->assertStringContainsString('swagger-ui', $response->getContent()); |
| 84 | + } |
| 85 | + |
| 86 | + public function testEntrypointStillServesJsonLd(): void |
| 87 | + { |
| 88 | + $response = self::createClient()->request('GET', '/index.jsonld', [ |
| 89 | + 'headers' => ['accept' => 'application/ld+json'], |
| 90 | + ]); |
| 91 | + |
| 92 | + $this->assertResponseIsSuccessful(); |
| 93 | + $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8'); |
| 94 | + $data = $response->toArray(); |
| 95 | + $this->assertArrayHasKey('multipleResourceBook2', $data); |
| 96 | + $this->assertEquals('/multi_route_books', $data['multipleResourceBook2']); |
| 97 | + $this->assertStringNotContainsString('resourceNameCollection', $response->getContent()); |
| 98 | + } |
| 99 | + |
| 100 | + public function testEntrypointStillServesJsonHal(): void |
| 101 | + { |
| 102 | + $response = self::createClient()->request('GET', '/index.jsonhal', [ |
| 103 | + 'headers' => ['accept' => 'application/hal+json'], |
| 104 | + ]); |
| 105 | + |
| 106 | + $this->assertResponseIsSuccessful(); |
| 107 | + $this->assertResponseHeaderSame('content-type', 'application/hal+json; charset=utf-8'); |
| 108 | + $data = $response->toArray(); |
| 109 | + $this->assertArrayHasKey('_links', $data); |
| 110 | + $this->assertArrayHasKey('multipleResourceBook2', $data['_links']); |
| 111 | + } |
| 112 | + |
| 113 | + public function testEntrypointStillServesJsonApi(): void |
| 114 | + { |
| 115 | + $response = self::createClient()->request('GET', '/index.jsonapi', [ |
| 116 | + 'headers' => ['accept' => 'application/vnd.api+json'], |
| 117 | + ]); |
| 118 | + |
| 119 | + $this->assertResponseIsSuccessful(); |
| 120 | + $this->assertResponseHeaderSame('content-type', 'application/vnd.api+json; charset=utf-8'); |
| 121 | + $data = $response->toArray(); |
| 122 | + $this->assertArrayHasKey('links', $data); |
| 123 | + $this->assertArrayHasKey('multipleResourceBook2', $data['links']); |
| 124 | + } |
| 125 | +} |
0 commit comments