100% coverage for Request and Route

pull/501/head
n0nag0n 1 year ago
parent 0d026b16d1
commit a416bfe19b

@ -129,10 +129,23 @@ final class Request
*/
public string $host;
/**
* Stream path for where to pull the request body from
*
* @var string
*/
private string $stream_path = 'php://input';
/**
* @var string Raw HTTP request body
*/
public string $body = '';
/**
* Constructor.
*
* @param array<string, mixed> $config Request configuration
* @param string
*/
public function __construct($config = array())
{
@ -196,7 +209,7 @@ final class Request
// Check for JSON input
if (0 === strpos($this->type, 'application/json')) {
$body = self::getBody();
$body = $this->getBody();
if ('' !== $body && null !== $body) {
$data = json_decode($body, true);
if (is_array($data)) {
@ -213,20 +226,22 @@ final class Request
*
* @return string Raw HTTP request body
*/
public static function getBody(): ?string
public function getBody(): ?string
{
static $body;
$body = $this->body;
if (null !== $body) {
if ('' !== $body) {
return $body;
}
$method = self::getMethod();
if ('POST' === $method || 'PUT' === $method || 'DELETE' === $method || 'PATCH' === $method) {
$body = file_get_contents('php://input');
$body = file_get_contents($this->stream_path);
}
$this->body = $body;
return $body;
}

@ -165,4 +165,32 @@ class RequestTest extends PHPUnit\Framework\TestCase
]);
$this->assertEquals('/flightphp', $request->url);
}
public function testInitNoUrl() {
$request = new Request([
'url' => '',
'base' => '/vagrant/public',
'type' => ''
]);
$this->assertEquals('/', $request->url);
}
public function testInitWithJsonBody() {
// create dummy file to pull request body from
$tmpfile = tmpfile();
$stream_path = stream_get_meta_data($tmpfile)['uri'];
file_put_contents($stream_path, '{"foo":"bar"}');
$_SERVER['REQUEST_METHOD'] = 'POST';
$request = new Request([
'url' => '/something/fancy',
'base' => '/vagrant/public',
'type' => 'application/json',
'length' => 13,
'data' => new Collection(),
'query' => new Collection(),
'stream_path' => $stream_path
]);
$this->assertEquals([ 'foo' => 'bar' ], $request->data->getData());
$this->assertEquals('{"foo":"bar"}', $request->getBody());
}
}

@ -105,6 +105,16 @@ class RouterTest extends PHPUnit\Framework\TestCase
$this->check('OK');
}
// Simple path with trailing slash
// Simple path
public function testPathRouteTrailingSlash()
{
$this->router->map('/path/', [$this, 'ok']);
$this->request->url = '/path';
$this->check('OK');
}
// POST route
public function testPostRoute()
{

Loading…
Cancel
Save