You are here

protected function RestWSTestCase::httpRequest in RESTful Web Services 7

Same name and namespace in other branches
  1. 7.2 restws.test \RestWSTestCase::httpRequest()

Helper function to issue a HTTP request with simpletest's cURL.

7 calls to RestWSTestCase::httpRequest()
RestWSTestCase::testBadInputFormat in ./restws.test
Tests access to restricted input formats.
RestWSTestCase::testBadRequests in ./restws.test
Tests bad requests.
RestWSTestCase::testCRUD in ./restws.test
CRUD tests for nodes.
RestWSTestCase::testErrors in ./restws.test
Test requests to non-existing resources and other errors.
RestWSTestCase::testFieldAccess in ./restws.test
Test field level access restrictions.

... See full list

File

./restws.test, line 348
RESTful web services tests.

Class

RestWSTestCase
@file RESTful web services tests.

Code

protected function httpRequest($url, $method, $account = NULL, $body = NULL, $format = 'json') {
  if (isset($account)) {
    unset($this->curlHandle);
    $this
      ->drupalLogin($account);
  }
  if (in_array($method, array(
    'POST',
    'PUT',
    'DELETE',
  ))) {

    // GET the CSRF token first for writing requests.
    $token = $this
      ->drupalGet('restws/session/token');
  }
  switch ($method) {
    case 'GET':
      return $this
        ->curlExec(array(
        CURLOPT_HTTPGET => TRUE,
        CURLOPT_URL => url($url, array(
          'absolute' => TRUE,
        )),
        CURLOPT_NOBODY => FALSE,
      ));
    case 'PUT':
      return $this
        ->curlExec(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: application/' . $format,
          'X-CSRF-Token: ' . $token,
        ),
      ));
    case 'POST':
      return $this
        ->curlExec(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: application/' . $format,
          'X-CSRF-Token: ' . $token,
        ),
      ));
    case 'DELETE':
      return $this
        ->curlExec(array(
        CURLOPT_HTTPGET => FALSE,
        CURLOPT_CUSTOMREQUEST => 'DELETE',
        CURLOPT_URL => url($url, array(
          'absolute' => TRUE,
        )),
        CURLOPT_NOBODY => FALSE,
        CURLOPT_HTTPHEADER => array(
          'X-CSRF-Token: ' . $token,
        ),
      ));
  }
}