Skip to content

Commit

Permalink
Fixes #29 by addind support for HEAD http method calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Gaigalas authored and augustohp committed Mar 11, 2012
1 parent 08798da commit 0eb0c0c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 6 additions & 3 deletions library/Respect/Rest/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ public function dispatchRequest(Request $request=null)

foreach ($matchedByPath as $route)
if (0 !== stripos($request->method, '__')
&& ($route->method === $request->method || $route->method === 'ANY')
&& $route->matchRoutines($request, $params))
&& ($route->method === $request->method || $route->method === 'ANY' || ($route->method === 'GET' && $request->method === 'HEAD'))
&& $route->matchRoutines($request, $params) )
return $this->configureRequest($request, $route, static::cleanUpParams($params));

header('HTTP/1.1 405');
Expand All @@ -172,7 +172,10 @@ public function dispatchRequest(Request $request=null)
public function run(Request $request=null)
{
$route = $this->dispatch($request);
return $route ? $route->response() : null;
if (!$route || (isset($request->method) && $request->method === 'HEAD'))
return null;

return $route->response();
}

/** Creates and returns an factory-based route */
Expand Down
15 changes: 14 additions & 1 deletion tests/library/Respect/Rest/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,23 @@ function test_method_not_allowed_header_with_conneg()
$this->assertContains('HTTP/1.1 405', $header);
$this->assertContains('Allow: GET', $header);
}
function test_http_method_head()
{
global $header;
$expectedHeader = 'X-Burger: With Cheese!';
$this->router->get('/', function() use ($expectedHeader) {
header($expectedHeader);
return 'ok';
});
$headResponse = $this->router->dispatch('HEAD', '/');
$getResponse = $this->router->dispatch('GET', '/');
$this->assertEquals('ok', (string) $getResponse);
$this->assertContains($expectedHeader, $header);
}
}
$header=array();
/**
* [header description]
* header() stub to tests ...
* @return void
*/
function header($string, $replace=true, $http_response_code=200)
Expand Down

0 comments on commit 0eb0c0c

Please sign in to comment.