function KernelTestBaseTest::testOutboundHttpRequest

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/KernelTestBaseTest.php \Drupal\KernelTests\KernelTestBaseTest::testOutboundHttpRequest()
  2. 8.9.x core/tests/Drupal/KernelTests/KernelTestBaseTest.php \Drupal\KernelTests\KernelTestBaseTest::testOutboundHttpRequest()
  3. 10 core/tests/Drupal/KernelTests/KernelTestBaseTest.php \Drupal\KernelTests\KernelTestBaseTest::testOutboundHttpRequest()

Tests that an outbound HTTP request can be performed inside of a test.

File

core/tests/Drupal/KernelTests/KernelTestBaseTest.php, line 162

Class

KernelTestBaseTest
@coversDefaultClass \Drupal\KernelTests\KernelTestBase[[api-linebreak]]

Namespace

Drupal\KernelTests

Code

public function testOutboundHttpRequest() : void {
  // 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 {
    /** @var \GuzzleHttp\Psr7\Response $response */
    $response = $this->container
      ->get('http_client')
      ->head('http://example.com');
    self::assertEquals(200, $response->getStatusCode());
  } catch (\Throwable $e) {
    // Ignore any HTTP errors, any other exception is considered an error.
    self::assertInstanceOf(ClientExceptionInterface::class, $e, sprintf('Asserting that a possible exception is thrown. Got "%s" with message: "%s".', get_class($e), $e->getMessage()));
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.