quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

lib1308.c (3656B)


      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.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 #include "first.h"
     25 
     26 static size_t print_httppost_callback(void *arg, const char *buf, size_t len)
     27 {
     28   fwrite(buf, len, 1, stdout);
     29   (*(size_t *) arg) += len;
     30   return len;
     31 }
     32 
     33 #define t1308_fail_unless(expr, msg)                             \
     34   do {                                                           \
     35     if(!(expr)) {                                                \
     36       curl_mfprintf(stderr, "%s:%d Assertion '%s' FAILED: %s\n", \
     37                     __FILE__, __LINE__, #expr, msg);             \
     38       errorcount++;                                              \
     39     }                                                            \
     40   } while(0)
     41 
     42 static CURLcode test_lib1308(char *URL)
     43 {
     44   int errorcount = 0;
     45   CURLFORMcode rc;
     46   int res;
     47   struct curl_httppost *post = NULL;
     48   struct curl_httppost *last = NULL;
     49   size_t total_size = 0;
     50   char buffer[] = "test buffer";
     51 
     52   rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "name",
     53                     CURLFORM_COPYCONTENTS, "content", CURLFORM_END);
     54   t1308_fail_unless(rc == 0, "curl_formadd returned error");
     55 
     56   /* after the first curl_formadd when there's a single entry, both pointers
     57      should point to the same struct */
     58   t1308_fail_unless(post == last, "post and last weren't the same");
     59 
     60   rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "htmlcode",
     61                     CURLFORM_COPYCONTENTS, "<HTML></HTML>",
     62                     CURLFORM_CONTENTTYPE, "text/html", CURLFORM_END);
     63   t1308_fail_unless(rc == 0, "curl_formadd returned error");
     64 
     65   rc = curl_formadd(&post, &last, CURLFORM_COPYNAME, "name_for_ptrcontent",
     66                     CURLFORM_PTRCONTENTS, buffer, CURLFORM_END);
     67   t1308_fail_unless(rc == 0, "curl_formadd returned error");
     68 
     69   res = curl_formget(post, &total_size, print_httppost_callback);
     70   t1308_fail_unless(res == 0, "curl_formget returned error");
     71 
     72   t1308_fail_unless(total_size == 518, "curl_formget got wrong size back");
     73 
     74   curl_formfree(post);
     75 
     76   /* start a new formpost with a file upload and formget */
     77   post = last = NULL;
     78 
     79   rc = curl_formadd(&post, &last,
     80                     CURLFORM_PTRNAME, "name of file field",
     81                     CURLFORM_FILE, URL,
     82                     CURLFORM_FILENAME, "custom named file",
     83                     CURLFORM_END);
     84   t1308_fail_unless(rc == 0, "curl_formadd returned error");
     85 
     86   res = curl_formget(post, &total_size, print_httppost_callback);
     87 
     88   t1308_fail_unless(res == 0, "curl_formget returned error");
     89   t1308_fail_unless(total_size == 899, "curl_formget got wrong size back");
     90 
     91   curl_formfree(post);
     92 
     93   return errorcount ? TEST_ERR_FAILURE : CURLE_OK;
     94 }