You are here

protected function WebTestBase::curlInitialize in Drupal 8

Initializes the cURL connection.

If the simpletest_httpauth_credentials variable is set, this function will add HTTP authentication headers. This is necessary for testing sites that are protected by login credentials from public access. See the description of $curl_options for other options.

1 call to WebTestBase::curlInitialize()
WebTestBase::curlExec in core/modules/simpletest/src/WebTestBase.php
Initializes and executes a cURL request.

File

core/modules/simpletest/src/WebTestBase.php, line 531

Class

WebTestBase
Test case for typical Drupal tests.

Namespace

Drupal\simpletest

Code

protected function curlInitialize() {
  global $base_url;
  if (!isset($this->curlHandle)) {
    $this->curlHandle = curl_init();

    // Some versions/configurations of cURL break on a NULL cookie jar, so
    // supply a real file.
    if (empty($this->cookieFile)) {
      $this->cookieFile = $this->publicFilesDirectory . '/cookie.jar';
    }
    $curl_options = [
      CURLOPT_COOKIEJAR => $this->cookieFile,
      CURLOPT_URL => $base_url,
      CURLOPT_FOLLOWLOCATION => FALSE,
      CURLOPT_RETURNTRANSFER => TRUE,
      // Required to make the tests run on HTTPS.
      CURLOPT_SSL_VERIFYPEER => FALSE,
      // Required to make the tests run on HTTPS.
      CURLOPT_SSL_VERIFYHOST => FALSE,
      CURLOPT_HEADERFUNCTION => [
        &$this,
        'curlHeaderCallback',
      ],
      CURLOPT_USERAGENT => $this->databasePrefix,
      // Disable support for the @ prefix for uploading files.
      CURLOPT_SAFE_UPLOAD => TRUE,
    ];
    if (isset($this->httpAuthCredentials)) {
      $curl_options[CURLOPT_HTTPAUTH] = $this->httpAuthMethod;
      $curl_options[CURLOPT_USERPWD] = $this->httpAuthCredentials;
    }

    // curl_setopt_array() returns FALSE if any of the specified options
    // cannot be set, and stops processing any further options.
    $result = curl_setopt_array($this->curlHandle, $this->additionalCurlOptions + $curl_options);
    if (!$result) {
      throw new \UnexpectedValueException('One or more cURL options could not be set.');
    }
  }

  // We set the user agent header on each request so as to use the current
  // time and a new uniqid.
  curl_setopt($this->curlHandle, CURLOPT_USERAGENT, drupal_generate_test_ua($this->databasePrefix));
}