Fix JSON request with empty body

pull/457/head
Michal Hybner 3 years ago
parent 85b5b97cf9
commit 21a0c55375

@ -192,7 +192,7 @@ final class Request
// Check for JSON input
if (0 === strpos($this->type, 'application/json')) {
$body = self::getBody();
if ('' !== $body) {
if ('' !== $body && null !== $body) {
$data = json_decode($body, true);
if (is_array($data)) {
$this->data->setData($data);

@ -24,6 +24,12 @@ class RequestTest extends PHPUnit\Framework\TestCase
$_SERVER['REMOTE_ADDR'] = '8.8.8.8';
$_SERVER['HTTP_X_FORWARDED_FOR'] = '32.32.32.32';
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['CONTENT_TYPE'] = '';
$_GET = [];
$_POST = [];
$_COOKIE = [];
$_FILES = [];
$this->request = new Request();
}
@ -87,6 +93,15 @@ class RequestTest extends PHPUnit\Framework\TestCase
self::assertEquals(1, $request->files->q);
}
public function testJsonWithEmptyBody()
{
$_SERVER['CONTENT_TYPE'] = 'application/json';
$request = new Request();
self::assertSame([], $request->data->getData());
}
public function testMethodOverrideWithHeader()
{
$_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT';

Loading…
Cancel
Save