little changes to requests and integration testing JSON POST requests

pull/558/head
Austin Collier 10 months ago
parent 4f52e1acc0
commit f4c5405d1b

@ -148,7 +148,7 @@ class Request
$config = [ $config = [
'url' => str_replace('@', '%40', self::getVar('REQUEST_URI', '/')), 'url' => str_replace('@', '%40', self::getVar('REQUEST_URI', '/')),
'base' => str_replace(['\\', ' '], ['/', '%20'], \dirname(self::getVar('SCRIPT_NAME'))), 'base' => str_replace(['\\', ' '], ['/', '%20'], \dirname(self::getVar('SCRIPT_NAME'))),
'method' => self::getMethod(), 'method' => ($config['method'] ?? self::getMethod()),
'referrer' => self::getVar('HTTP_REFERER'), 'referrer' => self::getVar('HTTP_REFERER'),
'ip' => self::getVar('REMOTE_ADDR'), 'ip' => self::getVar('REMOTE_ADDR'),
'ajax' => 'XMLHttpRequest' === self::getVar('HTTP_X_REQUESTED_WITH'), 'ajax' => 'XMLHttpRequest' === self::getVar('HTTP_X_REQUESTED_WITH'),
@ -181,7 +181,7 @@ class Request
{ {
// Set all the defined properties // Set all the defined properties
foreach ($properties as $name => $value) { foreach ($properties as $name => $value) {
$this->$name = $value; $this->{$name} = $value;
} }
// Get the requested URL without the base directory // Get the requested URL without the base directory
@ -229,7 +229,7 @@ class Request
return $body; return $body;
} }
$method = self::getMethod(); $method = $this->method ?? self::getMethod();
if ('POST' === $method || 'PUT' === $method || 'DELETE' === $method || 'PATCH' === $method) { if ('POST' === $method || 'PUT' === $method || 'DELETE' === $method || 'PATCH' === $method) {
$body = file_get_contents($this->stream_path); $body = file_get_contents($this->stream_path);

@ -7,7 +7,9 @@ namespace tests;
use Exception; use Exception;
use Flight; use Flight;
use flight\Engine; use flight\Engine;
use flight\net\Request;
use flight\net\Response; use flight\net\Response;
use flight\util\Collection;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
// phpcs:ignoreFile PSR2.Methods.MethodDeclaration.Underscore // phpcs:ignoreFile PSR2.Methods.MethodDeclaration.Underscore
@ -304,6 +306,33 @@ class EngineTest extends TestCase
$this->assertEquals(301, $engine->response()->status()); $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() public function testJson()
{ {
$engine = new Engine(); $engine = new Engine();

Loading…
Cancel
Save