Git init
[external/curl.git] / tests / libtest / lib578.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include "test.h"
11
12 #include "memdebug.h"
13
14 /* The size of data should be kept below MAX_INITIAL_POST_SIZE! */
15 static char data[]="this is a short string.\n";
16
17 static size_t data_size = sizeof(data) / sizeof(char);
18
19 static int progress_callback(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
20 {
21   FILE *moo = fopen(libtest_arg2, "wb");
22
23   (void)clientp; /* UNUSED */
24   (void)dltotal; /* UNUSED */
25   (void)dlnow; /* UNUSED */
26
27   if(moo) {
28     if ((size_t)ultotal == data_size && (size_t)ulnow == data_size)
29       fprintf(moo, "PASSED, UL data matched data size\n");
30     else
31       fprintf(moo, "Progress callback called with UL %f out of %f\n", ulnow, ultotal);
32     fclose(moo);
33   }
34   return 0;
35 }
36
37 int test(char *URL)
38 {
39   CURL *curl;
40   CURLcode res=CURLE_OK;
41
42   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
43     fprintf(stderr, "curl_global_init() failed\n");
44     return TEST_ERR_MAJOR_BAD;
45   }
46
47   if ((curl = curl_easy_init()) == NULL) {
48     fprintf(stderr, "curl_easy_init() failed\n");
49     curl_global_cleanup();
50     return TEST_ERR_MAJOR_BAD;
51   }
52
53   /* First set the URL that is about to receive our POST. */
54   test_setopt(curl, CURLOPT_URL, URL);
55
56   /* Now specify we want to POST data */
57   test_setopt(curl, CURLOPT_POST, 1L);
58
59 #ifdef CURL_DOES_CONVERSIONS
60   /* Convert the POST data to ASCII */
61   test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
62 #endif
63
64   /* Set the expected POST size */
65   test_setopt(curl, CURLOPT_POSTFIELDSIZE, data_size);
66   test_setopt(curl, CURLOPT_POSTFIELDS, data);
67
68   /* we want to use our own progress function */
69   test_setopt(curl, CURLOPT_NOPROGRESS, 0L);
70   test_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback);
71
72   /* pointer to pass to our read function */
73
74   /* get verbose debug output please */
75   test_setopt(curl, CURLOPT_VERBOSE, 1L);
76
77   /* include headers in the output */
78   test_setopt(curl, CURLOPT_HEADER, 1L);
79
80   /* Perform the request, res will get the return code */
81   res = curl_easy_perform(curl);
82
83 test_cleanup:
84
85   /* always cleanup */
86   curl_easy_cleanup(curl);
87   curl_global_cleanup();
88
89   return res;
90 }