Add Web server test part
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
NADAL Jean-Baptiste
2020-02-20 19:44:55 +01:00
parent b9beca6d72
commit af35811644
6 changed files with 680 additions and 16 deletions

1
src/tests/index.html Normal file
View File

@@ -0,0 +1 @@
<html><body><h1>TEST</h1></html>

View File

@@ -29,6 +29,7 @@
/*------------------------------- INCLUDES ----------------------------------*/
#include <fcntl.h>
#include <libgen.h>
#include <unistd.h>
#include <qlibc/qlibc.h>

View File

@@ -49,6 +49,8 @@
#define kput_method "PUT"
#define kdelete_method "DELETE"
#define k_max_path_len 200 /* make this larger if you need to. */
/*--------------------------------------------------------------------------*/
int my_error_handler(restd_resp_t *response, int reason, void *arg)
@@ -348,6 +350,39 @@ bool found_special_route(restd_server_t *server, enum evhttp_cmd_type method, co
/*--------------------------------------------------------------------------*/
char *get_config_path(void)
{
int length;
char full_path[k_max_path_len];
char *working_path;
char *last_slash;
length = readlink("/proc/self/exe", full_path, sizeof(full_path));
/* Catch some errors: */
if (length < 0)
{
fprintf(stderr, "Error resolving symlink /proc/self/exe.\n");
return NULL;
}
if (length >= k_max_path_len)
{
fprintf(stderr, "Path too long. Truncated.\n");
return NULL;
}
/* I don't know why, but the string this readlink() function
* returns is appended with a '@'.
*/
full_path[length] = '\0'; /* Strip '@' off the end. */
working_path = dirname(full_path);
return working_path;
}
/*--------------------------------------------------------------------------*/
TEST("Rest - create free\t")
{
restd_server_t *rest_server;
@@ -569,3 +604,36 @@ TEST("Rest - create start access to wrong path\t")
restd_server_free(rest_server);
}
/*--------------------------------------------------------------------------*/
TEST("Rest - Test Web server part.\t")
{
int ret;
restd_server_t *rest_server;
char *root_path;
rest_server = restd_server_new();
ASSERT_NOT_NULL(rest_server);
root_path = get_config_path();
restd_server_set_option(rest_server, "server.port", kserver_port);
restd_server_set_option(rest_server, "server.thread", "1");
restd_server_set_option(rest_server, "server.root_path", root_path);
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, kapi_test_get, my_success_rest_get_handler, NULL);
restd_server_register_error_handler(rest_server, my_error_handler, NULL);
ret = restd_server_start(rest_server);
ASSERT_EQUAL_INT(ret, 0);
sleep(1);
for (int i = 0; i < 10; i++)
{
ret = exec_request(kput_method, "http://localhost:" kserver_port "/index.html", 200, "", "<html><body><h1>TEST</h1></html>", false);
ASSERT_EQUAL_INT(ret, 0);
}
restd_server_free(rest_server);
}