NEW Accept / as designation for root URL controller

This commit is contained in:
Garion Herman 2019-02-24 01:05:18 +13:00
parent b76d52d4cd
commit 9d1d59d8d1
4 changed files with 49 additions and 4 deletions

View File

@ -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;
}

View File

@ -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());
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace SilverStripe\Control\Tests\RequestHandlingTest;
use SilverStripe\Control\Controller;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Dev\TestOnly;
class HTTPMethodTestController extends Controller implements TestOnly
{
private static $url_segment = 'HTTPMethodTestController';
private static $url_handlers = [
'GET /' => '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";
}
}

View File

@ -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