Skip to content

Commit

Permalink
Merge pull request #23 from shouze/fix-a-415-http-code-handling-edge-…
Browse files Browse the repository at this point in the history
…case

🐛 Fix a 415 http code handling edge case
  • Loading branch information
tyx authored Sep 6, 2017
2 parents f7dc2b9 + 6bd8c2d commit 6b5cb8c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/JsonBodyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public function onKernelRequest(GetResponseEvent $event)

private function requestFormatViolateSupportedFormats($format, $supportedFormats)
{
return null !== $format
&& false !== $supportedFormats
return false !== $supportedFormats
&& false === in_array($format, $supportedFormats, true)
;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/Units/JsonBodyListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,35 @@ public function test_it_try_to_validate_payload_when_jsonSchema_is_present()
;
}

/**
* @dataProvider supportedFormats
*/
public function test_it_check_request_format_when_supported_format_contains_json($contentType)
{
$this
->given(
$request = $this->requestWithContent('POST', $contentType, '{}'),
$request->attributes->set('_supportedFormats', ['json']),
$mockEvent = $this->eventOccuredByRequest($request)
)
->exception(function () use ($mockEvent) {
$this->newTestedInstance($this->mockPayloadValidator());
$this->testedInstance->onKernelRequest($mockEvent);
})
->isInstanceOf('\Symfony\Component\HttpKernel\Exception\UnsupportedMediaTypeHttpException')
;
}

protected function supportedFormats()
{
return [
'Request has a known content type' => ['application/xml'],
'Request has an unknown content type' => ['application/jsond'],
'Request has an empty content type' => [''],
'Request has a null content type' => [null],
];
}

private function requestWithContent($method, $contentType, $content)
{
return \Symfony\Component\HttpFoundation\Request::create(
Expand Down

0 comments on commit 6b5cb8c

Please sign in to comment.