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