Skip to content

Commit 6b5cb8c

Browse files
authored
Merge pull request #23 from shouze/fix-a-415-http-code-handling-edge-case
🐛 Fix a 415 http code handling edge case
2 parents f7dc2b9 + 6bd8c2d commit 6b5cb8c

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/JsonBodyListener.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public function onKernelRequest(GetResponseEvent $event)
6060

6161
private function requestFormatViolateSupportedFormats($format, $supportedFormats)
6262
{
63-
return null !== $format
64-
&& false !== $supportedFormats
63+
return false !== $supportedFormats
6564
&& false === in_array($format, $supportedFormats, true)
6665
;
6766
}

tests/Units/JsonBodyListener.php

+29
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,35 @@ public function test_it_try_to_validate_payload_when_jsonSchema_is_present()
151151
;
152152
}
153153

154+
/**
155+
* @dataProvider supportedFormats
156+
*/
157+
public function test_it_check_request_format_when_supported_format_contains_json($contentType)
158+
{
159+
$this
160+
->given(
161+
$request = $this->requestWithContent('POST', $contentType, '{}'),
162+
$request->attributes->set('_supportedFormats', ['json']),
163+
$mockEvent = $this->eventOccuredByRequest($request)
164+
)
165+
->exception(function () use ($mockEvent) {
166+
$this->newTestedInstance($this->mockPayloadValidator());
167+
$this->testedInstance->onKernelRequest($mockEvent);
168+
})
169+
->isInstanceOf('\Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException')
170+
;
171+
}
172+
173+
protected function supportedFormats()
174+
{
175+
return [
176+
'Request has a known content type' => ['application/xml'],
177+
'Request has an unknown content type' => ['application/jsond'],
178+
'Request has an empty content type' => [''],
179+
'Request has a null content type' => [null],
180+
];
181+
}
182+
154183
private function requestWithContent($method, $contentType, $content)
155184
{
156185
return \Symfony\Component\HttpFoundation\Request::create(

0 commit comments

Comments
 (0)