src/server/test/httpclient.c

changeset 705
30de3bfd0412
parent 704
778dcf4ad63c
--- a/src/server/test/httpclient.c	Wed Feb 25 22:16:20 2026 +0100
+++ b/src/server/test/httpclient.c	Wed Feb 25 22:38:51 2026 +0100
@@ -71,7 +71,26 @@
     return cxBufferWrite(str, 1, nbytes, userdata);
 }
 
-CX_TEST_SUBROUTINE(test_httpclient, cxstring *response_blocks, size_t num_blocks, CxBuffer *out, int error_interval) {
+static cxstring request_body_str;
+static int request_body_pos = 0;
+static size_t request_body_max_read = 16;
+
+ssize_t test_request_body_read(HttpClient *client, void *buf, size_t nbytes, void *userdata) {
+    if(nbytes > request_body_max_read) {
+        nbytes = request_body_max_read;
+    }
+    size_t available = request_body_str.length - request_body_pos;
+    if(nbytes > available) {
+        nbytes = available;
+    }
+    if(nbytes > 0) {
+        memcpy(buf, request_body_str.ptr + request_body_pos, nbytes);
+        request_body_pos += nbytes;
+    }
+    return nbytes;
+}
+
+CX_TEST_SUBROUTINE(test_httpclient, cxstring request_body, int chunked_transfer, cxstring *response_blocks, size_t num_blocks, CxBuffer *out) {
     HttpClient *client = http_client_new(test_eventhandler->instances[0]);
     client->response_body_write = test_response_body_write;
     client->response_body_write_userdata = out;
@@ -84,6 +103,17 @@
     http_client_set_socket(client, fds[0]);
     http_client_set_method(client, "GET");
     http_client_set_uri(client, "/testx01");
+    
+    request_body_str = request_body;
+    request_body_pos = 0;
+    if(request_body.length > 0) {
+        client->request_body_read = test_request_body_read;
+        if(chunked_transfer) {
+            http_client_enable_chunked_transfer_encoding(client);
+        } else {
+            http_client_set_content_length(client, request_body.length);
+        }
+    }
      
     test_finished = 0;
     int ret = http_client_start(client);
@@ -119,7 +149,7 @@
                 "Hello World!\n");
         CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
         
-        CX_TEST_CALL_SUBROUTINE(test_httpclient, &response, 1, out, 0);
+        CX_TEST_CALL_SUBROUTINE(test_httpclient, cx_str(NULL), FALSE, &response, 1, out);
         CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n"));
         
         cxBufferFree(out);
@@ -136,7 +166,7 @@
         
         CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
         
-        CX_TEST_CALL_SUBROUTINE(test_httpclient, response, 4, out, 0);
+        CX_TEST_CALL_SUBROUTINE(test_httpclient, cx_str(NULL), FALSE, response, 4, out);
         CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n"));
         
         cxBufferFree(out);
@@ -165,7 +195,7 @@
         
         CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
         
-        CX_TEST_CALL_SUBROUTINE(test_httpclient, response, 16, out, 0);
+        CX_TEST_CALL_SUBROUTINE(test_httpclient, cx_str(NULL), FALSE, response, 16, out);
         CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\nHello World!\n"));
         
         cxBufferFree(out);
@@ -186,9 +216,55 @@
         
         CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
         
-        CX_TEST_CALL_SUBROUTINE(test_httpclient, response, response_str.length, out, 0);
+        CX_TEST_CALL_SUBROUTINE(test_httpclient, cx_str(NULL), FALSE, response, response_str.length, out);
         CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n"));
         
         cxBufferFree(out);
     }
 }
+
+CX_TEST(test_http_client_post_ctlen) {
+    CX_TEST_DO {
+        cxstring response_str = cx_str(
+                "HTTP/1.1 200 OK\r\n"
+                "Content-length: 21\r\n"
+                "\r\n"
+                "Hello World!---post1\n");
+        cxstring *response = calloc(response_str.length, sizeof(cxstring));
+        for(int i=0;i<response_str.length;i++) {
+            response[i] = cx_strn(response_str.ptr+i, 1);
+        }
+        
+        cxstring request_body = cx_str("test request body, needs more than one read (len > 16)\n");
+        
+        CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
+        
+        CX_TEST_CALL_SUBROUTINE(test_httpclient, request_body, FALSE, response, response_str.length, out);
+        CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!---post1\n"));
+        
+        cxBufferFree(out);
+    }
+}
+
+CX_TEST(test_http_client_post_chunked) {
+    CX_TEST_DO {
+        cxstring response_str = cx_str(
+                "HTTP/1.1 200 OK\r\n"
+                "Content-length: 21\r\n"
+                "\r\n"
+                "Hello World!---post1\n");
+        cxstring *response = calloc(response_str.length, sizeof(cxstring));
+        for(int i=0;i<response_str.length;i++) {
+            response[i] = cx_strn(response_str.ptr+i, 1);
+        }
+        
+        cxstring request_body = cx_str("test request body, needs more than one read (len > 16)\n");
+        
+        CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
+        
+        CX_TEST_CALL_SUBROUTINE(test_httpclient, request_body, TRUE, response, response_str.length, out);
+        CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!---post1\n"));
+        
+        cxBufferFree(out);
+    }
+}

mercurial