Skip to content

Commit 1cf51a9

Browse files
authored
Merge pull request #11 from tdutrion/feature/trigger-on-accept-header
Trigger the plugin on json accept header
2 parents 64ab168 + 630d87c commit 1cf51a9

3 files changed

Lines changed: 20 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ SomeController {
5151
}
5252
```
5353

54-
When the controller is marked as a "json" format or the request `Content-Type` is `*/json`, this bundle kicks in.
54+
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.
5555
It will transform the exception to following response:
5656

5757
Headers:

src/EventListener/JsonApiProblemExceptionListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function onKernelException(ExceptionEvent $event): void
3434
{
3535
$request = $event->getRequest();
3636
if (
37-
false === mb_strpos($request->getRequestFormat(), 'json') &&
37+
false === mb_strpos($request->getPreferredFormat(), 'json') &&
3838
false === mb_strpos((string) $request->getContentType(), 'json')
3939
) {
4040
return;

test/EventListener/JsonApiProblemExceptionListenerTest.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected function setUp(): void
5959
public function it_does_nothing_on_non_json_requests(): void
6060
{
6161
$listener = new JsonApiProblemExceptionListener($this->exceptionTransformer->reveal(), false);
62-
$this->request->getRequestFormat()->willReturn('html');
62+
$this->request->getPreferredFormat()->willReturn('html');
6363
$this->request->getContentType()->willReturn('text/html');
6464
$listener->onKernelException($this->event);
6565

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

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

8787
$listener->onKernelException($this->event);
8888
$this->assertApiProblemWithResponseBody(500, $this->parseDataForException());
8989
}
9090

91+
/** @test */
92+
public function it_runs_on_json_accept_header(): void
93+
{
94+
$listener = new JsonApiProblemExceptionListener($this->exceptionTransformer->reveal(), false);
95+
$this->request->getPreferredFormat()->willReturn('json');
96+
$this->request->getContentType()->willReturn('html');
97+
98+
$listener->onKernelException($this->event);
99+
$this->assertApiProblemWithResponseBody(500, $this->parseDataForException());
100+
}
101+
91102
/** @test */
92103
public function it_parses_an_api_problem_response(): void
93104
{
94105
$listener = new JsonApiProblemExceptionListener($this->exceptionTransformer->reveal(), false);
95-
$this->request->getRequestFormat()->willReturn('json');
106+
$this->request->getPreferredFormat()->willReturn('json');
96107
$this->request->getContentType()->willReturn('application/json');
97108

98109
$listener->onKernelException($this->event);
@@ -103,7 +114,7 @@ public function it_parses_an_api_problem_response(): void
103114
public function it_uses_an_exception_transformer(): void
104115
{
105116
$listener = new JsonApiProblemExceptionListener($this->exceptionTransformer->reveal(), false);
106-
$this->request->getRequestFormat()->willReturn('json');
117+
$this->request->getPreferredFormat()->willReturn('json');
107118
$this->request->getContentType()->willReturn('application/json');
108119

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

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

149-
$this->request->getRequestFormat()->willReturn('json');
160+
$this->request->getPreferredFormat()->willReturn('json');
150161
$this->request->getContentType()->willReturn('application/json');
151162

152163
$listener->onKernelException($this->event);

0 commit comments

Comments
 (0)