Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 7b38652

Browse files
committed
Merge branch 'hotfix/464'
Close #464
2 parents a7b32c5 + a953bfb commit 7b38652

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ All notable changes to this project will be documented in this file, in reverse
1818

1919
### Fixed
2020

21-
- Nothing.
21+
- [#464](https://github.com/zendframework/zend-expressive/pull/464) fixes the
22+
`WhoopsErrorResponseGenerator` to provide a correct `Content-Type` header to
23+
the response when a JSON request occurs.
2224

2325
## 2.0.0 - 2017-03-07
2426

src/Middleware/WhoopsErrorResponseGenerator.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use InvalidArgumentException;
1111
use Psr\Http\Message\ResponseInterface;
1212
use Psr\Http\Message\ServerRequestInterface;
13+
use Whoops\Handler\JsonResponseHandler;
1314
use Whoops\Handler\PrettyPageHandler;
1415
use Whoops\Run;
1516
use Whoops\RunInterface;
@@ -55,6 +56,18 @@ public function __invoke($e, ServerRequestInterface $request, ResponseInterface
5556
if ($handler instanceof PrettyPageHandler) {
5657
$this->prepareWhoopsHandler($request, $handler);
5758
}
59+
60+
// Set Json content type header
61+
if ($handler instanceof JsonResponseHandler) {
62+
$contentType = 'application/json';
63+
64+
// Whoops < 2.1.5 does not provide contentType method
65+
if (method_exists($handler, 'contentType')) {
66+
$contentType = $handler->contentType();
67+
}
68+
69+
$response = $response->withHeader('Content-Type', $contentType);
70+
}
5871
}
5972

6073
$response

test/Middleware/WhoopsErrorResponseGeneratorTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Psr\Http\Message\ServerRequestInterface;
1414
use Psr\Http\Message\StreamInterface;
1515
use RuntimeException;
16+
use Whoops\Handler\JsonResponseHandler;
1617
use Whoops\Handler\PrettyPageHandler;
1718
use Whoops\Run;
1819
use Whoops\RunInterface;
@@ -111,4 +112,40 @@ public function testAddsRequestMetadataToWhoopsPrettyPageHandler()
111112
$generator($error, $this->request->reveal(), $this->response->reveal())
112113
);
113114
}
115+
116+
public function testJsonContentTypeResponseWithJsonResponseHandler()
117+
{
118+
$error = new RuntimeException();
119+
120+
$handler = $this->prophesize(JsonResponseHandler::class);
121+
122+
if (method_exists(JsonResponseHandler::class, 'contentType')) {
123+
$handler->contentType()->willReturn('application/json');
124+
}
125+
126+
$this->whoops->getHandlers()->willReturn([$handler->reveal()]);
127+
$this->whoops->handleException($error)->willReturn('error');
128+
129+
$this->request->getAttribute('originalUri', false)->willReturn('https://example.com/foo');
130+
$this->request->getAttribute('originalRequest', false)->will([$this->request, 'reveal']);
131+
$this->request->getMethod()->willReturn('POST');
132+
$this->request->getServerParams()->willReturn(['SCRIPT_NAME' => __FILE__]);
133+
$this->request->getHeaders()->willReturn([]);
134+
$this->request->getCookieParams()->willReturn([]);
135+
$this->request->getAttributes()->willReturn([]);
136+
$this->request->getQueryParams()->willReturn([]);
137+
$this->request->getParsedBody()->willReturn([]);
138+
139+
$this->response->withHeader('Content-Type', 'application/json')->will([$this->response, 'reveal']);
140+
$this->response->getBody()->will([$this->stream, 'reveal']);
141+
142+
$this->stream->write('error')->shouldBeCalled();
143+
144+
$generator = new WhoopsErrorResponseGenerator($this->whoops->reveal());
145+
146+
$this->assertSame(
147+
$this->response->reveal(),
148+
$generator($error, $this->request->reveal(), $this->response->reveal())
149+
);
150+
}
114151
}

0 commit comments

Comments
 (0)