Skip to content

Commit

Permalink
feat: add support for 405 method not allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
ipranjal committed Aug 7, 2023
1 parent 68cfa85 commit d98b563
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public function dispatch(string $httpMethod,string $uri) : array
{
$result = $this->engine->route($httpMethod,$uri);

if($result[0] == 0 || $result[0] == 2){
return $result;
}

if (\is_callable($result[1])) {
return $result;
}
Expand Down
13 changes: 12 additions & 1 deletion src/RouterEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ private function routeAuto() : array
{
$controller = $this->getController();
$method = $this->getMethod($controller);
if($controller == '' || $method == ''){
if($method == ''){
if($this->checkMethodNotAllowed($controller)){
return [2,'',[],$this->debugMsg];
}
return [0,'',[],$this->debugMsg];
}
$handler = $controller . '::' . $method;
Expand Down Expand Up @@ -219,6 +222,14 @@ private function getArguments(string $controller,string $method) : bool|array

}

private function checkMethodNotAllowed($controller): bool
{
if(method_exists($controller,'get'.ucfirst($this->pathInfo[1])) || method_exists($controller,'post'.ucfirst($this->pathInfo[1])) || method_exists($controller,'put'.ucfirst($this->pathInfo[1])) || method_exists($controller,'delete'.ucfirst($this->pathInfo[1]))){
return true;
}
return false;
}

//---------------------------------------------------------------//

/**
Expand Down
18 changes: 15 additions & 3 deletions tests/Unit/RouterEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@

});

it('tests for method not allowed ',function(){

$engine = new \Scrawler\Router\RouterEngine(getCollection(false));
[$status,$handler,$args,$debug] = $engine->route('GET','/random/r');
expect($status)->toBe(0);

[$status,$handler,$args,$debug] = $engine->route('GET','/appo/random');
expect($status)->toBe(0);

});

it('tests method not found exception',function(){

$engine = new \Scrawler\Router\RouterEngine(getCollection(false));
Expand All @@ -80,10 +91,11 @@

});

it('tests argument not found exception',function(){
it('tests method not allowed exception',function(){

$engine = new \Scrawler\Router\RouterEngine(getCollection(false));
[$status,$handler,$args,$debug] = $engine->route('GET','/bye/world');
expect($status)->toBe(0);
[$status,$handler,$args,$debug] = $engine->route('POST','/hello/world/pranjal');

expect($status)->toBe(\Scrawler\Router\Router::METHOD_NOT_ALLOWED);

});
3 changes: 3 additions & 0 deletions tests/Unit/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
expect($status)->toBe(\Scrawler\Router\Router::FOUND);
expect($response)->toBe('Hello pranjal');

[$status,$handler,$args,$debug] = $this->router->dispatch('GET','/hello/world');
expect($status)->toBe(\Scrawler\Router\Router::NOT_FOUND);


[$status,$handler,$args,$debug] = $this->router->dispatch('GET','/bye/world/nobody');
$response = call_user_func($handler,...$args);
Expand Down

0 comments on commit d98b563

Please sign in to comment.