|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Tests\Bundle\Rest\EventListener; |
| 10 | + |
| 11 | +use Ibexa\Bundle\Rest\EventListener\SupportedMediaTypesSubscriber; |
| 12 | +use PHPUnit\Framework\TestCase; |
| 13 | +use Symfony\Component\HttpFoundation\HeaderBag; |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpKernel\Event\RequestEvent; |
| 16 | +use Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException; |
| 17 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
| 18 | + |
| 19 | +final class SupportedMediaTypesSubscriberTest extends TestCase |
| 20 | +{ |
| 21 | + /** @var \Symfony\Component\HttpKernel\HttpKernelInterface&\PHPUnit\Framework\MockObject\MockObject */ |
| 22 | + private HttpKernelInterface $kernel; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + $this->kernel = $this->createMock(HttpKernelInterface::class); |
| 29 | + } |
| 30 | + |
| 31 | + public function testDoesNothingWhenSupportedMediaTypesParameterIsNotSet(): void |
| 32 | + { |
| 33 | + $request = new Request(); |
| 34 | + $event = new RequestEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST); |
| 35 | + |
| 36 | + $subscriber = new SupportedMediaTypesSubscriber(); |
| 37 | + $subscriber->allowOnlySupportedMediaTypes($event); |
| 38 | + |
| 39 | + self::expectNotToPerformAssertions(); |
| 40 | + } |
| 41 | + |
| 42 | + public function testDoesNothingWhenSupportedMediaTypesParameterIsEmpty(): void |
| 43 | + { |
| 44 | + $request = new Request(); |
| 45 | + $request->attributes->set('supported_media_types', []); |
| 46 | + |
| 47 | + $subscriber = new SupportedMediaTypesSubscriber(); |
| 48 | + $event = new RequestEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST); |
| 49 | + |
| 50 | + $subscriber->allowOnlySupportedMediaTypes($event); |
| 51 | + self::expectNotToPerformAssertions(); |
| 52 | + } |
| 53 | + |
| 54 | + public function testDoesNothingWhenMediaTypeIsSupported(): void |
| 55 | + { |
| 56 | + $request = new Request(); |
| 57 | + $request->attributes->set('supported_media_types', ['json', 'xml']); |
| 58 | + $request->headers = new HeaderBag([ |
| 59 | + 'Content-Type' => 'application/vnd.ibexa.api.ContentCreate+json', |
| 60 | + 'Accept' => 'application/vnd.ibexa.api.ContentCreate+json', |
| 61 | + ]); |
| 62 | + |
| 63 | + $subscriber = new SupportedMediaTypesSubscriber(); |
| 64 | + $event = new RequestEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST); |
| 65 | + |
| 66 | + $subscriber->allowOnlySupportedMediaTypes($event); |
| 67 | + |
| 68 | + self::expectNotToPerformAssertions(); |
| 69 | + } |
| 70 | + |
| 71 | + public function testThrowsExceptionWhenHeaderTypeIsNotSupported(): void |
| 72 | + { |
| 73 | + $request = new Request(); |
| 74 | + $request->attributes->set('supported_media_types', ['json']); |
| 75 | + $request->headers = new HeaderBag([ |
| 76 | + 'Content-Type' => 'application/vnd.ibexa.api.ContentCreate+xml', |
| 77 | + 'Accept' => 'application/vnd.ibexa.api.ContentCreate+xml', |
| 78 | + ]); |
| 79 | + |
| 80 | + $subscriber = new SupportedMediaTypesSubscriber(); |
| 81 | + $event = new RequestEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST); |
| 82 | + |
| 83 | + $this->expectException(UnsupportedMediaTypeHttpException::class); |
| 84 | + $subscriber->allowOnlySupportedMediaTypes($event); |
| 85 | + } |
| 86 | + |
| 87 | + public function testThrowsExceptionWhenUnknownMediaTypeIsUsed(): void |
| 88 | + { |
| 89 | + $request = new Request(); |
| 90 | + $request->attributes->set('supported_media_types', ['yaml']); |
| 91 | + $request->headers = new HeaderBag([ |
| 92 | + 'Content-Type' => 'application/vnd.ibexa.api.ContentCreate+unknown', |
| 93 | + 'Accept' => 'application/vnd.ibexa.api.ContentCreate+unknown', |
| 94 | + ]); |
| 95 | + |
| 96 | + $subscriber = new SupportedMediaTypesSubscriber(); |
| 97 | + $event = new RequestEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST); |
| 98 | + |
| 99 | + $this->expectException(UnsupportedMediaTypeHttpException::class); |
| 100 | + $subscriber->allowOnlySupportedMediaTypes($event); |
| 101 | + } |
| 102 | + |
| 103 | + public function testThrowsExceptionWhenDifferentMediaTypesInHeadersAreUsed(): void |
| 104 | + { |
| 105 | + $request = new Request(); |
| 106 | + $request->attributes->set('supported_media_types', ['json']); |
| 107 | + $request->headers = new HeaderBag([ |
| 108 | + 'Content-Type' => 'application/vnd.ibexa.api.ContentCreate+json', |
| 109 | + 'Accept' => 'application/vnd.ibexa.api.ContentCreate+xml', |
| 110 | + ]); |
| 111 | + |
| 112 | + $subscriber = new SupportedMediaTypesSubscriber(); |
| 113 | + $event = new RequestEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST); |
| 114 | + |
| 115 | + $this->expectException(UnsupportedMediaTypeHttpException::class); |
| 116 | + $subscriber->allowOnlySupportedMediaTypes($event); |
| 117 | + } |
| 118 | + |
| 119 | + public function testApplicationJsonHeaderIsSupported(): void |
| 120 | + { |
| 121 | + $request = new Request(); |
| 122 | + $request->attributes->set('supported_media_types', ['json']); |
| 123 | + $request->headers = new HeaderBag([ |
| 124 | + 'Content-Type' => 'application/json', |
| 125 | + 'Accept' => 'application/json', |
| 126 | + ]); |
| 127 | + |
| 128 | + $subscriber = new SupportedMediaTypesSubscriber(); |
| 129 | + $event = new RequestEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST); |
| 130 | + |
| 131 | + $subscriber->allowOnlySupportedMediaTypes($event); |
| 132 | + |
| 133 | + self::expectNotToPerformAssertions(); |
| 134 | + } |
| 135 | +} |
0 commit comments