src/server/test/httpclient.c

changeset 701
936e5487418a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/server/test/httpclient.c	Mon Feb 23 22:28:44 2026 +0100
@@ -0,0 +1,119 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2026 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+
+#include "httpclient.h"
+
+#include "../util/socket.h"
+#include "../daemon/event.h"
+#include "../proxy/httpclient.h"
+
+#include <cx/buffer.h>
+#include <cx/string.h>
+
+#include <pthread.h>
+
+static EVHandler *test_eventhandler;
+static pthread_mutex_t test_mutex;
+static pthread_cond_t test_cond;
+static volatile int test_finished;
+
+static void test_response_finished(HttpClient *client, void *userdata) {
+    pthread_mutex_lock(&test_mutex);
+    pthread_cond_signal(&test_cond);
+    pthread_mutex_unlock(&test_mutex);
+    http_client_free(client);
+}
+
+static ssize_t test_response_body_write(HttpClient *client, void *buf, size_t nbytes, void *userdata) {
+    char *str = buf;
+    return cxBufferWrite(buf, 1, nbytes, userdata);
+}
+
+CX_TEST_SUBROUTINE(test_httpclient, cxstring response, CxBuffer *out, int response_blocksz, int error_interval) {
+    pthread_mutex_init(&test_mutex, NULL);
+    pthread_cond_init(&test_cond, NULL);
+    
+    if(!test_eventhandler) {
+        EventHandlerConfig config;
+        config.isdefault = 0;
+        config.name = cx_str(NULL);
+        config.nthreads = 1;
+        test_eventhandler = evhandler_create(&config);
+    }
+    
+    HttpClient *client = http_client_new(test_eventhandler->instances[0]);
+    client->response_body_write = test_response_body_write;
+    client->response_body_write_userdata = out;
+    client->response_finished = test_response_finished;
+
+    int fds[2];
+    util_socketpair(fds);
+    util_socket_setnonblock(fds[0], 1);
+    
+    http_client_set_socket(client, fds[0]);
+    http_client_set_method(client, "GET");
+    http_client_set_uri(client, "/testx01");
+    
+    int ret = http_client_start(client);
+    CX_TEST_ASSERT(ret == 0);
+    
+    size_t response_pos = 0;
+    while(response_pos < response.length) { 
+        size_t nbytes = response.length - response_pos;
+        const char *str = response.ptr + response_pos;
+        ssize_t w = write(fds[1], str, nbytes);
+        CX_TEST_ASSERT(w >= 0);
+        response_pos += w;
+    }
+    close(fds[1]);
+    
+    pthread_mutex_lock(&test_mutex);
+    if(test_finished == 0) {
+        pthread_cond_wait(&test_cond, &test_mutex);
+    }
+    pthread_mutex_unlock(&test_mutex);
+    
+    
+}
+
+CX_TEST(test_http_client_simple_get1) {
+    CX_TEST_DO {
+        cxstring response = cx_str(
+                "HTTP/1.1 200 OK\r\n"
+                "Content-length: 13\r\n"
+                "\r\n"
+                "Hello World!\n");
+        CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
+        
+        CX_TEST_CALL_SUBROUTINE(test_httpclient, response, out, 1024, 0);
+        CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n"));
+        
+        cxBufferFree(out);
+    }
+}

mercurial