Skip to content

Commit

Permalink
Merge pull request #555 from BelleNottelling/fix/head-requests
Browse files Browse the repository at this point in the history
Also register the HEAD method alongside GET
  • Loading branch information
n0nag0n authored Mar 16, 2024
2 parents 4f52e1a + 38e2024 commit d378239
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
},
"scripts": {
"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-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",
Expand Down
5 changes: 5 additions & 0 deletions flight/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,11 @@ public function _start(): void
$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) {
$this->halt(403, 'Forbidden', empty(getenv('PHPUNIT_TEST')));
} elseif ($dispatched === false) {
Expand Down
5 changes: 5 additions & 0 deletions flight/net/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ public function map(string $pattern, callable $callback, bool $pass_route = fals
[$method, $url] = explode(' ', $url, 2);
$url = trim($url);
$methods = explode('|', $method);

// 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';
}
}

// And this finishes it off.
Expand Down
14 changes: 14 additions & 0 deletions tests/EngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,20 @@ public function testDeleteRoute()
$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()
{
$engine = new class extends Engine {
Expand Down
13 changes: 13 additions & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public function routeRequest()
$dispatched = false;
}

if ($this->request->method === 'HEAD') {
ob_clean();
}

if (!$dispatched) {
echo '404';
}
Expand Down Expand Up @@ -122,6 +126,15 @@ public function testGetRouteShortcut()
$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
public function testPostRoute()
{
Expand Down

0 comments on commit d378239

Please sign in to comment.