Skip to content

Commit

Permalink
Fix for #25, still needs refactoring related to #28
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 cd5fce8 commit 08798da
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
31 changes: 22 additions & 9 deletions library/Respect/Rest/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,29 @@ public function dispatchRequest(Request $request=null)
$request->uri =
preg_replace('#^' . preg_quote($this->virtualHost) . '#', '', $request->uri);

try {
foreach ($this->routes as $route)
if ($this->matchRoute($request, $route, $params))
return $this->configureRequest($request, $route, static::cleanUpParams($params));
} catch (MethodNotAllowed $e) {
header('HTTP/1.1 405');
return;
}
$matchedByPath = array();
$allowedMethods = array();

foreach ($this->routes as $route)
if ($this->matchRoute($request, $route, $params)) {
$matchedByPath[] = $route;
$allowedMethods[] = $route->method;
}

if (!$matchedByPath)
header('HTTP/1.1 404');

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

header('HTTP/1.1 405');

if ($allowedMethods)
header('Allow: '.implode(', ', $allowedMethods));

header('HTTP/1.1 404');
$request->route = null;
return $request;
}
Expand Down
23 changes: 11 additions & 12 deletions library/Respect/Rest/Routes/AbstractRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,24 @@ public function createUri($param1=null, $etc=null)
);
}

public function matchRoutines(Request $request, &$params=array())
{
foreach ($this->routines as $routine)
if ($routine instanceof ProxyableWhen
&& !$request->routineCall('when', $request->method, $routine, $params))
return false;

return true;
}

/** Checks if this route matches a request */
public function match(Request $request, &$params=array())
{
if (0 === stripos($request->method, '__'))
return false;

// Checks if the given HTTP method is not implemented
if (($request->method !== $this->method ) && $this->method !== 'ANY')
throw new MethodNotAllowed(sprintf('%s not allowed for %s', $request->method, $this->pattern));

$matchUri = $request->uri;

foreach ($this->routines as $routine) {
if ($routine instanceof ProxyableWhen
&& !$request->routineCall('when', $request->method, $routine, $params))
return false;
foreach ($this->routines as $routine)
if ($routine instanceof IgnorableFileExtension)
$matchUri = preg_replace('#(\.\w+)*$#', '', $request->uri);
}

if (!preg_match($this->regexForMatch, $matchUri, $params))
return false;
Expand Down
11 changes: 10 additions & 1 deletion tests/library/Respect/Rest/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,22 @@ function test_dispatch_non_existing_route()
$this->assertContains('HTTP/1.1 404', $header);
}
function test_method_not_allowed_header()
{
global $header;
$this->router->get('/', function() { return 'ok'; });
$this->router->put('/', function() { return 'ok'; });
$this->router->dispatch('delete', '/');
$this->assertContains('HTTP/1.1 405', $header);
$this->assertContains('Allow: GET, PUT', $header);
}
function test_method_not_allowed_header_with_conneg()
{
global $header;
$this->router->get('/', function() { return 'ok'; })
->accept(array('text/html' => function($d) {return $d;}));
$this->router->dispatch('delete', '/');
$this->assertContains('HTTP/1.1 405', $header);
//$this->assertContains('Allow: ', $header);
$this->assertContains('Allow: GET', $header);
}
}
$header=array();
Expand Down

0 comments on commit 08798da

Please sign in to comment.