Skip to content

Commit

Permalink
Merge pull request #558 from flightphp/request-improvements
Browse files Browse the repository at this point in the history
little changes to requests and integration testing JSON POST requests
  • Loading branch information
n0nag0n committed Mar 18, 2024
2 parents 018ca42 + 9786820 commit 412596e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
38 changes: 19 additions & 19 deletions flight/net/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
];
}

Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down
29 changes: 29 additions & 0 deletions tests/EngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 412596e

Please sign in to comment.