unit1308.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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 https://curl.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curlcheck.h"
  25. #include <curl/curl.h>
  26. static CURLcode unit_setup(void)
  27. {
  28. return CURLE_OK;
  29. }
  30. static void unit_stop(void)
  31. {
  32. }
  33. static size_t print_httppost_callback(void *arg, const char *buf, size_t len)
  34. {
  35. fwrite(buf, len, 1, stdout);
  36. (*(size_t *) arg) += len;
  37. return len;
  38. }
  39. UNITTEST_START
  40. CURLFORMcode rc;
  41. int res;
  42. struct curl_httppost *post = NULL;
  43. struct curl_httppost *last = NULL;
  44. size_t total_size = 0;
  45. char buffer[] = "test buffer";
  46. CURL_IGNORE_DEPRECATION(
  47. rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
  48. CURLFORM_COPYCONTENTS, "content", CURLFORM_END);
  49. )
  50. fail_unless(rc == 0, "curl_formadd returned error");
  51. /* after the first curl_formadd when there's a single entry, both pointers
  52. should point to the same struct */
  53. fail_unless(post == last, "post and last weren't the same");
  54. CURL_IGNORE_DEPRECATION(
  55. rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "htmlcode",
  56. CURLFORM_COPYCONTENTS, "<HTML></HTML>",
  57. CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
  58. )
  59. fail_unless(rc == 0, "curl_formadd returned error");
  60. CURL_IGNORE_DEPRECATION(
  61. rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "name_for_ptrcontent",
  62. CURLFORM_PTRCONTENTS, buffer, CURLFORM_END);
  63. )
  64. fail_unless(rc == 0, "curl_formadd returned error");
  65. CURL_IGNORE_DEPRECATION(
  66. res = curl_formget(post, &total_size, print_httppost_callback);
  67. )
  68. fail_unless(res == 0, "curl_formget returned error");
  69. fail_unless(total_size == 518, "curl_formget got wrong size back");
  70. CURL_IGNORE_DEPRECATION(
  71. curl_formfree(post);
  72. )
  73. /* start a new formpost with a file upload and formget */
  74. post = last = NULL;
  75. CURL_IGNORE_DEPRECATION(
  76. rc = curl_formadd(&post, &last,
  77. CURLFORM_PTRNAME, "name of file field",
  78. CURLFORM_FILE, arg,
  79. CURLFORM_FILENAME, "custom named file",
  80. CURLFORM_END);
  81. )
  82. fail_unless(rc == 0, "curl_formadd returned error");
  83. CURL_IGNORE_DEPRECATION(
  84. res = curl_formget(post, &total_size, print_httppost_callback);
  85. )
  86. fail_unless(res == 0, "curl_formget returned error");
  87. fail_unless(total_size == 899, "curl_formget got wrong size back");
  88. CURL_IGNORE_DEPRECATION(
  89. curl_formfree(post);
  90. )
  91. UNITTEST_STOP