From b747124b9ca25e4af6d5520089e000d7f9c59f39 Mon Sep 17 00:00:00 2001 From: Francesco Date: Thu, 12 Mar 2026 01:11:28 +0100 Subject: [PATCH] feat(test): add get index test --- meson.build | 18 ++++++++++---- src/config/config.c | 1 - src/core/worker.c | 5 ++-- src/http/handler.c | 1 + test/server.c | 58 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 test/server.c diff --git a/meson.build b/meson.build index 6e7ce78..baaba88 100644 --- a/meson.build +++ b/meson.build @@ -1,8 +1,8 @@ project( - 'cws', - 'c', - version: '0.1.0', - default_options: ['c_std=gnu23', 'warning_level=3'], + 'cws', + 'c', + version: '0.1.0', + default_options: ['c_std=gnu23', 'warning_level=3'], ) add_global_arguments('-Wno-pedantic', language: 'c') @@ -23,4 +23,12 @@ add_global_arguments('-DUSE_COLORS', language: 'c') add_global_arguments('-DEVELOPER', language: 'c') add_global_arguments('-D_POSIX_C_SOURCE=200809L', language: 'c') -executable('cws', server, include_directories: incdir, dependencies: deps) +exe = executable('cws', server, include_directories: incdir, dependencies: deps) + +# Test +test_src = files('test/server.c') +test_curl_dep = dependency('libcurl', required: false) +test_deps = [test_curl_dep] +test_exec = executable('test_http', test_src, dependencies: test_deps) + +test('index get', test_exec, args: [exe.full_path()]) \ No newline at end of file diff --git a/src/config/config.c b/src/config/config.c index f0b0ce1..231e99a 100644 --- a/src/config/config.c +++ b/src/config/config.c @@ -2,7 +2,6 @@ #include #include -#include #include #include "utils/debug.h" diff --git a/src/core/worker.c b/src/core/worker.c index fd279f5..f188114 100644 --- a/src/core/worker.c +++ b/src/core/worker.c @@ -35,8 +35,9 @@ static cws_vhost_s *get_vhost(cws_config_s *config, char *host) { } } - /* This should not happen */ - return NULL; + /* Return first domain */ + /* TODO: return default domain */ + return &config->virtual_hosts[0]; } static cws_return worker_handle_client_data(int epfd, int client_fd, cws_config_s *config) { diff --git a/src/http/handler.c b/src/http/handler.c index ca998c2..191c4d9 100644 --- a/src/http/handler.c +++ b/src/http/handler.c @@ -5,6 +5,7 @@ #include /* Sanitize and resolve file path */ +/* TODO: fix path traversal */ static string_s *resolve_file_path(const char *url_path, cws_handler_config_s *config) { string_s *full_path = string_new(config->root, 256); diff --git a/test/server.c b/test/server.c new file mode 100644 index 0000000..6f72ac1 --- /dev/null +++ b/test/server.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include + +#define URL "http://localhost:3030" + +int main(int argc, char **argv) { + if (argc < 2) { + return 1; + } + + char *server_path = argv[1]; + fprintf(stdout, "server path: %s\n", server_path); + + /* Run the server as child proc */ + pid_t pid = fork(); + if (pid == 0) { + execl(server_path, "cws", NULL); + fprintf(stderr, "execl failed: %s\n", strerror(errno)); + exit(1); + } + + if (pid == -1) { + fprintf(stderr, "fork failed: %s\n", strerror(errno)); + exit(1); + } + + fprintf(stdout, "pid: %d\n", pid); + + CURL *curl = curl_easy_init(); + curl_easy_setopt(curl, CURLOPT_URL, URL); + curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 1L); + + long http_code = 0; + int max_retries = 10; + for (int i = 0; i < max_retries; i++) { + CURLcode res = curl_easy_perform(curl); + if (res == CURLE_OK) { + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); + break; + } + fprintf(stdout, "Server not ready, retry %d/%d...\n", i + 1, max_retries); + sleep(1); + } + + curl_easy_cleanup(curl); + + fprintf(stdout, "http_code: %ld\n", http_code); + + /* Kill the server */ + kill(pid, SIGTERM); + waitpid(pid, NULL, 0); + + return http_code == 200 ? 0 : 1; +}