feat(test): add get index test
This commit is contained in:
+13
-5
@@ -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()])
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <string.h>
|
||||
#include <tomlc17.h>
|
||||
|
||||
#include "utils/debug.h"
|
||||
|
||||
+3
-2
@@ -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) {
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <sys/stat.h>
|
||||
|
||||
/* 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);
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
#include <curl/curl.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user