xref: /curl/tests/libtest/lib1525.c (revision 25cbc2f7)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9  * Copyright (C) Vijay Panghal, <vpanghal@maginatics.com>, et al.
10  *
11  * This software is licensed as described in the file COPYING, which
12  * you should have received as part of this distribution. The terms
13  * are also available at https://curl.se/docs/copyright.html.
14  *
15  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
16  * copies of the Software, and permit persons to whom the Software is
17  * furnished to do so, under the terms of the COPYING file.
18  *
19  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20  * KIND, either express or implied.
21  *
22  * SPDX-License-Identifier: curl
23  *
24  ***************************************************************************/
25 
26 /*
27  * This unit test PUT http data over proxy. Proxy header will be different
28  * from server http header
29  */
30 
31 #include "test.h"
32 
33 #include "memdebug.h"
34 
35 static char data [] = "Hello Cloud!\n";
36 
read_callback(char * ptr,size_t size,size_t nmemb,void * stream)37 static size_t read_callback(char *ptr, size_t size, size_t nmemb, void *stream)
38 {
39   size_t  amount = nmemb * size; /* Total bytes curl wants */
40   if(amount < strlen(data)) {
41     return strlen(data);
42   }
43   (void)stream;
44   memcpy(ptr, data, strlen(data));
45   return strlen(data);
46 }
47 
48 
test(char * URL)49 CURLcode test(char *URL)
50 {
51   CURL *curl = NULL;
52   CURLcode res = CURLE_FAILED_INIT;
53   /* http and proxy header list */
54   struct curl_slist *hhl = NULL;
55 
56   if(curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
57     fprintf(stderr, "curl_global_init() failed\n");
58     return TEST_ERR_MAJOR_BAD;
59   }
60 
61   curl = curl_easy_init();
62   if(!curl) {
63     fprintf(stderr, "curl_easy_init() failed\n");
64     curl_global_cleanup();
65     return TEST_ERR_MAJOR_BAD;
66   }
67 
68   hhl = curl_slist_append(hhl, "User-Agent: Http Agent");
69 
70   if(!hhl) {
71     goto test_cleanup;
72   }
73 
74   test_setopt(curl, CURLOPT_URL, URL);
75   test_setopt(curl, CURLOPT_PROXY, libtest_arg2);
76   test_setopt(curl, CURLOPT_HTTPHEADER, hhl);
77   test_setopt(curl, CURLOPT_PROXYHEADER, hhl);
78   test_setopt(curl, CURLOPT_HEADEROPT, CURLHEADER_UNIFIED);
79   test_setopt(curl, CURLOPT_POST, 0L);
80   test_setopt(curl, CURLOPT_UPLOAD, 1L);
81   test_setopt(curl, CURLOPT_VERBOSE, 1L);
82   test_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
83   test_setopt(curl, CURLOPT_HEADER, 1L);
84   test_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
85   test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
86   test_setopt(curl, CURLOPT_HTTPPROXYTUNNEL, 1L);
87   test_setopt(curl, CURLOPT_INFILESIZE, (long)strlen(data));
88 
89   res = curl_easy_perform(curl);
90 
91 test_cleanup:
92 
93   curl_easy_cleanup(curl);
94 
95   curl_slist_free_all(hhl);
96 
97   curl_global_cleanup();
98 
99   return res;
100 }
101