Git init
[external/curl.git] / tests / libtest / lib508.c
1 /*****************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  */
9
10 #include "test.h"
11
12 #include "memdebug.h"
13
14 static char data[]="this is what we post to the silly web server\n";
15
16 struct WriteThis {
17   char *readptr;
18   size_t sizeleft;
19 };
20
21 static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp)
22 {
23   struct WriteThis *pooh = (struct WriteThis *)userp;
24
25   if(size*nmemb < 1)
26     return 0;
27
28   if(pooh->sizeleft) {
29     *(char *)ptr = pooh->readptr[0]; /* copy one single byte */
30     pooh->readptr++;                 /* advance pointer */
31     pooh->sizeleft--;                /* less data left */
32     return 1;                        /* we return 1 byte at a time! */
33   }
34
35   return 0;                         /* no more data left to deliver */
36 }
37
38 int test(char *URL)
39 {
40   CURL *curl;
41   CURLcode res=CURLE_OK;
42
43   struct WriteThis pooh;
44
45   pooh.readptr = data;
46   pooh.sizeleft = strlen(data);
47
48   if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
49     fprintf(stderr, "curl_global_init() failed\n");
50     return TEST_ERR_MAJOR_BAD;
51   }
52
53   if ((curl = curl_easy_init()) == NULL) {
54     fprintf(stderr, "curl_easy_init() failed\n");
55     curl_global_cleanup();
56     return TEST_ERR_MAJOR_BAD;
57   }
58
59   /* First set the URL that is about to receive our POST. */
60   test_setopt(curl, CURLOPT_URL, URL);
61
62   /* Now specify we want to POST data */
63   test_setopt(curl, CURLOPT_POST, 1L);
64
65 #ifdef CURL_DOES_CONVERSIONS
66   /* Convert the POST data to ASCII */
67   test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L);
68 #endif
69
70   /* Set the expected POST size */
71   test_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)pooh.sizeleft);
72
73   /* we want to use our own read function */
74   test_setopt(curl, CURLOPT_READFUNCTION, read_callback);
75
76   /* pointer to pass to our read function */
77   test_setopt(curl, CURLOPT_INFILE, &pooh);
78
79   /* get verbose debug output please */
80   test_setopt(curl, CURLOPT_VERBOSE, 1L);
81
82   /* include headers in the output */
83   test_setopt(curl, CURLOPT_HEADER, 1L);
84
85   /* Perform the request, res will get the return code */
86   res = curl_easy_perform(curl);
87
88 test_cleanup:
89
90   /* always cleanup */
91   curl_easy_cleanup(curl);
92   curl_global_cleanup();
93
94   return res;
95 }