Skip to content

Commit

Permalink
Merge pull request #11 from tdutrion/feature/trigger-on-accept-header
Browse files Browse the repository at this point in the history
Trigger the plugin on json accept header
  • Loading branch information
veewee authored Nov 4, 2020
2 parents 64ab168 + 630d87c commit 1cf51a9
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ SomeController {
}
```

When the controller is marked as a "json" format or the request `Content-Type` is `*/json`, this bundle kicks in.
When the controller is marked as a "json" format, the request `Content-Type` is `*/json` or the request `Accept` header first value contains json (i.e. `application/json, text/html`), this bundle kicks in.
It will transform the exception to following response:

Headers:
Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/JsonApiProblemExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function onKernelException(ExceptionEvent $event): void
{
$request = $event->getRequest();
if (
false === mb_strpos($request->getRequestFormat(), 'json') &&
false === mb_strpos($request->getPreferredFormat(), 'json') &&
false === mb_strpos((string) $request->getContentType(), 'json')
) {
return;
Expand Down
25 changes: 18 additions & 7 deletions test/EventListener/JsonApiProblemExceptionListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected function setUp(): void
public function it_does_nothing_on_non_json_requests(): void
{
$listener = new JsonApiProblemExceptionListener($this->exceptionTransformer->reveal(), false);
$this->request->getRequestFormat()->willReturn('html');
$this->request->getPreferredFormat()->willReturn('html');
$this->request->getContentType()->willReturn('text/html');
$listener->onKernelException($this->event);

Expand All @@ -70,7 +70,7 @@ public function it_does_nothing_on_non_json_requests(): void
public function it_runs_on_json_route_formats(): void
{
$listener = new JsonApiProblemExceptionListener($this->exceptionTransformer->reveal(), false);
$this->request->getRequestFormat()->willReturn('json');
$this->request->getPreferredFormat()->willReturn('json');
$this->request->getContentType()->willReturn(null);
$listener->onKernelException($this->event);

Expand All @@ -81,18 +81,29 @@ public function it_runs_on_json_route_formats(): void
public function it_runs_on_json_content_types(): void
{
$listener = new JsonApiProblemExceptionListener($this->exceptionTransformer->reveal(), false);
$this->request->getRequestFormat()->willReturn('html');
$this->request->getPreferredFormat()->willReturn('html');
$this->request->getContentType()->willReturn('application/json');

$listener->onKernelException($this->event);
$this->assertApiProblemWithResponseBody(500, $this->parseDataForException());
}

/** @test */
public function it_runs_on_json_accept_header(): void
{
$listener = new JsonApiProblemExceptionListener($this->exceptionTransformer->reveal(), false);
$this->request->getPreferredFormat()->willReturn('json');
$this->request->getContentType()->willReturn('html');

$listener->onKernelException($this->event);
$this->assertApiProblemWithResponseBody(500, $this->parseDataForException());
}

/** @test */
public function it_parses_an_api_problem_response(): void
{
$listener = new JsonApiProblemExceptionListener($this->exceptionTransformer->reveal(), false);
$this->request->getRequestFormat()->willReturn('json');
$this->request->getPreferredFormat()->willReturn('json');
$this->request->getContentType()->willReturn('application/json');

$listener->onKernelException($this->event);
Expand All @@ -103,7 +114,7 @@ public function it_parses_an_api_problem_response(): void
public function it_uses_an_exception_transformer(): void
{
$listener = new JsonApiProblemExceptionListener($this->exceptionTransformer->reveal(), false);
$this->request->getRequestFormat()->willReturn('json');
$this->request->getPreferredFormat()->willReturn('json');
$this->request->getContentType()->willReturn('application/json');

$apiProblem = $this->prophesize(ApiProblemInterface::class);
Expand All @@ -120,7 +131,7 @@ public function it_uses_an_exception_transformer(): void
public function it_returns_the_status_code_from_the_api_problem(): void
{
$listener = new JsonApiProblemExceptionListener($this->exceptionTransformer->reveal(), false);
$this->request->getRequestFormat()->willReturn('json');
$this->request->getPreferredFormat()->willReturn('json');
$this->request->getContentType()->willReturn('application/json');

$apiProblem = $this->prophesize(ApiProblemInterface::class);
Expand All @@ -146,7 +157,7 @@ public function it_parses_a_debuggable_api_problem_response(): void
$this->exceptionTransformer->accepts($this->exception)->willReturn(true);
$this->exceptionTransformer->transform($this->exception)->willReturn($apiProblem->reveal());

$this->request->getRequestFormat()->willReturn('json');
$this->request->getPreferredFormat()->willReturn('json');
$this->request->getContentType()->willReturn('application/json');

$listener->onKernelException($this->event);
Expand Down

0 comments on commit 1cf51a9

Please sign in to comment.