diff --git a/flight/net/Request.php b/flight/net/Request.php index eb932c0..569994e 100644 --- a/flight/net/Request.php +++ b/flight/net/Request.php @@ -146,24 +146,24 @@ public function __construct(array $config = []) // Default properties if (empty($config)) { $config = [ - 'url' => str_replace('@', '%40', self::getVar('REQUEST_URI', '/')), - 'base' => str_replace(['\\', ' '], ['/', '%20'], \dirname(self::getVar('SCRIPT_NAME'))), - 'method' => self::getMethod(), - 'referrer' => self::getVar('HTTP_REFERER'), - 'ip' => self::getVar('REMOTE_ADDR'), - 'ajax' => 'XMLHttpRequest' === self::getVar('HTTP_X_REQUESTED_WITH'), - 'scheme' => self::getScheme(), + 'url' => str_replace('@', '%40', self::getVar('REQUEST_URI', '/')), + 'base' => str_replace(['\\', ' '], ['/', '%20'], \dirname(self::getVar('SCRIPT_NAME'))), + 'method' => self::getMethod(), + 'referrer' => self::getVar('HTTP_REFERER'), + 'ip' => self::getVar('REMOTE_ADDR'), + 'ajax' => 'XMLHttpRequest' === self::getVar('HTTP_X_REQUESTED_WITH'), + 'scheme' => self::getScheme(), 'user_agent' => self::getVar('HTTP_USER_AGENT'), - 'type' => self::getVar('CONTENT_TYPE'), - 'length' => intval(self::getVar('CONTENT_LENGTH', 0)), - 'query' => new Collection($_GET), - 'data' => new Collection($_POST), - 'cookies' => new Collection($_COOKIE), - 'files' => new Collection($_FILES), - 'secure' => 'https' === self::getScheme(), - 'accept' => self::getVar('HTTP_ACCEPT'), - 'proxy_ip' => self::getProxyIpAddress(), - 'host' => self::getVar('HTTP_HOST'), + 'type' => self::getVar('CONTENT_TYPE'), + 'length' => intval(self::getVar('CONTENT_LENGTH', 0)), + 'query' => new Collection($_GET), + 'data' => new Collection($_POST), + 'cookies' => new Collection($_COOKIE), + 'files' => new Collection($_FILES), + 'secure' => 'https' === self::getScheme(), + 'accept' => self::getVar('HTTP_ACCEPT'), + 'proxy_ip' => self::getProxyIpAddress(), + 'host' => self::getVar('HTTP_HOST'), ]; } @@ -181,7 +181,7 @@ public function init(array $properties = []): self { // Set all the defined properties foreach ($properties as $name => $value) { - $this->$name = $value; + $this->{$name} = $value; } // Get the requested URL without the base directory @@ -229,7 +229,7 @@ public function getBody(): string return $body; } - $method = self::getMethod(); + $method = $this->method ?? self::getMethod(); if ('POST' === $method || 'PUT' === $method || 'DELETE' === $method || 'PATCH' === $method) { $body = file_get_contents($this->stream_path); diff --git a/tests/EngineTest.php b/tests/EngineTest.php index 26df60f..a977e3a 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -7,7 +7,9 @@ use Exception; use Flight; use flight\Engine; +use flight\net\Request; use flight\net\Response; +use flight\util\Collection; use PHPUnit\Framework\TestCase; // phpcs:ignoreFile PSR2.Methods.MethodDeclaration.Underscore @@ -318,6 +320,33 @@ public function testRedirectWithBaseUrl() $this->assertEquals(301, $engine->response()->status()); } + public function testJsonRequestBody() + { + $engine = new Engine(); + $tmpfile = tmpfile(); + $stream_path = stream_get_meta_data($tmpfile)['uri']; + file_put_contents($stream_path, '{"key1":"value1","key2":"value2"}'); + + $engine->register('request', Request::class, [ + [ + 'method' => 'POST', + 'url' => '/something/fancy', + 'base' => '/vagrant/public', + 'type' => 'application/json', + 'length' => 13, + 'data' => new Collection(), + 'query' => new Collection(), + 'stream_path' => $stream_path + ] + ]); + $engine->post('/something/fancy', function () use ($engine) { + echo $engine->request()->data->key1; + echo $engine->request()->data->key2; + }); + $engine->start(); + $this->expectOutputString('value1value2'); + } + public function testJson() { $engine = new Engine();