src/server/test/httpclient.c

Tue, 24 Feb 2026 21:28:06 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 24 Feb 2026 21:28:06 +0100
changeset 703
395c62fac7e5
parent 701
936e5487418a
permissions
-rw-r--r--

add more httpclient tests

/*
 * 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);
    test_finished = 1;
}

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_blocks, size_t num_blocks, CxBuffer *out, 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");
    
    test_finished = 0;
    int ret = http_client_start(client);
    CX_TEST_ASSERT(ret == 0);
    
    for(int i=0;i<num_blocks;i++) {
        size_t response_pos = 0;
        cxstring response = response_blocks[i];
        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, 1, out, 0);
        CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n"));
        
        cxBufferFree(out);
    }
}

CX_TEST(test_http_client_simple_get_line_io) {
    CX_TEST_DO {
        cxstring response[8];
        response[0] = cx_str("HTTP/1.1 200 OK\r\n");
        response[1] = cx_str("Content-length: 13\r\n");
        response[2] = cx_str("\r\n");
        response[3] = cx_str("Hello World!\n");
        
        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_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n"));
        
        cxBufferFree(out);
    }
}

CX_TEST(test_http_client_simple_get_small_blocksize) {
    CX_TEST_DO {
        cxstring response[16];
        response[0] = cx_str("HTTP/1.1");
        response[1] = cx_str(" 2");
        response[2] = cx_str("00 ");
        response[3] = cx_str(" O");
        response[4] = cx_str("K\r\nContent-");
        response[5] = cx_str("length:");
        response[6] = cx_str(" 26");
        response[7] = cx_str("\r");
        response[8] = cx_str("\nHost");
        response[9] = cx_str(": localhost\r\n\r\nHello");
        response[10] = cx_str(" World!\n");
        response[11] = cx_str("He");
        response[12] = cx_str("llo Wor");
        response[13] = cx_str("l");
        response[14] = cx_str("d!");
        response[15] = cx_str("\n");
        
        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_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\nHello World!\n"));
        
        cxBufferFree(out);
    }
}

CX_TEST(test_http_client_simple_get_1b_blocksize) {
    CX_TEST_DO {
        cxstring response_str = cx_str(
                "HTTP/1.1 200 OK\r\n"
                "Content-length: 13\r\n"
                "\r\n"
                "Hello World!\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);
        }
        
        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_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n"));
        
        cxBufferFree(out);
    }
}

mercurial