diff options
author | Julien Chaffraix <julien.chaffraix@gmail.com> | 2010-08-19 00:27:04 +0200 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2010-08-19 00:27:04 +0200 |
commit | 70baf46d8ddfc07b488d17cf0f5d8e23cf7de14e (patch) | |
tree | cfa01b5a681f6d8d4da9cc7a72aee15ce5b74739 /tests/libtest | |
parent | 280d2cff2eed563cb19b46056fcb17e06dc4e158 (diff) |
test: added test 579 to verify progress callback for chunked post
The 66 bytes checked are those 38 bytes with the chunked encoding
headers added: 8+8+10+35+5 = 66
The three-letter words become 8 bytes on the wire because they are sent
like: "3\r\none\r\n"
... and there's the trailing 5 bytes write after the four lines since
the final chunk is sent (which is "0\r\n\r\n").
Diffstat (limited to 'tests/libtest')
-rw-r--r-- | tests/libtest/Makefile.inc | 4 | ||||
-rw-r--r-- | tests/libtest/lib579.c | 149 |
2 files changed, 152 insertions, 1 deletions
diff --git a/tests/libtest/Makefile.inc b/tests/libtest/Makefile.inc index a0d73b8ef..a0f1c0ecb 100644 --- a/tests/libtest/Makefile.inc +++ b/tests/libtest/Makefile.inc @@ -8,7 +8,7 @@ SUPPORTFILES = first.c test.h noinst_PROGRAMS = lib500 lib501 lib502 lib503 lib504 lib505 lib506 \ lib507 lib508 lib510 lib511 lib512 lib513 lib514 lib515 lib516 \ lib517 lib518 lib519 lib520 lib521 lib523 lib524 lib525 lib526 lib527 \ - lib574 lib575 lib576 lib577 lib578 \ + lib574 lib575 lib576 lib577 lib578 lib579 \ lib529 lib530 lib532 lib533 lib536 lib537 lib540 lib541 lib542 lib543 \ lib544 lib545 lib547 lib548 lib549 lib552 lib553 lib554 lib555 lib556 \ lib539 lib557 lib558 lib559 lib560 lib562 lib564 lib565 lib566 lib567 \ @@ -162,3 +162,5 @@ lib572_SOURCES = lib572.c $(SUPPORTFILES) lib573_SOURCES = lib573.c $(SUPPORTFILES) $(TESTUTIL) lib578_SOURCES = lib578.c $(SUPPORTFILES) + +lib579_SOURCES = lib579.c $(SUPPORTFILES) diff --git a/tests/libtest/lib579.c b/tests/libtest/lib579.c new file mode 100644 index 000000000..01e91d8f0 --- /dev/null +++ b/tests/libtest/lib579.c @@ -0,0 +1,149 @@ +/***************************************************************************** + * _ _ ____ _ + * Project ___| | | | _ \| | + * / __| | | | |_) | | + * | (__| |_| | _ <| |___ + * \___|\___/|_| \_\_____| + * + */ + +#include "test.h" + +#include "memdebug.h" + +static const char *post[]={ + "one", + "two", + "three", + "and a final longer crap: four", + NULL +}; + + +struct WriteThis { + int counter; +}; + +static int progress_callback(void *clientp, double dltotal, double dlnow, + double ultotal, double ulnow) +{ + FILE *moo; + (void)clientp; /* UNUSED */ + (void)dltotal; /* UNUSED */ + (void)dlnow; /* UNUSED */ + static int prev_ultotal = -1; + static int prev_ulnow = -1; + + /* to avoid depending on timing, which will cause this progress function to + get called a different number of times depending on circumstances, we + only log these lines if the numbers are different from the previous + invoke */ + if((prev_ultotal != (int)ultotal) || + (prev_ulnow != (int)ulnow)) { + + moo = fopen(libtest_arg2, "ab"); + if(moo) { + fprintf(moo, "Progress callback called with UL %d out of %d\n", + (int)ulnow, (int)ultotal); + fclose(moo); + } + prev_ulnow = (int) ulnow; + prev_ultotal = (int) ultotal; + } + return 0; +} + +static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *userp) +{ + struct WriteThis *pooh = (struct WriteThis *)userp; + const char *data; + + if(size*nmemb < 1) + return 0; + + data = post[pooh->counter]; + + if(data) { + size_t len = strlen(data); + memcpy(ptr, data, len); + pooh->counter++; /* advance pointer */ + return len; + } + return 0; /* no more data left to deliver */ +} + +int test(char *URL) +{ + CURL *curl; + CURLcode res=CURLE_OK; + struct curl_slist *slist = NULL; + struct WriteThis pooh; + pooh.counter = 0; + + if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) { + fprintf(stderr, "curl_global_init() failed\n"); + return TEST_ERR_MAJOR_BAD; + } + + if ((curl = curl_easy_init()) == NULL) { + fprintf(stderr, "curl_easy_init() failed\n"); + curl_global_cleanup(); + return TEST_ERR_MAJOR_BAD; + } + + slist = curl_slist_append(slist, "Transfer-Encoding: chunked"); + if (slist == NULL) { + fprintf(stderr, "curl_slist_append() failed\n"); + curl_easy_cleanup(curl); + curl_global_cleanup(); + return TEST_ERR_MAJOR_BAD; + } + + /* First set the URL that is about to receive our POST. */ + test_setopt(curl, CURLOPT_URL, URL); + + /* Now specify we want to POST data */ + test_setopt(curl, CURLOPT_POST, 1L); + +#ifdef CURL_DOES_CONVERSIONS + /* Convert the POST data to ASCII */ + test_setopt(curl, CURLOPT_TRANSFERTEXT, 1L); +#endif + + /* we want to use our own read function */ + test_setopt(curl, CURLOPT_READFUNCTION, read_callback); + + /* pointer to pass to our read function */ + test_setopt(curl, CURLOPT_INFILE, &pooh); + + /* get verbose debug output please */ + test_setopt(curl, CURLOPT_VERBOSE, 1L); + + /* include headers in the output */ + test_setopt(curl, CURLOPT_HEADER, 1L); + + /* enforce chunked transfer by setting the header */ + test_setopt(curl, CURLOPT_HTTPHEADER, slist); + + test_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); + test_setopt(curl, CURLOPT_USERPWD, "foo:bar"); + + /* we want to use our own progress function */ + test_setopt(curl, CURLOPT_NOPROGRESS, 0L); + test_setopt(curl, CURLOPT_PROGRESSFUNCTION, progress_callback); + + /* Perform the request, res will get the return code */ + res = curl_easy_perform(curl); + +test_cleanup: + + /* clean up the headers list */ + if(slist) + curl_slist_free_all(slist); + + /* always cleanup */ + curl_easy_cleanup(curl); + curl_global_cleanup(); + + return res; +} |