From 9d1d59d8d172ee6670ab1ff26797449822e86385 Mon Sep 17 00:00:00 2001 From: Garion Herman Date: Sun, 24 Feb 2019 01:05:18 +1300 Subject: [PATCH] NEW Accept / as designation for root URL controller --- src/Control/HTTPRequest.php | 4 +-- tests/php/Control/ControllerTest.php | 13 ++++++++ .../HTTPMethodTestController.php | 32 +++++++++++++++++++ .../RequestHandlingTest/TestFormHandler.php | 4 +-- 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 tests/php/Control/RequestHandlingTest/HTTPMethodTestController.php diff --git a/src/Control/HTTPRequest.php b/src/Control/HTTPRequest.php index 150bcb733..536c20b25 100644 --- a/src/Control/HTTPRequest.php +++ b/src/Control/HTTPRequest.php @@ -521,8 +521,8 @@ class HTTPRequest implements ArrayAccess $pattern = $matches[2]; } - // Special case for the root URL controller - if (!$pattern) { + // Special case for the root URL controller (designated as an empty string, or a slash) + if (!$pattern || $pattern === '/') { return ($this->dirParts == array()) ? array('Matched' => true) : false; } diff --git a/tests/php/Control/ControllerTest.php b/tests/php/Control/ControllerTest.php index a38648bc5..5e5ea8ea5 100644 --- a/tests/php/Control/ControllerTest.php +++ b/tests/php/Control/ControllerTest.php @@ -14,6 +14,7 @@ use SilverStripe\Control\Tests\ControllerTest\IndexSecuredController; use SilverStripe\Control\Tests\ControllerTest\SubController; use SilverStripe\Control\Tests\ControllerTest\TestController; use SilverStripe\Control\Tests\ControllerTest\UnsecuredController; +use SilverStripe\Control\Tests\RequestHandlingTest\HTTPMethodTestController; use SilverStripe\Dev\FunctionalTest; use SilverStripe\Security\Member; use SilverStripe\View\SSViewer; @@ -34,6 +35,7 @@ class ControllerTest extends FunctionalTest ContainerController::class, HasAction::class, HasAction_Unsecured::class, + HTTPMethodTestController::class, IndexSecuredController::class, SubController::class, TestController::class, @@ -493,4 +495,15 @@ class ControllerTest extends FunctionalTest $response = $this->get('ContainerController/subcontroller/substring/subvieweraction'); $this->assertEquals('Hope this works', $response->getBody()); } + + public function testSpecificHTTPMethods() + { + // 'GET /' + $response = $this->get('HTTPMethodTestController'); + $this->assertEquals('Routed to getRoot', $response->getBody()); + + // 'POST ' (legacy method of specifying root route) + $response = $this->post('HTTPMethodTestController', ['dummy' => 'example']); + $this->assertEquals('Routed to postLegacyRoot', $response->getBody()); + } } diff --git a/tests/php/Control/RequestHandlingTest/HTTPMethodTestController.php b/tests/php/Control/RequestHandlingTest/HTTPMethodTestController.php new file mode 100644 index 000000000..beb211e21 --- /dev/null +++ b/tests/php/Control/RequestHandlingTest/HTTPMethodTestController.php @@ -0,0 +1,32 @@ + 'getRoot', + 'POST ' => 'postLegacyRoot', + ]; + + private static $allowed_actions = [ + 'getRoot', + 'postLegacyRoot', + ]; + + public function getRoot(HTTPRequest $request) + { + return "Routed to getRoot"; + } + + public function postLegacyRoot(HTTPRequest $request) + { + return "Routed to postLegacyRoot"; + } +} diff --git a/tests/php/Control/RequestHandlingTest/TestFormHandler.php b/tests/php/Control/RequestHandlingTest/TestFormHandler.php index eae42fc8f..9ee8c41e7 100644 --- a/tests/php/Control/RequestHandlingTest/TestFormHandler.php +++ b/tests/php/Control/RequestHandlingTest/TestFormHandler.php @@ -13,8 +13,8 @@ class TestFormHandler extends FormRequestHandler { private static $url_handlers = array( 'fields/$FieldName' => 'handleField', - "POST " => "handleSubmission", - "GET " => "handleGet", + "POST /" => "handleSubmission", + "GET /" => "handleGet", ); // These are a different case from those in url_handlers to confirm that it's all case-insensitive