Helper function to issue a HTTP request with simpletest's cURL.
string $url: The relative URL, e.g. "entity/node/1"
string $method: HTTP method, one of GET, POST, PUT or DELETE.
array $body: Either the body for POST and PUT or additional URL parameters for GET.
string $format: The MIME type of the transmitted content.
protected function httpRequest($url, $method, $body = NULL, $format = 'application/ld+json') {
switch ($method) {
case 'GET':
// Set query if there are additional GET parameters.
$options = isset($body) ? array(
'absolute' => TRUE,
'query' => $body,
) : array(
'absolute' => TRUE,
);
$curl_options = array(
CURLOPT_HTTPGET => TRUE,
CURLOPT_URL => url($url, $options),
CURLOPT_NOBODY => FALSE,
);
break;
case 'POST':
$curl_options = array(
CURLOPT_HTTPGET => FALSE,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $body,
CURLOPT_URL => url($url, array(
'absolute' => TRUE,
)),
CURLOPT_NOBODY => FALSE,
CURLOPT_HTTPHEADER => array(
'Content-Type: ' . $format,
),
);
break;
case 'PUT':
$curl_options = array(
CURLOPT_HTTPGET => FALSE,
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_POSTFIELDS => $body,
CURLOPT_URL => url($url, array(
'absolute' => TRUE,
)),
CURLOPT_NOBODY => FALSE,
CURLOPT_HTTPHEADER => array(
'Content-Type: ' . $format,
),
);
break;
case 'DELETE':
$curl_options = array(
CURLOPT_HTTPGET => FALSE,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_URL => url($url, array(
'absolute' => TRUE,
)),
CURLOPT_NOBODY => FALSE,
);
break;
}
// Include all HTTP headers in the response.
$curl_options[CURLOPT_HEADER] = TRUE;
$response = $this
->curlExec($curl_options);
list($header, $body) = explode("\r\n\r\n", $response, 2);
$header_lines = explode("\r\n", $header);
foreach ($header_lines as $line) {
$parts = explode(':', $line, 2);
$this->responseHeaders[$parts[0]] = isset($parts[1]) ? trim($parts[1]) : '';
}
$this
->verbose($method . ' request to: ' . $url . '<hr />Code: ' . curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE) . '<hr />Response headers: ' . $header . '<hr />Response body: ' . $body);
return $body;
}