WIP to test with params
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
NADAL Jean-Baptiste
2020-02-19 11:18:07 +01:00
parent fb82198417
commit 6db405df1f
2 changed files with 187 additions and 186 deletions

View File

@@ -31,9 +31,18 @@
#define ksuccess_post_body "{\"status\":\"post\"}"
#define kerror_body "{\"status\":\"error\"}"
#define ksuccess_put_param1 "{\"id\":\"1\"}"
#define ksuccess_put_param2 "{\"id\":\"1977\"}"
#define kserver_port "7777"
#define kapi_test_get "/api/v1/test_get"
#define kapi_test_put_id1 "/api/v1/test_put/:id"
#define kapi_test_put_id1_body "/api/v1/test_put/1"
#define kapi_test_put_id2 "/api/v1/test_put/:id/action"
#define kapi_test_put_id2_body "/api/v1/test_put/1977/action"
#define kpost_method "POST"
#define kget_method "GET"
#define kput_method "PUT"
@@ -125,12 +134,12 @@ size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream)
/*--------------------------------------------------------------------------*/
int exec_request(const char *request, const char *path, int expected_code, const char *expected_body)
int exec_request(const char *request, const char *path, int expected_code, const char *body, const char *expected_body)
{
int ret = 0;
CURL *curl_handle;
CURLcode res;
char *body = NULL;
char *resp_body = NULL;
long http_result_code;
/* init libcurl */
@@ -155,14 +164,14 @@ int exec_request(const char *request, const char *path, int expected_code, const
{
curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 0);
curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, kput_method);
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, strlen(expected_body));
curl_easy_setopt(curl_handle, CURLOPT_COPYPOSTFIELDS, expected_body);
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, strlen(body));
curl_easy_setopt(curl_handle, CURLOPT_COPYPOSTFIELDS, body);
}
else if (strcmp(request, kpost_method) == 0)
{
curl_easy_setopt(curl_handle, CURLOPT_POST, 0);
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, strlen(expected_body));
curl_easy_setopt(curl_handle, CURLOPT_COPYPOSTFIELDS, expected_body);
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, strlen(body));
curl_easy_setopt(curl_handle, CURLOPT_COPYPOSTFIELDS, body);
}
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 2);
@@ -172,7 +181,7 @@ int exec_request(const char *request, const char *path, int expected_code, const
/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_callback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &body);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, &resp_body);
curl_easy_setopt(curl_handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
/* get it! */
@@ -186,15 +195,15 @@ int exec_request(const char *request, const char *path, int expected_code, const
curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_result_code);
ASSERT_EQUAL_INT(http_result_code, expected_code);
if (body == NULL)
if (resp_body == NULL)
{
res = 1;
}
else
{
res = strcmp(expected_body, body);
res = strcmp(expected_body, resp_body);
}
//printf ("body: %s != expected_body: %s\n", body, expected_body);
// printf ("body: %s != expected_body: %s\n", resp_body, expected_body);
if (res != 0)
{
ret = 2;
@@ -203,7 +212,7 @@ int exec_request(const char *request, const char *path, int expected_code, const
ASSERT_EQUAL_INT(res, 0);
exit:
free(body);
free(resp_body);
/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);
@@ -407,7 +416,7 @@ TEST("Rest - create start access http hook free\t")
ret = restd_server_start(rest_server);
ASSERT_EQUAL_INT(ret, 0);
ret = exec_request(kget_method, "http://localhost:" kserver_port kapi_test_get, 200, ksuccess_get_body);
ret = exec_request(kget_method, "http://localhost:" kserver_port kapi_test_get, 200, ksuccess_get_body, ksuccess_get_body);
ASSERT_EQUAL_INT(ret, 0);
restd_server_free(rest_server);
@@ -415,7 +424,7 @@ TEST("Rest - create start access http hook free\t")
/*--------------------------------------------------------------------------*/
TEST("Rest - create start access rest hook free\t")
TEST("Rest - create start access to all rest hook free\t")
{
int ret;
restd_server_t *rest_server;
@@ -435,27 +444,60 @@ TEST("Rest - create start access rest hook free\t")
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_DELETE, kapi_test_get, my_success_rest_delete_handler, NULL);
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_POST, kapi_test_get, my_success_rest_post_handler, NULL);
ret = restd_server_start(rest_server);
ASSERT_EQUAL_INT(ret, 0);
sleep(1);
//PRINTLN("\n - GET");
ret = exec_request(kget_method, "http://localhost:" kserver_port kapi_test_get, 200, ksuccess_get_body, ksuccess_get_body);
ASSERT_EQUAL_INT(ret, 0);
//PRINTLN("\n - DELETE");
ret = exec_request(kdelete_method, "http://localhost:" kserver_port kapi_test_get, 200, ksuccess_delete_body, ksuccess_delete_body);
ASSERT_EQUAL_INT(ret, 0);
//PRINTLN("\n - PUT");
ret = exec_request(kput_method, "http://localhost:" kserver_port kapi_test_get, 200, ksuccess_put_body, ksuccess_put_body);
ASSERT_EQUAL_INT(ret, 0);
//PRINTLN("\n - POST");
ret = exec_request(kpost_method, "http://localhost:" kserver_port kapi_test_get, 200, ksuccess_post_body, ksuccess_post_body);
ASSERT_EQUAL_INT(ret, 0);
restd_server_free(rest_server);
}
/*--------------------------------------------------------------------------*/
#if 0
TEST("Rest - create start access to rest hook with params free\t")
{
int ret;
restd_server_t *rest_server;
rest_server = restd_server_new();
ASSERT_NOT_NULL(rest_server);
//restd_log_level(RESTD_LOG_DEBUG);
restd_server_set_option(rest_server, "server.port", kserver_port);
restd_server_set_option(rest_server, "server.thread", "1");
//restd_server_register_call_hooks_handler(rest_server, restd_rest_handler);
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, kapi_test_get, my_success_rest_get_handler, NULL);
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_PUT, kapi_test_put_id1, my_success_rest_put_handler, NULL);
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_PUT, kapi_test_put_id2, my_success_rest_put_handler, NULL);
ret = restd_server_start(rest_server);
ASSERT_EQUAL_INT(ret, 0);
sleep(1);
PRINTLN("\n - GET");
ret = exec_request(kget_method, "http://localhost:" kserver_port kapi_test_get, 200, ksuccess_get_body);
ASSERT_EQUAL_INT(ret, 0);
PRINTLN("\n - DELETE");
ret = exec_request(kdelete_method, "http://localhost:" kserver_port kapi_test_get, 200, ksuccess_delete_body);
ASSERT_EQUAL_INT(ret, 0);
PRINTLN("\n - PUT");
ret = exec_request(kput_method, "http://localhost:" kserver_port kapi_test_get, 200, ksuccess_put_body);
ASSERT_EQUAL_INT(ret, 0);
PRINTLN("\n - POST");
ret = exec_request(kpost_method, "http://localhost:" kserver_port kapi_test_get, 200, ksuccess_post_body);
PRINTLN("\n - Param1");
ret = exec_request(kput_method, "http://localhost:" kserver_port kapi_test_put_id1_body, 200, "", ksuccess_put_param1);
ASSERT_EQUAL_INT(ret, 0);
restd_server_free(rest_server);
}
#endif