From 9529c697038ea1ebff53ce76c07220fa1e870714 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 1 Aug 2024 14:38:35 +0200 Subject: [PATCH] expand PP test a bit --- src/tests/client_server/libtest.h | 23 ++++++++++ .../libtest_convenience_client_request.c | 39 ++++++++++------ src/tests/client_server/test_postprocessor.c | 45 +++++++++++++++++++ 3 files changed, 93 insertions(+), 14 deletions(-) diff --git a/src/tests/client_server/libtest.h b/src/tests/client_server/libtest.h index 1c00985a..ef2101ea 100644 --- a/src/tests/client_server/libtest.h +++ b/src/tests/client_server/libtest.h @@ -155,6 +155,29 @@ struct MHDT_PostInstructions */ enum MHD_HTTP_PostEncoding enc; + /** + * Data to be POSTed to the server. + */ + const char *postdata; + + /** + * HTTP header to set POST content encoding, use + * NULL if you want to set @e request_hdr directly. + */ + const char *postheader; + + /** + * HTTP header values for the request, automatically + * updated to include @e postheader. + */ + struct curl_slist *request_hdr; + + /** + * Number of bytes in @e postdata, use 0 for + * 0-terminated @e postdata. + */ + size_t postdata_size; + /** * size to use for the buffer. */ diff --git a/src/tests/client_server/libtest_convenience_client_request.c b/src/tests/client_server/libtest_convenience_client_request.c index c7f79d06..74cf3ffc 100644 --- a/src/tests/client_server/libtest_convenience_client_request.c +++ b/src/tests/client_server/libtest_convenience_client_request.c @@ -574,12 +574,6 @@ MHDT_client_do_post ( const struct MHDT_PhaseContext *pc) { struct MHDT_PostInstructions *pi = cls; - const char *text = "fixme"; - struct ReadBuffer rb = { - .buf = text, - .len = strlen (text), - .chunks = 2 - }; CURL *c; c = curl_easy_init (); @@ -595,27 +589,44 @@ MHDT_client_do_post ( } if (CURLE_OK != curl_easy_setopt (c, - CURLOPT_UPLOAD, + CURLOPT_POST, 1L)) { curl_easy_cleanup (c); - return "Failed to set PUT method for curl request"; + return "Failed to set POST method for curl request"; } if (CURLE_OK != curl_easy_setopt (c, - CURLOPT_READFUNCTION, - &read_cb)) + CURLOPT_POSTFIELDS, + pi->postdata)) { curl_easy_cleanup (c); - return "Failed to set READFUNCTION for curl request"; + return "Failed to set POSTFIELDS for curl request"; + } + if (0 != pi->postdata_size) + { + if (CURLE_OK != + curl_easy_setopt (c, + CURLOPT_POSTFIELDSIZE_LARGE, + (curl_off_t) pi->postdata_size)) + { + curl_easy_cleanup (c); + return "Failed to set POSTFIELDS for curl request"; + } + } + if (NULL != pi->postheader) + { + pi->request_hdr = curl_slist_append (pi->request_hdr, + pi->postheader); + pi->postheader = NULL; } if (CURLE_OK != curl_easy_setopt (c, - CURLOPT_READDATA, - &rb)) + CURLOPT_HTTPHEADER, + pi->request_hdr)) { curl_easy_cleanup (c); - return "Failed to set READFUNCTION for curl request"; + return "Failed to set HTTPHEADER for curl request"; } PERFORM_REQUEST (c); CHECK_STATUS (c, diff --git a/src/tests/client_server/test_postprocessor.c b/src/tests/client_server/test_postprocessor.c index 25d6a830..517532d7 100644 --- a/src/tests/client_server/test_postprocessor.c +++ b/src/tests/client_server/test_postprocessor.c @@ -56,6 +56,35 @@ main (int argc, char *argv[]) }; struct MHDT_PostInstructions simple_pi = { .enc = MHD_HTTP_POST_ENCODING_FORM_URLENCODED, + .postdata = "V1=One&V2=Two", + .postheader = "application/x-www-form-urlencoded", + .buffer_size = 32, + .auto_stream_size = 16 + }; + struct MHDT_PostInstructions simple_mp = { + .enc = MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA, + .postdata = "--{boundary string}\n" + "Content-Disposition: form-data; name=\"username\",\n" + "\n" + "Bob\n" + "--XXXX\n" + "Content-Disposition: form-data; name=\"password\",\n" + "\n" + "Passwo3d\n" + "--XXXX\n" + "Content-Disposition: form-data; name=\"file\"; filename=\"image.jpg\"\n" + "Content-Type: image/jpeg,\n" + "\n" + "IMAGEDATA" + "--XXXX--\n", + .postheader = "multipart/form-data; boundary=XXXX", + .buffer_size = 32, + .auto_stream_size = 16 + }; + struct MHDT_PostInstructions simple_tp = { + .enc = MHD_HTTP_POST_ENCODING_TEXT_PLAIN, + .postdata = "V1=One\r\nV2=Two\r\n", + .postheader = "text/plain", .buffer_size = 32, .auto_stream_size = 16 }; @@ -68,6 +97,22 @@ main (int argc, char *argv[]) .client_cb_cls = &simple_pi, .timeout_ms = 2500, }, + { + .label = "multipart post", + .server_cb = &MHDT_server_reply_check_post, + .server_cb_cls = &simple_mp, + .client_cb = &MHDT_client_do_post, + .client_cb_cls = &simple_mp, + .timeout_ms = 2500, + }, + { + .label = "plain text post", + .server_cb = &MHDT_server_reply_check_post, + .server_cb_cls = &simple_tp, + .client_cb = &MHDT_client_do_post, + .client_cb_cls = &simple_tp, + .timeout_ms = 2500, + }, { .label = NULL, },