protected function RESTTestBase::httpRequest in Drupal 8
Helper function to issue a HTTP request with simpletest's cURL.
Parameters
string|\Drupal\Core\Url $url: A Url object or system path.
string $method: HTTP method, one of GET, POST, PUT or DELETE.
string $body: The body for POST and PUT.
string $mime_type: The MIME type of the transmitted content.
bool $csrf_token: If NULL, a CSRF token will be retrieved and used. If FALSE, omit the X-CSRF-Token request header (to simulate developer error). Otherwise, the passed in value will be used as the value for the X-CSRF-Token request header (to simulate developer error, by sending an invalid CSRF token).
Return value
string The content returned from the request.
File
- core/
modules/ rest/ src/ Tests/ RESTTestBase.php, line 147
Class
- RESTTestBase
- Test helper class that provides a REST client method to send HTTP requests.
Namespace
Drupal\rest\TestsCode
protected function httpRequest($url, $method, $body = NULL, $mime_type = NULL, $csrf_token = NULL) {
if (!isset($mime_type)) {
$mime_type = $this->defaultMimeType;
}
if (!in_array($method, [
'GET',
'HEAD',
'OPTIONS',
'TRACE',
])) {
// GET the CSRF token first for writing requests.
$requested_token = $this
->drupalGet('session/token');
}
$client = \Drupal::httpClient();
$url = $this
->buildUrl($url);
$options = [
'http_errors' => FALSE,
'cookies' => $this
->cookies(),
'curl' => [
CURLOPT_HEADERFUNCTION => [
&$this,
'curlHeaderCallback',
],
],
];
switch ($method) {
case 'GET':
$options += [
'headers' => [
'Accept' => $mime_type,
],
];
$response = $client
->get($url, $options);
break;
case 'HEAD':
$response = $client
->head($url, $options);
break;
case 'POST':
$options += [
'headers' => $csrf_token !== FALSE ? [
'Content-Type' => $mime_type,
'X-CSRF-Token' => $csrf_token === NULL ? $requested_token : $csrf_token,
] : [
'Content-Type' => $mime_type,
],
'body' => $body,
];
$response = $client
->post($url, $options);
break;
case 'PUT':
$options += [
'headers' => $csrf_token !== FALSE ? [
'Content-Type' => $mime_type,
'X-CSRF-Token' => $csrf_token === NULL ? $requested_token : $csrf_token,
] : [
'Content-Type' => $mime_type,
],
'body' => $body,
];
$response = $client
->put($url, $options);
break;
case 'PATCH':
$options += [
'headers' => $csrf_token !== FALSE ? [
'Content-Type' => $mime_type,
'X-CSRF-Token' => $csrf_token === NULL ? $requested_token : $csrf_token,
] : [
'Content-Type' => $mime_type,
],
'body' => $body,
];
$response = $client
->patch($url, $options);
break;
case 'DELETE':
$options += [
'headers' => $csrf_token !== FALSE ? [
'Content-Type' => $mime_type,
'X-CSRF-Token' => $csrf_token === NULL ? $requested_token : $csrf_token,
] : [],
];
$response = $client
->delete($url, $options);
break;
}
$this->response = $response;
$this->responseBody = (string) $response
->getBody();
$this
->setRawContent($this->responseBody);
// Ensure that any changes to variables in the other thread are picked up.
$this
->refreshVariables();
$this
->verbose($method . ' request to: ' . $url . '<hr />Code: ' . $this->response
->getStatusCode() . (isset($options['headers']) ? '<hr />Request headers: ' . nl2br(print_r($options['headers'], TRUE)) : '') . (isset($options['body']) ? '<hr />Request body: ' . nl2br(print_r($options['body'], TRUE)) : '') . '<hr />Response headers: ' . nl2br(print_r($response
->getHeaders(), TRUE)) . '<hr />Response body: ' . $this->responseBody);
return $this->responseBody;
}