protected function RestfulCurlBaseTestCase::httpRequest in RESTful 7
Same name and namespace in other branches
- 7.2 tests/RestfulCurlBaseTestCase.test \RestfulCurlBaseTestCase::httpRequest()
Helper function to issue a HTTP request with simpletest's cURL.
Copied and slightly adjusted from the RestWS module.
Parameters
string $url: The URL of the request.
string $method: The HTTP method of the request. Defaults to "GET".
array $body: Either the body for POST and PUT or additional URL parameters for GET.
array $headers: Additional HTTP header parameters.
bool $use_token: Determines if a CSRF token should be retrieved by default for write operations for logged in users. Defaults to TRUE.
Return value
array Array keyed with the "code", "headers", and "body" of the response.
15 calls to RestfulCurlBaseTestCase::httpRequest()
- RestfulAuthenticationTestCase::testAuthentication in tests/
RestfulAuthenticationTestCase.test - Test authenticating a user.
- RestfulCreateEntityTestCase::uploadFile in tests/
RestfulCreateEntityTestCase.test - Uploads a file issuing a POST HTTP request.
- RestfulCsrfTokenTestCase::checkCsrfRequest in tests/
RestfulCsrfTokenTestCase.test - Perform requests without, with invalid and with valid CSRF tokens.
- RestfulDiscoveryTestCase::testOptionsMethod in tests/
RestfulDiscoveryTestCase.test - Test the headers populated in an OPTIONS request.
- RestfulExceptionHandleTestCase::testEntityNotFoundDelivery in tests/
RestfulExceptionHandleTestCase.test - Test when an entity is not found that a 4XX is returned instead of 500.
File
- tests/
RestfulCurlBaseTestCase.test, line 29
Class
- RestfulCurlBaseTestCase
- Contains RestfulCurlBaseTestCase
Code
protected function httpRequest($url, $method = \RestfulInterface::GET, $body = NULL, $headers = array(), $use_token = TRUE) {
$format = 'json';
switch ($method) {
case \RestfulInterface::GET:
// Set query if there are addition 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 \RestfulInterface::HEAD:
// Set query if there are addition GET parameters.
$options = isset($body) ? array(
'absolute' => TRUE,
'query' => $body,
) : array(
'absolute' => TRUE,
);
$curl_options = array(
CURLOPT_HTTPGET => FALSE,
CURLOPT_CUSTOMREQUEST => \RestfulInterface::HEAD,
CURLOPT_URL => url($url, $options),
CURLOPT_NOBODY => FALSE,
);
break;
case \RestfulInterface::POST:
$curl_options = array(
CURLOPT_HTTPGET => FALSE,
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $body,
CURLOPT_URL => url($url, array(
'absolute' => TRUE,
)),
CURLOPT_NOBODY => FALSE,
);
if (empty($headers['Content-Type'])) {
$headers['Content-Type'] = 'application/' . $format;
$curl_options[CURLOPT_POSTFIELDS] = http_build_query($body);
}
break;
case \RestfulInterface::PUT:
case \RestfulInterface::PATCH:
$curl_options = array(
CURLOPT_HTTPGET => FALSE,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => $body,
CURLOPT_URL => url($url, array(
'absolute' => TRUE,
)),
CURLOPT_NOBODY => FALSE,
);
if (empty($headers['Content-Type'])) {
$headers['Content-Type'] = 'application/' . $format;
$curl_options[CURLOPT_POSTFIELDS] = http_build_query($body);
}
break;
case \RestfulInterface::DELETE:
$curl_options = array(
CURLOPT_HTTPGET => FALSE,
CURLOPT_CUSTOMREQUEST => \RestfulInterface::DELETE,
CURLOPT_URL => url($url, array(
'absolute' => TRUE,
)),
CURLOPT_NOBODY => FALSE,
);
break;
}
$curl_options += array(
CURLOPT_HTTPHEADER => array(),
);
if ($this->loggedInUser && \RestfulBase::isWriteMethod($method, FALSE) && $use_token) {
// Add CSRF token for write operations.
if (empty($this->csrfToken)) {
$response = $this
->drupalGet('api/session/token');
$result = drupal_json_decode($response);
$this->csrfToken = $result['X-CSRF-Token'];
}
$headers['X-CSRF-Token'] = $this->csrfToken;
}
foreach ($headers as $key => $value) {
$curl_options[CURLOPT_HTTPHEADER][] = $key . ': ' . $value;
}
$response = $this
->curlExec($curl_options);
$response_headers = $this
->drupalGetHeaders();
$response_headers = implode("\n", $response_headers);
$code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
$this
->verbose($method . ' request to: ' . $url . '<hr />Code: ' . $code . '<hr />Response headers: ' . $response_headers . '<hr />Response body: ' . $response);
return array(
'code' => $code,
'headers' => $response_headers,
'body' => $response,
);
}