2018-04-24 00:22:50 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Control\Tests\Middleware;
|
|
|
|
|
2022-10-10 23:19:31 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2018-04-24 00:22:50 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
|
|
|
use SilverStripe\Control\Middleware\CanonicalURLMiddleware;
|
2022-10-10 23:19:31 +02:00
|
|
|
use SilverStripe\Core\Environment;
|
2018-04-24 00:22:50 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
|
|
|
class CanonicalURLMiddlewareTest extends SapphireTest
|
|
|
|
{
|
|
|
|
|
|
|
|
public function testHttpsIsForcedForBasicAuth()
|
|
|
|
{
|
2022-10-10 23:19:31 +02:00
|
|
|
$middleware = $this->getMockedMiddleware();
|
|
|
|
$middleware->expects($this->once())->method('getRedirect');
|
2018-04-24 00:22:50 +02:00
|
|
|
|
|
|
|
$request = new HTTPRequest('GET', '/');
|
2022-10-10 23:19:31 +02:00
|
|
|
$request->addHeader('host', 'www.example.com');
|
2018-04-24 00:22:50 +02:00
|
|
|
$mockResponse = (new HTTPResponse)
|
|
|
|
->addHeader('WWW-Authenticate', 'basic')
|
|
|
|
->setStatusCode(401);
|
|
|
|
|
2022-10-10 23:19:31 +02:00
|
|
|
$result = $middleware->process($request, function () use ($mockResponse) {
|
2018-04-24 00:22:50 +02:00
|
|
|
return $mockResponse;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertNotSame($mockResponse, $result, 'New response is created and returned');
|
|
|
|
$this->assertEquals(301, $result->getStatusCode(), 'Basic auth responses are redirected');
|
2021-10-27 04:39:47 +02:00
|
|
|
$this->assertStringContainsString('https://', $result->getHeader('Location'), 'HTTPS is in the redirect location');
|
2018-04-24 00:22:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMiddlewareDelegateIsReturnedWhenBasicAuthRedirectIsDisabled()
|
|
|
|
{
|
2022-10-10 23:19:31 +02:00
|
|
|
$middleware = $this->getMockedMiddleware();
|
|
|
|
$middleware->expects($this->once())->method('getRedirect');
|
|
|
|
$middleware->setForceBasicAuthToSSL(false);
|
2018-04-24 00:22:50 +02:00
|
|
|
|
|
|
|
$request = new HTTPRequest('GET', '/');
|
2022-10-10 23:19:31 +02:00
|
|
|
$request->addHeader('host', 'www.example.com');
|
2018-04-24 00:22:50 +02:00
|
|
|
$mockResponse = (new HTTPResponse)
|
|
|
|
->addHeader('WWW-Authenticate', 'basic')
|
|
|
|
->setStatusCode(401);
|
|
|
|
|
2022-10-10 23:19:31 +02:00
|
|
|
$result = $middleware->process($request, function () use ($mockResponse) {
|
2018-04-24 00:22:50 +02:00
|
|
|
return $mockResponse;
|
|
|
|
});
|
|
|
|
$this->assertSame($mockResponse, $result, 'Response returned verbatim with auto redirect disabled');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMiddlewareDelegateIsReturnedWhenNoBasicAuthIsPresent()
|
|
|
|
{
|
2022-10-10 23:19:31 +02:00
|
|
|
$middleware = $this->getMockedMiddleware();
|
|
|
|
$middleware->expects($this->once())->method('getRedirect');
|
2018-04-24 00:22:50 +02:00
|
|
|
|
|
|
|
$request = new HTTPRequest('GET', '/');
|
2022-10-10 23:19:31 +02:00
|
|
|
$request->addHeader('host', 'www.example.com');
|
2018-04-24 00:22:50 +02:00
|
|
|
$mockResponse = (new HTTPResponse)->addHeader('Foo', 'bar');
|
|
|
|
|
2022-10-10 23:19:31 +02:00
|
|
|
$result = $middleware->process($request, function () use ($mockResponse) {
|
2018-04-24 00:22:50 +02:00
|
|
|
return $mockResponse;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame($mockResponse, $result, 'Non basic-auth responses are returned verbatim');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetForceBasicAuthToSSL()
|
|
|
|
{
|
2022-10-10 23:19:31 +02:00
|
|
|
$middleware = $this->getMockedMiddleware();
|
|
|
|
$middleware->setForceBasicAuthToSSL(null);
|
|
|
|
|
|
|
|
$middleware->setForceSSL(true);
|
|
|
|
$this->assertTrue($middleware->getForceBasicAuthToSSL(), 'Default falls over to forceSSL');
|
|
|
|
|
|
|
|
$middleware->setForceSSL(false);
|
|
|
|
$this->assertFalse($middleware->getForceBasicAuthToSSL(), 'Default falls over to forceSSL');
|
|
|
|
|
|
|
|
$middleware->setForceBasicAuthToSSL(true);
|
|
|
|
$this->assertTrue($middleware->getForceBasicAuthToSSL(), 'Explicitly set is returned');
|
2018-04-24 00:22:50 +02:00
|
|
|
|
2022-10-10 23:19:31 +02:00
|
|
|
$middleware->setForceBasicAuthToSSL(false);
|
|
|
|
$middleware->setForceSSL(true);
|
|
|
|
$this->assertFalse($middleware->getForceBasicAuthToSSL(), 'Explicitly set is returned');
|
|
|
|
}
|
|
|
|
|
2023-05-17 00:49:34 +02:00
|
|
|
public function provideRedirectTrailingSlash()
|
2022-10-10 23:19:31 +02:00
|
|
|
{
|
2023-05-17 00:49:34 +02:00
|
|
|
$testScenarios = [];
|
|
|
|
foreach ([true, false] as $forceRedirect) {
|
|
|
|
foreach ([true, false] as $addTrailingSlash) {
|
|
|
|
foreach ([true, false] as $requestHasSlash) {
|
|
|
|
$testScenarios[] = [
|
|
|
|
$forceRedirect,
|
|
|
|
$addTrailingSlash,
|
|
|
|
$requestHasSlash,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $testScenarios;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideRedirectTrailingSlash
|
|
|
|
*/
|
|
|
|
public function testRedirectTrailingSlash(bool $forceRedirect, bool $addTrailingSlash, bool $requestHasSlash)
|
|
|
|
{
|
|
|
|
Controller::config()->set('add_trailing_slash', $addTrailingSlash);
|
|
|
|
|
|
|
|
$noRedirect = !$forceRedirect || ($addTrailingSlash && $requestHasSlash) || (!$addTrailingSlash && !$requestHasSlash);
|
|
|
|
$middleware = $this->getMockedMiddleware(false);
|
|
|
|
$middleware->setEnforceTrailingSlashConfig($forceRedirect);
|
|
|
|
|
|
|
|
$requestSlash = $requestHasSlash ? '/' : '';
|
|
|
|
$requestURL = "/about-us{$requestSlash}";
|
|
|
|
|
|
|
|
$this->performRedirectTest($requestURL, $middleware, !$noRedirect, $addTrailingSlash);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function performRedirectTest(string $requestURL, CanonicalURLMiddleware $middleware, bool $shouldRedirect, bool $addTrailingSlash)
|
|
|
|
{
|
|
|
|
Environment::setEnv('REQUEST_URI', $requestURL);
|
|
|
|
$request = new HTTPRequest('GET', $requestURL);
|
|
|
|
$request->setScheme('https');
|
|
|
|
$request->addHeader('host', 'www.example.com');
|
|
|
|
$mockResponse = (new HTTPResponse)
|
|
|
|
->setStatusCode(200);
|
|
|
|
|
|
|
|
$result = $middleware->process($request, function () use ($mockResponse) {
|
|
|
|
return $mockResponse;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!$shouldRedirect) {
|
|
|
|
$this->assertNull($result->getHeader('Location'), 'No location header should be added');
|
|
|
|
$this->assertEquals(200, $result->getStatusCode(), 'No redirection should be made');
|
|
|
|
} else {
|
|
|
|
$this->assertEquals(301, $result->getStatusCode(), 'Responses should be redirected to include/omit trailing slash');
|
|
|
|
if ($addTrailingSlash) {
|
|
|
|
$this->assertStringEndsWith('/', $result->getHeader('Location'), 'Trailing slash should be added');
|
|
|
|
} else {
|
|
|
|
$this->assertStringEndsNotWith('/', $result->getHeader('Location'), 'Trailing slash should be removed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideRedirectTrailingSlashIgnorePaths()
|
|
|
|
{
|
|
|
|
return [
|
2022-10-10 23:19:31 +02:00
|
|
|
[
|
2023-05-17 00:49:34 +02:00
|
|
|
'addTrailingSlash' => false,
|
2022-10-10 23:19:31 +02:00
|
|
|
'requestHasSlash' => false,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'addTrailingSlash' => false,
|
|
|
|
'requestHasSlash' => true,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'addTrailingSlash' => true,
|
|
|
|
'requestHasSlash' => true,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'addTrailingSlash' => true,
|
|
|
|
'requestHasSlash' => false,
|
|
|
|
],
|
|
|
|
];
|
2023-05-17 00:49:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideRedirectTrailingSlashIgnorePaths
|
|
|
|
*/
|
|
|
|
public function testRedirectTrailingSlashIgnorePaths(bool $addTrailingSlash, bool $requestHasSlash)
|
|
|
|
{
|
|
|
|
Controller::config()->set('add_trailing_slash', $addTrailingSlash);
|
|
|
|
|
|
|
|
$middleware = $this->getMockedMiddleware(false);
|
|
|
|
$middleware->setEnforceTrailingSlashConfig(true);
|
|
|
|
|
|
|
|
$requestSlash = $requestHasSlash ? '/' : '';
|
|
|
|
$noRedirectPaths = [
|
|
|
|
"/admin{$requestSlash}",
|
|
|
|
"/admin/graphql{$requestSlash}",
|
|
|
|
"/dev/tasks/my-task{$requestSlash}",
|
|
|
|
];
|
|
|
|
$allowRedirectPaths = [
|
|
|
|
"/administration{$requestSlash}",
|
|
|
|
"/administration/more-path{$requestSlash}",
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($noRedirectPaths as $path) {
|
|
|
|
$this->performRedirectTest($path, $middleware, false, $addTrailingSlash);
|
|
|
|
}
|
|
|
|
foreach ($allowRedirectPaths as $path) {
|
|
|
|
$this->performRedirectTest($path, $middleware, $addTrailingSlash !== $requestHasSlash, $addTrailingSlash);
|
2022-10-10 23:19:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getMockedMiddleware($mockGetRedirect = true): CanonicalURLMiddleware
|
|
|
|
{
|
|
|
|
$mockedMethods = ['isEnabled'];
|
|
|
|
if ($mockGetRedirect) {
|
|
|
|
$mockedMethods[] = 'getRedirect';
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var CanonicalURLMiddleware $middleware */
|
|
|
|
$middleware = $this->getMockBuilder(CanonicalURLMiddleware::class)
|
|
|
|
->setMethods($mockedMethods)
|
|
|
|
->getMock();
|
2018-04-24 00:22:50 +02:00
|
|
|
|
2022-10-10 23:19:31 +02:00
|
|
|
$middleware->expects($this->any())->method('isEnabled')->willReturn(true);
|
|
|
|
if ($mockGetRedirect) {
|
|
|
|
$middleware->expects($this->any())->method('getRedirect')->willReturn(false);
|
|
|
|
}
|
2018-04-24 00:22:50 +02:00
|
|
|
|
2022-10-10 23:19:31 +02:00
|
|
|
$middleware->setForceBasicAuthToSSL(true);
|
2018-04-24 00:22:50 +02:00
|
|
|
|
2022-10-10 23:19:31 +02:00
|
|
|
return $middleware;
|
2018-04-24 00:22:50 +02:00
|
|
|
}
|
|
|
|
}
|