xref: /curl/tests/libtest/lib1948.c (revision 25cbc2f7)
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.haxx.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 
25 #include "test.h"
26 
27 typedef struct
28 {
29   char *buf;
30   size_t len;
31 } put_buffer;
32 
put_callback(char * ptr,size_t size,size_t nmemb,void * stream)33 static size_t put_callback(char *ptr, size_t size, size_t nmemb, void *stream)
34 {
35   put_buffer *putdata = (put_buffer *)stream;
36   size_t totalsize = size * nmemb;
37   size_t tocopy = (putdata->len < totalsize) ? putdata->len : totalsize;
38   memcpy(ptr, putdata->buf, tocopy);
39   putdata->len -= tocopy;
40   putdata->buf += tocopy;
41   return tocopy;
42 }
43 
test(char * URL)44 CURLcode test(char *URL)
45 {
46   CURL *curl;
47   CURLcode res = CURLE_OK;
48   const char *testput = "This is test PUT data\n";
49   put_buffer pbuf;
50 
51   curl_global_init(CURL_GLOBAL_DEFAULT);
52 
53   easy_init(curl);
54 
55   /* PUT */
56   easy_setopt(curl, CURLOPT_UPLOAD, 1L);
57   easy_setopt(curl, CURLOPT_HEADER, 1L);
58   easy_setopt(curl, CURLOPT_READFUNCTION, put_callback);
59   pbuf.buf = (char *)testput;
60   pbuf.len = strlen(testput);
61   easy_setopt(curl, CURLOPT_READDATA, &pbuf);
62   easy_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(testput));
63   easy_setopt(curl, CURLOPT_URL, URL);
64   res = curl_easy_perform(curl);
65   if(res)
66     goto test_cleanup;
67 
68   /* POST */
69   easy_setopt(curl, CURLOPT_POST, 1L);
70   easy_setopt(curl, CURLOPT_POSTFIELDS, testput);
71   easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(testput));
72   res = curl_easy_perform(curl);
73 
74 test_cleanup:
75   curl_easy_cleanup(curl);
76   curl_global_cleanup();
77   return res;
78 }
79