Allow * wildcard at end of URI template
This commit is contained in:
parent
9275d12ff0
commit
d1a77c5454
|
|
@ -110,7 +110,14 @@ class TemplateRoute extends RegexRoute
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$pattern = '/^' . $pattern . '$/';
|
$pattern = '/^' . $pattern;
|
||||||
|
if (substr($pattern, -1) === "*") {
|
||||||
|
// Allow path to include characters passed the pattern.
|
||||||
|
$pattern = rtrim($pattern, "*") . '/';
|
||||||
|
} else {
|
||||||
|
// Path must end at the end of the pattern.
|
||||||
|
$pattern .= "$/";
|
||||||
|
}
|
||||||
return $pattern;
|
return $pattern;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,11 @@ namespace pjdietz\WellRESTed\Test;
|
||||||
|
|
||||||
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
|
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
|
||||||
use pjdietz\WellRESTed\Interfaces\RequestInterface;
|
use pjdietz\WellRESTed\Interfaces\RequestInterface;
|
||||||
|
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
|
||||||
use pjdietz\WellRESTed\Response;
|
use pjdietz\WellRESTed\Response;
|
||||||
use pjdietz\WellRESTed\Router;
|
use pjdietz\WellRESTed\Router;
|
||||||
use pjdietz\WellRESTed\Routes\StaticRoute;
|
use pjdietz\WellRESTed\Routes\StaticRoute;
|
||||||
|
use pjdietz\WellRESTed\Routes\TemplateRoute;
|
||||||
|
|
||||||
class RouterTest extends \PHPUnit_Framework_TestCase
|
class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
@ -101,6 +103,37 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertEquals("No resource at /cats/", $captured);
|
$this->assertEquals("No resource at /cats/", $captured);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider nestedRouterRoutesProvider
|
||||||
|
*/
|
||||||
|
public function testNestedRouterFromWithRoutes($path, $expectedBody)
|
||||||
|
{
|
||||||
|
$router = new Router();
|
||||||
|
$router->addRoutes(array(
|
||||||
|
new TemplateRoute("/cats/*", __NAMESPACE__ . "\\CatRouter"),
|
||||||
|
new TemplateRoute("/dogs/*", __NAMESPACE__ . "\\DogRouter"),
|
||||||
|
new NotFoundHandler()
|
||||||
|
));
|
||||||
|
|
||||||
|
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
|
||||||
|
$mockRequest->expects($this->any())
|
||||||
|
->method('getPath')
|
||||||
|
->will($this->returnValue($path));
|
||||||
|
|
||||||
|
$resp = $router->getResponse($mockRequest);
|
||||||
|
$this->assertEquals($expectedBody, $resp->getBody());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function nestedRouterRoutesProvider()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
["/cats/", "/cats/"],
|
||||||
|
["/cats/molly", "/cats/molly"],
|
||||||
|
["/dogs/", "/dogs/"],
|
||||||
|
["/birds/", "No resource found at /birds/"]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -112,6 +145,42 @@ class RouterTestHandler implements HandlerInterface
|
||||||
{
|
{
|
||||||
$resp = new Response();
|
$resp = new Response();
|
||||||
$resp->setStatusCode(200);
|
$resp->setStatusCode(200);
|
||||||
|
$resp->setBody($request->getPath());
|
||||||
return $resp;
|
return $resp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class CatRouter extends Router
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
$this->addRoutes([
|
||||||
|
new StaticRoute("/cats/", __NAMESPACE__ . "\\RouterTestHandler"),
|
||||||
|
new StaticRoute("/cats/molly", __NAMESPACE__ . "\\RouterTestHandler"),
|
||||||
|
new StaticRoute("/cats/oscar", __NAMESPACE__ . "\\RouterTestHandler")
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class DogRouter extends Router
|
||||||
|
{
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
$this->addRoutes([
|
||||||
|
new StaticRoute("/dogs/", __NAMESPACE__ . "\\RouterTestHandler"),
|
||||||
|
new StaticRoute("/dogs/bear", __NAMESPACE__ . "\\RouterTestHandler")
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class NotFoundHandler implements HandlerInterface
|
||||||
|
{
|
||||||
|
public function getResponse(RequestInterface $request, array $args = null)
|
||||||
|
{
|
||||||
|
$response = new Response(404);
|
||||||
|
$response->setBody("No resource found at " . $request->getPath());
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,18 +27,19 @@ class TemplateRouteTest extends \PHPUnit_Framework_TestCase
|
||||||
|
|
||||||
public function matchingTemplateProvider()
|
public function matchingTemplateProvider()
|
||||||
{
|
{
|
||||||
return array(
|
return [
|
||||||
array("/cat/{id}", TemplateRoute::RE_NUM, null, "/cat/12", "id", "12"),
|
["/cat/{id}", TemplateRoute::RE_NUM, null, "/cat/12", "id", "12"],
|
||||||
array("/cat/{catId}/{dogId}", TemplateRoute::RE_SLUG, null, "/cat/molly/bear", "dogId", "bear"),
|
["/cat/{catId}/{dogId}", TemplateRoute::RE_SLUG, null, "/cat/molly/bear", "dogId", "bear"],
|
||||||
array("/cat/{catId}/{dogId}", TemplateRoute::RE_NUM, array(
|
["/cat/{catId}/{dogId}", TemplateRoute::RE_NUM, [
|
||||||
"catId" => TemplateRoute::RE_SLUG,
|
"catId" => TemplateRoute::RE_SLUG,
|
||||||
"dogId" => TemplateRoute::RE_SLUG),
|
"dogId" => TemplateRoute::RE_SLUG],
|
||||||
"/cat/molly/bear", "dogId", "bear"),
|
"/cat/molly/bear", "dogId", "bear"],
|
||||||
array("cat/{catId}/{dogId}", TemplateRoute::RE_NUM, (object) array(
|
["cat/{catId}/{dogId}", TemplateRoute::RE_NUM, (object) [
|
||||||
"catId" => TemplateRoute::RE_SLUG,
|
"catId" => TemplateRoute::RE_SLUG,
|
||||||
"dogId" => TemplateRoute::RE_SLUG),
|
"dogId" => TemplateRoute::RE_SLUG],
|
||||||
"/cat/molly/bear", "dogId", "bear")
|
"/cat/molly/bear", "dogId", "bear"],
|
||||||
);
|
["/cat/{id}/*", null, null, "/cat/12/molly", "id", "12"]
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue