Merge pull request #457 from mu1f407/master

[2.x] Fix JSON request with empty body
pull/458/merge
Mike Cao 3 years ago committed by GitHub
commit c5649791c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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