unit1308.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2011, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curlcheck.h"
  23. #include <curl/curl.h>
  24. static CURLcode unit_setup(void)
  25. {
  26. return CURLE_OK;
  27. }
  28. static void unit_stop(void)
  29. {
  30. }
  31. static size_t print_httppost_callback(void *arg, const char *buf, size_t len)
  32. {
  33. fwrite(buf, len, 1, stdout);
  34. (*(size_t *) arg) += len;
  35. return len;
  36. }
  37. UNITTEST_START
  38. int rc;
  39. struct curl_httppost* post = NULL;
  40. struct curl_httppost* last = NULL;
  41. size_t total_size = 0;
  42. char buffer[] = "test buffer";
  43. rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
  44. CURLFORM_COPYCONTENTS, "content", CURLFORM_END);
  45. fail_unless(rc == 0, "curl_formadd returned error");
  46. /* after the first curl_formadd when there's a single entry, both pointers
  47. should point to the same struct */
  48. fail_unless(post == last, "post and last weren't the same");
  49. rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "htmlcode",
  50. CURLFORM_COPYCONTENTS, "<HTML></HTML>",
  51. CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
  52. fail_unless(rc == 0, "curl_formadd returned error");
  53. rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "name_for_ptrcontent",
  54. CURLFORM_PTRCONTENTS, buffer, CURLFORM_END);
  55. fail_unless(rc == 0, "curl_formadd returned error");
  56. rc = curl_formget(post, &total_size, print_httppost_callback);
  57. fail_unless(rc == 0, "curl_formget returned error");
  58. fail_unless(total_size == 486, "curl_formget got wrong size back");
  59. curl_formfree(post);
  60. /* start a new formpost with a file upload and formget */
  61. post = last = NULL;
  62. rc = curl_formadd(&post, &last,
  63. CURLFORM_PTRNAME, "name of file field",
  64. CURLFORM_FILE, "log/test-1308",
  65. CURLFORM_FILENAME, "custom named file",
  66. CURLFORM_END);
  67. fail_unless(rc == 0, "curl_formadd returned error");
  68. rc = curl_formget(post, &total_size, print_httppost_callback);
  69. fail_unless(rc == 0, "curl_formget returned error");
  70. fail_unless(total_size == 847, "curl_formget got wrong size back");
  71. curl_formfree(post);
  72. UNITTEST_STOP