Skip to content

Commit

Permalink
Merge branch 'moritz-h-3.x' into 4.x
Browse files Browse the repository at this point in the history
Forward port #2066
  • Loading branch information
akrabat committed Mar 18, 2017
2 parents dd94c56 + dc5092c commit d839e5a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Slim/Http/Request.php
Expand Up @@ -202,7 +202,11 @@ public function __construct(
}

$this->registerMediaTypeParser('application/json', function ($input) {
return json_decode($input, true);
$result = json_decode($input, true);
if (!is_array($result)) {
return null;
}
return $result;
});

$this->registerMediaTypeParser('application/xml', function ($input) {
Expand Down
30 changes: 30 additions & 0 deletions tests/Http/RequestTest.php
Expand Up @@ -787,6 +787,36 @@ public function testGetParsedBodyJson()
$this->assertEquals(['foo' => 'bar'], $request->getParsedBody());
}

public function testGetParsedBodyInvalidJson()
{
$method = 'GET';
$uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', '');
$headers = new Headers();
$headers->set('Content-Type', 'application/json;charset=utf8');
$cookies = [];
$serverParams = [];
$body = new RequestBody();
$body->write('{foo}bar');
$request = new Request($method, $uri, $headers, $cookies, $serverParams, $body);

$this->assertNull($request->getParsedBody());
}

public function testGetParsedBodySemiValidJson()
{
$method = 'GET';
$uri = new Uri('https', 'example.com', 443, '/foo/bar', 'abc=123', '', '');
$headers = new Headers();
$headers->set('Content-Type', 'application/json;charset=utf8');
$cookies = [];
$serverParams = [];
$body = new RequestBody();
$body->write('"foo bar"');
$request = new Request($method, $uri, $headers, $cookies, $serverParams, $body);

$this->assertNull($request->getParsedBody());
}

public function testGetParsedBodyWithJsonStructuredSuffix()
{
$method = 'GET';
Expand Down

0 comments on commit d839e5a

Please sign in to comment.