public function ClientIpRestoreTest::testEnabledClientIpRestoreProvider in CloudFlare 8
Test ClientIpRestoreProvider functionality.
@dataProvider requestProvider
Parameters
bool $client_ip_restore_enabled: Bool to indicate if client ip restore is enabled.
string $host_header: Host header to send as part of request.
string $remote_header_ip: The server server ip.
string $cf_header: CloudFlare header with the originating user's IP.
string $bypass_host: Host/Domain that is expected to bypass CloudFlare.
string $expected_message: The expected message to be logged.
string $expected_client_ip: The expected clientIp address after ClientIpRestore has been run.
File
- tests/
src/ Unit/ ClientIpRestoreTest.php, line 66
Class
- ClientIpRestoreTest
- Tests functionality of CloudFlareState object.
Namespace
Drupal\Tests\cloudflare\UnitCode
public function testEnabledClientIpRestoreProvider($client_ip_restore_enabled, $host_header, $remote_header_ip, $cf_header, $bypass_host, $expected_message, $expected_client_ip) {
$logger = $this
->createMock(LoggerInterface::class);
if (empty($expected_message)) {
$logger
->expects($this
->never())
->method('warning')
->with($expected_message);
}
else {
$logger
->expects($this
->once())
->method('warning')
->with((string) $expected_message);
}
$config_factory = $this
->createMock('\\Drupal\\Core\\Config\\ConfigFactoryInterface');
$config = $this
->getMockBuilder('Drupal\\Core\\Config\\Config')
->disableOriginalConstructor()
->getMock();
// Create a map of arguments to return values.
$map = [
[
ClientIpRestore::CLOUDFLARE_BYPASS_HOST,
$bypass_host,
],
[
ClientIpRestore::CLOUDFLARE_CLIENT_IP_RESTORE_ENABLED,
$client_ip_restore_enabled,
],
];
$config
->expects($this
->atLeastOnce())
->method('get')
->will($this
->returnValueMap($map));
$config_factory
->expects($this
->once())
->method('get')
->will($this
->returnValue($config));
$cf_ips = explode("\n", "103.21.244.0/22\n 103.22.200.0/22\n 103.31.4.0/22\n 104.16.0.0/12\n 108.162.192.0/18\n 141.101.64.0/18\n 162.158.0.0/15\n 172.64.0.0/13\n 173.245.48.0/20\n 188.114.96.0/20\n 190.93.240.0/20\n 197.234.240.0/22\n 198.41.128.0/17\n 199.27.128.0/21");
$cf_ips = array_map('trim', $cf_ips);
$cache_backend = new MemoryBackend('foo');
$cache_backend
->set(ClientIpRestore::CLOUDFLARE_RANGE_KEY, $cf_ips);
$client_ip_restore = new ClientIpRestore($config_factory, $cache_backend, $this
->createMock(ClientInterface::class), $logger);
$request = Request::create('/test', 'get');
$kernel = $this
->createMock('Symfony\\Component\\HttpKernel\\HttpKernelInterface');
if (!empty($cf_header)) {
$request->server
->set('HTTP_CF_CONNECTING_IP', $cf_header);
}
if (!empty($host_header)) {
$request->headers
->set('host', $host_header);
}
if (!empty($remote_header_ip)) {
$request->server
->set('REMOTE_ADDR', $remote_header_ip);
}
$request
->overrideGlobals();
$event = new GetResponseEvent($kernel, $request, 'foo', new NotFoundHttpException('foo'));
$client_ip_restore
->onRequest($event);
$this
->assertEquals($expected_client_ip, $request
->getClientIp());
}