From 51e0c383e1ab9accff9519d4d4ae76a95e4ce377 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 3 Feb 2021 16:02:39 +0000 Subject: [PATCH] Issue #2571475 by mglaman, tstoeckler, tedbow, eiriksm, Jaesin, phenaproxima, alexpott: Outbound HTTP requests fail with KernelTestBase --- core/lib/Drupal/Core/CoreServiceProvider.php | 5 +++++ .../Drupal/KernelTests/KernelTestBaseTest.php | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/core/lib/Drupal/Core/CoreServiceProvider.php b/core/lib/Drupal/Core/CoreServiceProvider.php index 8eb6c04d695c..92e9594c3487 100644 --- a/core/lib/Drupal/Core/CoreServiceProvider.php +++ b/core/lib/Drupal/Core/CoreServiceProvider.php @@ -131,6 +131,11 @@ class CoreServiceProvider implements ServiceProviderInterface, ServiceModifierIn if (!drupal_valid_test_ua()) { return; } + // The test middleware is not required for kernel tests as there is no child + // site. DRUPAL_TEST_IN_CHILD_SITE is not defined in this case. + if (!defined('DRUPAL_TEST_IN_CHILD_SITE')) { + return; + } // Add the HTTP request middleware to Guzzle. $container ->register('test.http_client.middleware', 'Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware') diff --git a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php index 4613296b58f9..4775bf53fa8e 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php +++ b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php @@ -4,6 +4,7 @@ namespace Drupal\KernelTests; use Drupal\Component\FileCache\FileCacheFactory; use Drupal\Core\Database\Database; +use GuzzleHttp\Exception\GuzzleException; use org\bovigo\vfs\vfsStream; use org\bovigo\vfs\visitor\vfsStreamStructureVisitor; use PHPUnit\Framework\SkippedTestError; @@ -164,6 +165,21 @@ class KernelTestBaseTest extends KernelTestBase { $this->assertNull($this->installConfig('user')); } + /** + * Tests that an outbound HTTP request can be performed inside of a test. + */ + public function testOutboundHttpRequest() { + // The middleware test.http_client.middleware calls drupal_generate_test_ua + // which checks the DRUPAL_TEST_IN_CHILD_SITE constant, that is not defined + // in Kernel tests. + try { + $this->container->get('http_client')->get('http://example.com'); + } + catch (GuzzleException $e) { + // Ignore any HTTP errors. + } + } + /** * @covers ::render */