You are here

protected function ServicesWebTestCase::servicesPostFile in Services 7.3

Post file as multipart/form-data.

2 calls to ServicesWebTestCase::servicesPostFile()
ServicesResourceFileTests::testResourceFileCreateRaw in tests/functional/ServicesResourceFileTests.test
Test create_raw method.
ServicesResourceNodetests::testAttachFileTargetedAction in tests/functional/ServicesResourceNodeTests.test
Testing targeted_action attach_file.

File

tests/services.test, line 52

Class

ServicesWebTestCase

Code

protected function servicesPostFile($url, $filepath, $headers = array(), $additional_arguments = array()) {
  $this
    ->addCSRFHeader($headers);
  if (!is_array($filepath)) {
    $filepath = array(
      $filepath,
    );
  }

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

  // Otherwise Services will reject arguments.
  $headers[] = "Content-type: multipart/form-data";

  // Prepare arguments.
  $post = $additional_arguments;
  $i = 0;
  foreach ($filepath as $path) {
    $post['files[file_contents' . $i . ']'] = '@' . variable_get('file_public_path', '') . '/' . file_uri_target($path);
    $i++;
  }
  $content = $this
    ->curlExec(array(
    CURLOPT_URL => $url,
    CURLOPT_POST => TRUE,
    CURLOPT_POSTFIELDS => $post,
    CURLOPT_HTTPHEADER => $headers,
    CURLOPT_HEADER => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_FOLLOWLOCATION => TRUE,
    CURLOPT_VERBOSE => TRUE,
  ));

  // Parse response.
  list($info, $header, $status, $code, $body) = $this
    ->parseHeader($content);
  $this
    ->verbose('POST request to: ' . $url . '<hr />File Name(s): ' . highlight_string('<?php . ' . var_export($filepath, 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,
  );
}