Skip to content

Commit 78dba9c

Browse files
konradobozaadamwojs
authored andcommitted
cr remarks 2
1 parent aa5ac91 commit 78dba9c

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/bundle/EventListener/SupportedMediaTypesSubscriber.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,16 @@ public function allowOnlySupportedMediaTypes(RequestEvent $event): void
3939
return;
4040
}
4141

42-
$contentTypeHeader = $request->headers->get('Content-Type') ?? '';
42+
$acceptHeader = $request->headers->get('Accept') ?? '';
4343

44-
preg_match(self::SUPPORTED_MEDIA_TYPES_REGEX, $contentTypeHeader, $matches);
44+
preg_match(self::SUPPORTED_MEDIA_TYPES_REGEX, $acceptHeader, $matches);
4545

4646
$match = reset($matches);
47-
if ($match !== false && in_array($match, $supportedMediaTypes, true)) {
47+
if ($match === false) {
48+
return;
49+
}
50+
51+
if (in_array($match, $supportedMediaTypes, true)) {
4852
return;
4953
}
5054

tests/bundle/EventListener/SupportedMediaTypesSubscriberTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testDoesNothingWhenMediaTypeIsSupported(): void
5656
$request = new Request();
5757
$request->attributes->set('supported_media_types', ['json', 'xml']);
5858
$request->headers = new HeaderBag([
59-
'Content-Type' => 'application/vnd.ibexa.api.ContentCreate+json',
59+
'Accept' => 'application/vnd.ibexa.api.ContentCreate+json',
6060
]);
6161

6262
$subscriber = new SupportedMediaTypesSubscriber();
@@ -67,12 +67,12 @@ public function testDoesNothingWhenMediaTypeIsSupported(): void
6767
self::expectNotToPerformAssertions();
6868
}
6969

70-
public function testThrowsExceptionWhenContentTypeHeaderTypeIsNotSupported(): void
70+
public function testThrowsExceptionWhenHeaderTypeIsNotSupported(): void
7171
{
7272
$request = new Request();
7373
$request->attributes->set('supported_media_types', ['json']);
7474
$request->headers = new HeaderBag([
75-
'Content-Type' => 'application/vnd.ibexa.api.ContentCreate+xml',
75+
'Accept' => 'application/vnd.ibexa.api.ContentCreate+xml',
7676
]);
7777

7878
$subscriber = new SupportedMediaTypesSubscriber();
@@ -87,7 +87,7 @@ public function testThrowsExceptionWhenUnknownMediaTypeIsUsed(): void
8787
$request = new Request();
8888
$request->attributes->set('supported_media_types', ['yaml']);
8989
$request->headers = new HeaderBag([
90-
'Content-Type' => 'application/vnd.ibexa.api.ContentCreate+unknown',
90+
'Accept' => 'application/vnd.ibexa.api.ContentCreate+unknown',
9191
]);
9292

9393
$subscriber = new SupportedMediaTypesSubscriber();
@@ -96,4 +96,20 @@ public function testThrowsExceptionWhenUnknownMediaTypeIsUsed(): void
9696
$this->expectException(UnsupportedMediaTypeHttpException::class);
9797
$subscriber->allowOnlySupportedMediaTypes($event);
9898
}
99+
100+
public function testApplicationJsonHeaderIsSupported(): void
101+
{
102+
$request = new Request();
103+
$request->attributes->set('supported_media_types', ['json']);
104+
$request->headers = new HeaderBag([
105+
'Accept' => 'application/json',
106+
]);
107+
108+
$subscriber = new SupportedMediaTypesSubscriber();
109+
$event = new RequestEvent($this->kernel, $request, HttpKernelInterface::MAIN_REQUEST);
110+
111+
$subscriber->allowOnlySupportedMediaTypes($event);
112+
113+
self::expectNotToPerformAssertions();
114+
}
99115
}

0 commit comments

Comments
 (0)