xref: /curl/tests/unit/unit1308.c (revision 5cefda1b)
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 
26 #include <curl/curl.h>
27 
unit_setup(void)28 static CURLcode unit_setup(void)
29 {
30   return CURLE_OK;
31 }
32 
unit_stop(void)33 static void unit_stop(void)
34 {
35 
36 }
37 
print_httppost_callback(void * arg,const char * buf,size_t len)38 static 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 
45 UNITTEST_START
46   CURLFORMcode rc;
47   int res;
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   CURL_IGNORE_DEPRECATION(
54     rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
55                       CURLFORM_COPYCONTENTS, "content", CURLFORM_END);
56   )
57   fail_unless(rc == 0, "curl_formadd returned error");
58 
59   /* after the first curl_formadd when there's a single entry, both pointers
60      should point to the same struct */
61   fail_unless(post == last, "post and last weren't the same");
62 
63   CURL_IGNORE_DEPRECATION(
64     rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "htmlcode",
65                       CURLFORM_COPYCONTENTS, "<HTML></HTML>",
66                       CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
67   )
68   fail_unless(rc == 0, "curl_formadd returned error");
69 
70   CURL_IGNORE_DEPRECATION(
71     rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "name_for_ptrcontent",
72                      CURLFORM_PTRCONTENTS, buffer, CURLFORM_END);
73   )
74   fail_unless(rc == 0, "curl_formadd returned error");
75 
76   CURL_IGNORE_DEPRECATION(
77     res = curl_formget(post, &total_size, print_httppost_callback);
78   )
79   fail_unless(res == 0, "curl_formget returned error");
80 
81   fail_unless(total_size == 518, "curl_formget got wrong size back");
82 
83   CURL_IGNORE_DEPRECATION(
84     curl_formfree(post);
85   )
86 
87   /* start a new formpost with a file upload and formget */
88   post = last = NULL;
89 
90   CURL_IGNORE_DEPRECATION(
91     rc = curl_formadd(&post, &last,
92                       CURLFORM_PTRNAME, "name of file field",
93                       CURLFORM_FILE, arg,
94                       CURLFORM_FILENAME, "custom named file",
95                       CURLFORM_END);
96   )
97   fail_unless(rc == 0, "curl_formadd returned error");
98 
99   CURL_IGNORE_DEPRECATION(
100     res = curl_formget(post, &total_size, print_httppost_callback);
101   )
102   fail_unless(res == 0, "curl_formget returned error");
103   fail_unless(total_size == 899, "curl_formget got wrong size back");
104 
105   CURL_IGNORE_DEPRECATION(
106     curl_formfree(post);
107   )
108 
109 UNITTEST_STOP
110