diff --git a/flight/net/Request.php b/flight/net/Request.php index d340510..e14297e 100644 --- a/flight/net/Request.php +++ b/flight/net/Request.php @@ -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); diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 2232677..842c1bc 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -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';