Skip to content

Commit

Permalink
Merge pull request #11 from utopia-php/fix-head-method-routing
Browse files Browse the repository at this point in the history
Fixed head method routing
  • Loading branch information
eldadfux authored Mar 17, 2021
2 parents bfdb236 + b234bb2 commit ba17789
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ public function run(Request $request, Response $response): self
$groups = ($route instanceof Route) ? $route->getGroups() : [];

if (self::REQUEST_METHOD_HEAD == $method) {
$method = self::REQUEST_METHOD_GET;
$response->disablePayload();
}

Expand Down
12 changes: 6 additions & 6 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,14 +375,14 @@ public function getCookies(): array
*/
public function send(string $body = '', int $exit = null): void
{
if (!$this->disablePayload) {
$this->addHeader('X-Debug-Speed', (string)(\microtime(true) - $this->startTime));
$this->addHeader('X-Debug-Speed', (string)(\microtime(true) - $this->startTime));

$this
->appendCookies()
->appendHeaders()
;
$this
->appendCookies()
->appendHeaders()
;

if (!$this->disablePayload) {
$this->size = $this->size + \mb_strlen(\implode("\n", \headers_list())) + \mb_strlen($body, '8bit');

echo $body;
Expand Down
28 changes: 28 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,34 @@ public function testSetRoute() {
$this->assertEquals($this->app->getRoute(), $route);
}

public function testRun()
{
// Test head requests

$method = (isset($_SERVER['REQUEST_METHOD'])) ? $_SERVER['REQUEST_METHOD'] : null;
$uri = (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : null;

$_SERVER['REQUEST_METHOD'] = 'HEAD';
$_SERVER['REQUEST_URI'] = '/path';

App::get('/path')
->inject('response')
->action(function($response) {
$response->send('HELLO');
})
;

\ob_start();
$this->app->run(new Request(), new Response());
$result = \ob_get_contents();
\ob_end_clean();

$_SERVER['REQUEST_METHOD'] = $method;
$_SERVER['REQUEST_URI'] = $uri;

$this->assertStringNotContainsString('HELLO', $result);
}

public function tearDown():void
{
$this->app = null;
Expand Down

0 comments on commit ba17789

Please sign in to comment.