You are here

protected function ServicesWebTestCase::servicesPost in Services 6.3

Same name and namespace in other branches
  1. 7.3 tests/services.test \ServicesWebTestCase::servicesPost()
29 calls to ServicesWebTestCase::servicesPost()
ServicesParserTests::testJSONCall in tests/functional/ServicesParserTests.test
Do JSON call. Ensure it is parsed properly.
ServicesParserTests::testParser in tests/functional/ServicesParserTests.test
Testing parser functionality.
ServicesResourceCommentTests::testCommentCountAll in tests/functional/ServicesResourceCommentTests.test
Test countAll method.
ServicesResourceCommentTests::testCommentCountNew in tests/functional/ServicesResourceCommentTests.test
Test countNew method.
ServicesResourceCommentTests::testCommentCreate in tests/functional/ServicesResourceCommentTests.test
Test create method.

... See full list

File

tests/functional/ServicesWebTestCase.php, line 32
Services base testing class.

Class

ServicesWebTestCase
@file Services base testing class.

Code

protected function servicesPost($url, $data = array(), $headers = array(), $call_type = 'php') {
  $options = array();
  switch ($call_type) {
    case 'php':

      // Add .php to get serialized response.
      $url = $this
        ->getAbsoluteUrl($url) . '.php';

      // Otherwise Services will reject arguments.
      $headers = array(
        "Content-type: application/x-www-form-urlencoded",
      );

      // Prepare arguments.
      $post = http_build_query($data, '', '&');
      break;
    case 'json':

      // Add .json to get json encoded response.
      $url = $this
        ->getAbsoluteUrl($url) . '.json';

      // Set proper headers.
      $headers = array(
        "Content-type: application/json",
      );

      // Prepare arguments.
      $post = json_encode($data);
      break;
  }
  $content = $this
    ->curlExec(array(
    CURLOPT_URL => $url,
    CURLOPT_POST => TRUE,
    CURLOPT_POSTFIELDS => $post,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_HEADER => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
  ));

  // Parse response.
  list($info, $header, $status, $code, $body) = $this
    ->parseHeader($content, $call_type);
  $this
    ->verbose('POST request to: ' . $url . '<hr />Arguments: ' . highlight_string('<?php ' . var_export($data, TRUE), TRUE) . '<hr />Response: ' . highlight_string('<?php ' . var_export($body, TRUE), TRUE) . '<hr />Curl info: ' . highlight_string('<?php ' . var_export($info, TRUE), TRUE) . '<hr />Raw response: ' . $content);
  return array(
    'header' => $header,
    'status' => $status,
    'code' => $code,
    'body' => $body,
  );
}