<?php
namespace Lia\Test\Addon;
class Router extends \Lia\Test\Tester {
public function testMultipleHttpRoutes(){
$lia = new \Lia();
$app = new \Lia\App\Server($lia);
$router = new \Lia\Addon\Router($app);
$router->main_router = new \Lia\Http\Router();
$router->addRouter($router->main_router);
$args = [
'cat-name'=>'jeffury',
];
$router->addRoute('/test/',
function(\Lia\Http\Route $route){
echo $route->args['cat-name'];
},["GET"], $args
);
$router->addRoute('/test/',
function(\Lia\Http\Route $route){
echo "Cat Name: ".$route->args['cat-name'];
},["GET"], $args
);
$request = new \Lia\Http\Request('/test/');
$response = new \Lia\Http\Response($request);
$routes = $router->onGetHttpRoutes(
$request,
$response,
);
$router->onMultipleHttpRoutes($request,$response,$routes);
$this->str_not_contains($response->body, "jeffury");
$this->str_contains($response->body,
"Multiple Potential Responses",
"This request has 2 potential responses, and the server is not configured to select a response automatically.",
"The Website Administrator or Developer should consult the documentation and implement a fix, unless they like errors.",
"The requested url was '/test/'",
);
}
public function testNoRoute(){
//
// The NoHttpRoutes implementation:
// - doesn't handle normalizing routes
// - doesn't allow alternate error messages
// - doesn't allow custom handling of error response
// - should do those things
//
$lia = new \Lia();
$app = new \Lia\App\Server($lia);
$router = new \Lia\Addon\Router($app);
$router->main_router = new \Lia\Http\Router();
$router->main_router->allowExecutableFile = true;
$router->addRouter($router->main_router);
$router->addRoute('/test/', $this->file("test/run/Addons/Router.sh"));
$request = new \Lia\Http\Request('/whoops/');
$response = new \Lia\Http\Response($request);
$routes = $router->onGetHttpRoutes(
$request,
$response,
);
$router->onNoHttpRoutes($request,$response);
$this->compare('No page was found for your request to /whoops/', trim($response->body));
}
public function testExecutableRoute(){
$lia = new \Lia();
$app = new \Lia\App\Server($lia);
$router = new \Lia\Addon\Router($app);
$router->main_router = new \Lia\Http\Router();
$router->main_router->allowExecutableFile = true;
$router->addRouter($router->main_router);
$router->addRoute('/test/', $this->file("test/run/Addons/Router.sh"));
$request = new \Lia\Http\Request('/test/');
$response = new \Lia\Http\Response($request);
$routes = $router->onGetHttpRoutes(
$request,
$response,
);
$router->onSingleHttpRoute($request,$response,$routes[0]);
$this->compare('You ran a shell script with your router!', trim($response->body));
}
public function testSingleRoute(){
$lia = new \Lia();
$app = new \Lia\App\Server($lia);
$router = new \Lia\Addon\Router($app);
$router->main_router = new \Lia\Http\Router();
$router->addRouter($router->main_router);
$args = [
'cat-name'=>'jeffury',
];
$router->addRoute('/test/',
function(\Lia\Http\Route $route){
echo $route->args['cat-name'];
},["GET"], $args
);
$request = new \Lia\Http\Request('/test/');
$response = new \Lia\Http\Response($request);
$routes = $router->onGetHttpRoutes(
$request,
$response,
);
$router->onSingleHttpRoute($request,$response,$routes[0]);
$this->compare('jeffury', trim($response->body));
}
}