added test coverage and clearing body for head requests

pull/555/head
Austin Collier 10 months ago
parent 296c9b57d5
commit 5406bbedc1

@ -56,7 +56,7 @@
}, },
"scripts": { "scripts": {
"test": "phpunit", "test": "phpunit",
"test-coverage": "rm clover.xml && XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100", "test-coverage": "rm -f clover.xml && XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100",
"test-server": "echo \"Running Test Server\" && php -S localhost:8000 -t tests/server/", "test-server": "echo \"Running Test Server\" && php -S localhost:8000 -t tests/server/",
"test-server-v2": "echo \"Running Test Server\" && php -S localhost:8000 -t tests/server-v2/", "test-server-v2": "echo \"Running Test Server\" && php -S localhost:8000 -t tests/server-v2/",
"test-coverage:win": "del clover.xml && phpunit --coverage-html=coverage --coverage-clover=clover.xml && coverage-check clover.xml 100", "test-coverage:win": "del clover.xml && phpunit --coverage-html=coverage --coverage-clover=clover.xml && coverage-check clover.xml 100",

@ -533,6 +533,11 @@ class Engine
$dispatched = false; $dispatched = false;
} }
// HEAD requests should be identical to GET requests but have no body
if($request->method === 'HEAD') {
$response->clearBody();
}
if ($failed_middleware_check === true) { if ($failed_middleware_check === true) {
$this->halt(403, 'Forbidden', empty(getenv('PHPUNIT_TEST'))); $this->halt(403, 'Forbidden', empty(getenv('PHPUNIT_TEST')));
} elseif ($dispatched === false) { } elseif ($dispatched === false) {

@ -105,11 +105,12 @@ class Router
[$method, $url] = explode(' ', $url, 2); [$method, $url] = explode(' ', $url, 2);
$url = trim($url); $url = trim($url);
$methods = explode('|', $method); $methods = explode('|', $method);
}
if (in_array('GET', $methods) && !in_array('HEAD', $methods)) { // Add head requests to get methods, should they come in as a get request
if (in_array('GET', $methods, true) === true && in_array('HEAD', $methods, true) === false) {
$methods[] = 'HEAD'; $methods[] = 'HEAD';
} }
}
// And this finishes it off. // And this finishes it off.
if ($this->group_prefix !== '') { if ($this->group_prefix !== '') {

@ -263,6 +263,20 @@ class EngineTest extends TestCase
$this->assertEquals('/someRoute', $routes[0]->pattern); $this->assertEquals('/someRoute', $routes[0]->pattern);
} }
public function testHeadRoute()
{
$engine = new Engine();
$engine->route('GET /someRoute', function () {
echo 'i ran';
}, true);
$engine->request()->method = 'HEAD';
$engine->request()->url = '/someRoute';
$engine->start();
// No body should be sent
$this->expectOutputString('');
}
public function testHalt() public function testHalt()
{ {
$engine = new class extends Engine { $engine = new class extends Engine {

@ -81,6 +81,10 @@ class RouterTest extends TestCase
$dispatched = false; $dispatched = false;
} }
if($this->request->method === 'HEAD') {
ob_clean();
}
if (!$dispatched) { if (!$dispatched) {
echo '404'; echo '404';
} }
@ -122,6 +126,15 @@ class RouterTest extends TestCase
$this->check('OK'); $this->check('OK');
} }
public function testHeadRouteShortcut()
{
$route = $this->router->get('/path', [$this, 'ok']);
$this->assertEquals(['GET', 'HEAD'], $route->methods);
$this->request->url = '/path';
$this->request->method = 'HEAD';
$this->check('');
}
// POST route // POST route
public function testPostRoute() public function testPostRoute()
{ {

Loading…
Cancel
Save