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');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRedirectTrailingSlash()
|
|
|
|
{
|
|
|
|
$testScenarios = [
|
|
|
|
[
|
|
|
|
'forceRedirect' => true,
|
|
|
|
'addTrailingSlash' => true,
|
|
|
|
'requestHasSlash' => true,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'forceRedirect' => true,
|
|
|
|
'addTrailingSlash' => true,
|
|
|
|
'requestHasSlash' => false,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'forceRedirect' => true,
|
|
|
|
'addTrailingSlash' => false,
|
|
|
|
'requestHasSlash' => true,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'forceRedirect' => true,
|
|
|
|
'addTrailingSlash' => false,
|
|
|
|
'requestHasSlash' => false,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'forceRedirect' => false,
|
|
|
|
'addTrailingSlash' => true,
|
|
|
|
'requestHasSlash' => true,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'forceRedirect' => false,
|
|
|
|
'addTrailingSlash' => true,
|
|
|
|
'requestHasSlash' => false,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'forceRedirect' => false,
|
|
|
|
'addTrailingSlash' => false,
|
|
|
|
'requestHasSlash' => true,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'forceRedirect' => false,
|
|
|
|
'addTrailingSlash' => false,
|
|
|
|
'requestHasSlash' => false,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
foreach ($testScenarios as $scenario) {
|
|
|
|
$forceRedirect = $scenario['forceRedirect'];
|
|
|
|
$addTrailingSlash = $scenario['addTrailingSlash'];
|
|
|
|
$requestHasSlash = $scenario['requestHasSlash'];
|
|
|
|
|
|
|
|
$middleware = $this->getMockedMiddleware(false);
|
|
|
|
|
|
|
|
$middleware->setEnforceTrailingSlashConfig($forceRedirect);
|
|
|
|
Controller::config()->set('add_trailing_slash', $addTrailingSlash);
|
|
|
|
|
|
|
|
$requestSlash = $requestHasSlash ? '/' : '';
|
|
|
|
$requestURL = "/about-us{$requestSlash}";
|
|
|
|
|
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
|
|
|
$noRedirect = !$forceRedirect || ($addTrailingSlash && $requestHasSlash) || (!$addTrailingSlash && !$requestHasSlash);
|
|
|
|
if ($noRedirect) {
|
|
|
|
$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');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|