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