Skip to content

Commit f6c5796

Browse files
committed
Fix Json renderer that was not checking for errors
1 parent 91d7ad3 commit f6c5796

File tree

4 files changed

+103
-3
lines changed

4 files changed

+103
-3
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"require": {
2020
"php": ">=7.0",
2121
"ext-zlib": "*",
22+
"ext-json": "*",
2223
"react/http": "0.8.4",
2324
"zendframework/zend-validator": "^2.10",
2425
"zendframework/zend-filter": "^2.8",

composer.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Renderer/Json.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,16 @@ class Json implements RendererInterface
3939
public function render(ServerRequestInterface $request, ResponseInterface $response, array $options, $data): ResponseInterface
4040
{
4141
$response = $response->withHeader('Content-Type', 'application/json');
42-
$response = $response->withBody(stream_for(json_encode($data)));
42+
$dataStr = json_encode($data);
43+
if ($dataStr === false) {
44+
// Check for JSON error
45+
$err = json_last_error();
46+
if ($err !== JSON_ERROR_NONE) {
47+
$response = $response->withStatus(500);
48+
$dataStr = "Error {$err}: " . json_last_error_msg();
49+
}
50+
}
51+
$response = $response->withBody(stream_for($dataStr));
4352
return $response;
4453
}
4554

tests/Renderer/JsonTest.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/**
4+
* MIT License
5+
*
6+
* Copyright (c) 2018 Samuel CHEMLA
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in all
16+
* copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
* SOFTWARE.
25+
*/
26+
27+
namespace PhpBg\MiniHttpd\Tests\Renderer;
28+
29+
use GuzzleHttp\Psr7\Response;
30+
use GuzzleHttp\Psr7\ServerRequest;
31+
use PhpBg\MiniHttpd\HttpException\NotFoundException;
32+
use PhpBg\MiniHttpd\Renderer\Json;
33+
use PHPUnit\Framework\TestCase;
34+
35+
class JsonTest extends TestCase
36+
{
37+
public function testRenderOK()
38+
{
39+
$renderer = new Json();
40+
$request = new ServerRequest('GET', '/');
41+
$response = new Response();
42+
$data = ['foo' => 'bar'];
43+
44+
$response = $renderer->render($request, $response, [], $data);
45+
46+
$this->assertSame(200, $response->getStatusCode());
47+
$this->assertSame(json_encode($data), $response->getBody()->getContents());
48+
$this->assertSame('application/json', $response->getHeaderLine('Content-Type'));
49+
}
50+
51+
public function testRenderKO()
52+
{
53+
$renderer = new Json();
54+
$request = new ServerRequest('GET', '/');
55+
$response = new Response();
56+
$data = ['foo' => log(0)];
57+
58+
$response = $renderer->render($request, $response, [], $data);
59+
60+
$this->assertSame(500, $response->getStatusCode());
61+
$msg = $response->getBody()->getContents();
62+
$this->assertNotEmpty($msg);
63+
$this->assertSame(0, strpos($msg, "Error"));
64+
}
65+
66+
public function testRenderHttpException()
67+
{
68+
$renderer = new Json();
69+
$request = new ServerRequest('GET', '/');
70+
$response = new Response();
71+
$exception = new NotFoundException();
72+
73+
$response = $renderer->renderException($request, $response, [], $exception);
74+
75+
$this->assertSame(404, $response->getStatusCode());
76+
}
77+
78+
public function testRenderException()
79+
{
80+
$renderer = new Json();
81+
$request = new ServerRequest('GET', '/');
82+
$response = new Response();
83+
$exception = new \Exception();
84+
85+
$response = $renderer->renderException($request, $response, [], $exception);
86+
87+
$this->assertSame(500, $response->getStatusCode());
88+
}
89+
}

0 commit comments

Comments
 (0)