Merge pull request #558 from flightphp/request-improvements

little changes to requests and integration testing JSON POST requests
pull/560/head v3.6.2
n0nag0n 10 months ago committed by GitHub
commit 412596e863
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -146,24 +146,24 @@ class Request
// 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 @@ class Request
{
// 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 @@ class Request
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);

@ -7,7 +7,9 @@ namespace tests;
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 @@ class EngineTest extends TestCase
$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();

Loading…
Cancel
Save