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