View source
<?php
namespace Drupal\Tests\cdn\Unit\StackMiddleware;
use Drupal\cdn\StackMiddleware\DuplicateContentPreventionMiddleware;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Utility\UnroutedUrlAssemblerInterface;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class DuplicateContentPreventionMiddlewareTest extends UnitTestCase {
public function testDuplicateContentPrevention($path, $user_agent, array $other_request_headers, $expected_redirect) {
$request_prophecy = $this
->prophesize(Request::class);
$request_prophecy
->getPathInfo()
->willReturn($path);
$request = $request_prophecy
->reveal();
$request->headers = new HeaderBag([
'User-Agent' => $user_agent,
] + $other_request_headers);
$container = new ContainerBuilder();
$url_assembler_prophecy = $this
->prophesize(UnroutedUrlAssemblerInterface::class);
$url_assembler_prophecy
->assemble(Argument::type('string'), [
'absolute' => TRUE,
], FALSE)
->will(function ($args) {
return str_replace('base:', 'http://๐ท.com', $args[0]);
});
$container
->set('unrouted_url_assembler', $url_assembler_prophecy
->reveal());
\Drupal::setContainer($container);
$kernel = $this
->prophesize(HttpKernelInterface::class);
$kernel
->handle($request, HttpKernelInterface::MASTER_REQUEST, TRUE)
->willReturn(new Response());
$middleware = new DuplicateContentPreventionMiddleware($kernel
->reveal(), new RequestStack());
$response = $middleware
->handle($request);
if ($expected_redirect !== FALSE) {
$this
->assertInstanceOf(RedirectResponse::class, $response);
$this
->assertSame(301, $response
->getStatusCode());
$this
->assertSame('<' . $expected_redirect . '>; rel="canonical"', $response->headers
->get('Link'));
}
else {
$this
->assertNotInstanceOf(RedirectResponse::class, $response);
}
}
public function duplicateContentPreventionProvider() {
return [
[
'/',
'Mozilla',
[],
FALSE,
],
[
'/node/1',
'Mozilla',
[],
FALSE,
],
[
'/node/1.html',
'Mozilla',
[],
FALSE,
],
[
'/node/1.htm',
'Mozilla',
[],
FALSE,
],
[
'/node/1.php',
'Mozilla',
[],
FALSE,
],
[
'/',
'Amazon CloudFront',
[],
'http://๐ท.com/',
],
[
'/node/1',
'Amazon CloudFront',
[],
'http://๐ท.com/node/1',
],
[
'/node/1.html',
'Amazon CloudFront',
[],
'http://๐ท.com/node/1.html',
],
[
'/node/1.htm',
'Amazon CloudFront',
[],
'http://๐ท.com/node/1.htm',
],
[
'/node/1.php',
'Amazon CloudFront',
[],
'http://๐ท.com/node/1.php',
],
[
'/',
'Mozilla',
[
'CF-RAY' => $this
->randomMachineName(),
],
'http://๐ท.com/',
],
[
'/node/1',
'Mozilla',
[
'CF-RAY' => $this
->randomMachineName(),
],
'http://๐ท.com/node/1',
],
[
'/node/1.html',
'Mozilla',
[
'CF-RAY' => $this
->randomMachineName(),
],
'http://๐ท.com/node/1.html',
],
[
'/node/1.htm',
'Mozilla',
[
'CF-RAY' => $this
->randomMachineName(),
],
'http://๐ท.com/node/1.htm',
],
[
'/node/1.php',
'Mozilla',
[
'CF-RAY' => $this
->randomMachineName(),
],
'http://๐ท.com/node/1.php',
],
[
'/misc/jquery.js',
'Mozilla',
[],
FALSE,
],
[
'/misc/jquery.js',
'Amazon CloudFront',
[],
FALSE,
],
[
'/sites/default/files/styles/thumbnail/foobar.png',
'Mozilla',
[],
FALSE,
],
[
'/sites/default/files/styles/thumbnail/foobar.png',
'Amazon CloudFront',
[],
FALSE,
],
];
}
}