diff --git a/core/lib/Drupal/Core/CoreServiceProvider.php b/core/lib/Drupal/Core/CoreServiceProvider.php
index 8eb6c04d695cd4afdc58e6a0e688df4ab09ad4f7..92e9594c3487b331070d3edb7d59707090efe4fe 100644
--- a/core/lib/Drupal/Core/CoreServiceProvider.php
+++ b/core/lib/Drupal/Core/CoreServiceProvider.php
@@ -131,6 +131,11 @@ protected function registerTest(ContainerBuilder $container) {
     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 4613296b58f95afafffa273bb2178cd5e26dac73..4775bf53fa8e6532ee6e1c813439f6ab8c81d0ed 100644
--- a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php
+++ b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php
@@ -4,6 +4,7 @@
 
 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 @@ public function testSubsequentContainerIsolation() {
     $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
    */