This commit is contained in:
@@ -23,11 +23,18 @@
|
||||
*
|
||||
*/
|
||||
|
||||
// This is an independent project of an individual developer. Dear PVS-Studio, please check it.
|
||||
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
#define ksuccesGet_body "{\"status\":\"ok\"}"
|
||||
#define ksuccesDelete_body "{\"status\":\"delete\"}"
|
||||
|
||||
#define ksuccess_body "{\"status\":\"ok\"}"
|
||||
#define kServerPort "7777"
|
||||
#define kApiTestGet "/api/v1/test_get"
|
||||
|
||||
#define kPostMethod "POST"
|
||||
#define kGetMethod "GET"
|
||||
#define kPutMethod "PUT"
|
||||
#define kDeleteMethod "DELETE"
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
@@ -39,11 +46,11 @@ int my_error_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_success_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
int my_success_http_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
{
|
||||
if (event & RESTD_EVENT_READ)
|
||||
{
|
||||
restd_http_response(conn, 200, "application/json", ksuccess_body, 15);
|
||||
restd_http_response(conn, 200, "application/json", ksuccesGet_body, strlen(ksuccesGet_body));
|
||||
return RESTD_CLOSE; // Close connection.
|
||||
}
|
||||
return RESTD_OK;
|
||||
@@ -51,6 +58,22 @@ int my_success_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_success_rest_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
{
|
||||
restd_http_response(conn, 200, "application/json", ksuccesGet_body, strlen(ksuccesDelete_body));
|
||||
return RESTD_CLOSE; // Close connection.
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_success_rest_delete_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
{
|
||||
restd_http_response(conn, 200, "application/json", ksuccesDelete_body, strlen(ksuccesDelete_body));
|
||||
return RESTD_CLOSE; // Close connection.
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream)
|
||||
{
|
||||
size_t body_size = 0;
|
||||
@@ -81,6 +104,76 @@ 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 ret;
|
||||
CURL *curl_handle;
|
||||
CURLcode res;
|
||||
char *body = NULL;
|
||||
long http_result_code;
|
||||
|
||||
printf ("\n\nexec_request: %s\n", request);
|
||||
/* init libcurl */
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
|
||||
/* init the curl session */
|
||||
curl_handle = curl_easy_init();
|
||||
|
||||
/* specify URL and method */
|
||||
curl_easy_setopt(curl_handle, CURLOPT_URL, path);
|
||||
|
||||
if (strcmp(request, kGetMethod) == 0)
|
||||
{
|
||||
printf("method GET\n");
|
||||
curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1);
|
||||
}
|
||||
else if (strcmp(request, kDeleteMethod) == 0)
|
||||
{
|
||||
printf("method Delete\n");
|
||||
curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 0);
|
||||
curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, kDeleteMethod);
|
||||
}
|
||||
else if (strcmp(request, kPutMethod) == 0)
|
||||
{
|
||||
printf("method PUT\n");
|
||||
curl_easy_setopt(curl_handle, CURLOPT_PUT, 0);
|
||||
}
|
||||
else if (strcmp(request, kPostMethod) == 0)
|
||||
{
|
||||
printf("method POST\n");
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POST, 0);
|
||||
}
|
||||
|
||||
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);
|
||||
|
||||
/* 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_IPRESOLVE, CURL_IPRESOLVE_V4);
|
||||
|
||||
/* get it! */
|
||||
res = curl_easy_perform(curl_handle);
|
||||
ASSERT_EQUAL_INT(res, CURLE_OK);
|
||||
|
||||
curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_result_code);
|
||||
ASSERT_EQUAL_INT(http_result_code, expected_code);
|
||||
|
||||
res = strcmp(expected_body, body);
|
||||
ASSERT_EQUAL_INT(res, 0);
|
||||
|
||||
free(body);
|
||||
|
||||
/* cleanup curl stuff */
|
||||
curl_easy_cleanup(curl_handle);
|
||||
|
||||
/* we're done with libcurl, so clean it up */
|
||||
curl_global_cleanup();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
bool found_route(restd_server_t *server, const char *method, const char *path)
|
||||
{
|
||||
qlist_t *hooks = server->hooks;
|
||||
@@ -194,20 +287,20 @@ TEST("Rest - create access regular route free\t")
|
||||
|
||||
restd_server_set_option(rest_server, "server.port", "7777");
|
||||
|
||||
restd_server_register_hook_on_path(rest_server, "POST", "/api/v1/test", my_success_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, "POST", "/api/v1/test", my_success_http_handler, NULL);
|
||||
|
||||
ASSERT_TRUE(found_route(rest_server, "POST", "/api/v1/test"));
|
||||
ASSERT_FALSE(found_route(rest_server, "POST", "/api/v1/notfound"));
|
||||
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test", my_success_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test", my_success_http_handler, NULL);
|
||||
ASSERT_TRUE(found_route(rest_server, "GET", "/api/v1/test"));
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test", my_success_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test", my_success_http_handler, NULL);
|
||||
ASSERT_TRUE(found_route(rest_server, "GET", "/api/v1/test"));
|
||||
|
||||
restd_server_register_hook_on_path(rest_server, "PUT", "/api/v1/test", my_success_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, "PUT", "/api/v1/test", my_success_http_handler, NULL);
|
||||
ASSERT_TRUE(found_route(rest_server, "PUT", "/api/v1/test"));
|
||||
|
||||
restd_server_register_hook_on_path(rest_server, "DELETE", "/api/v1/test", my_success_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, "DELETE", "/api/v1/test", my_success_http_handler, NULL);
|
||||
ASSERT_TRUE(found_route(rest_server, "DELETE", "/api/v1/test"));
|
||||
|
||||
restd_server_free(rest_server);
|
||||
@@ -222,9 +315,9 @@ TEST("Rest - create access route with param free\t")
|
||||
rest_server = restd_server_new();
|
||||
ASSERT_NOT_NULL(rest_server);
|
||||
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/klong/:id", my_success_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/donkey", my_success_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test/:id", my_success_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/klong/:id", my_success_http_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/donkey", my_success_http_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test/:id", my_success_http_handler, NULL);
|
||||
|
||||
ASSERT_FALSE(found_special_route(rest_server, "GET", "/api/v1/notfound/77", &id));
|
||||
|
||||
@@ -247,7 +340,7 @@ TEST("Rest - create start free\t")
|
||||
restd_server_set_option(rest_server, "server.port", "7777");
|
||||
restd_server_set_option(rest_server, "server.thread", "1");
|
||||
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test/:id", my_success_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test/:id", my_success_http_handler, NULL);
|
||||
|
||||
ret = restd_server_start(rest_server);
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
@@ -257,58 +350,65 @@ TEST("Rest - create start free\t")
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
TEST("Rest - create start access route free\t")
|
||||
TEST("Rest - create start access http hook free\t")
|
||||
{
|
||||
restd_server_t *rest_server;
|
||||
int ret;
|
||||
CURL *curl_handle;
|
||||
CURLcode res;
|
||||
char *body = NULL;
|
||||
long http_result_code;
|
||||
restd_server_t *rest_server;
|
||||
|
||||
rest_server = restd_server_new();
|
||||
ASSERT_NOT_NULL(rest_server);
|
||||
|
||||
restd_server_set_option(rest_server, "server.port", "7777");
|
||||
restd_server_set_option(rest_server, "server.port", kServerPort);
|
||||
restd_server_set_option(rest_server, "server.thread", "1");
|
||||
|
||||
restd_server_register_hook(rest_server, restd_http_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test/", my_success_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, "GET", kApiTestGet, my_success_http_handler, NULL);
|
||||
|
||||
ret = restd_server_start(rest_server);
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
|
||||
/* init libcurl */
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
|
||||
/* init the curl session */
|
||||
curl_handle = curl_easy_init();
|
||||
|
||||
/* specify URL to get */
|
||||
curl_easy_setopt(curl_handle, CURLOPT_URL, "http://localhost:7777/api/v1/test/");
|
||||
|
||||
/* 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_IPRESOLVE, CURL_IPRESOLVE_V4);
|
||||
|
||||
/* get it! */
|
||||
res = curl_easy_perform(curl_handle);
|
||||
ASSERT_EQUAL_INT(res, CURLE_OK);
|
||||
|
||||
curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &http_result_code);
|
||||
ASSERT_EQUAL_INT(http_result_code, 200);
|
||||
|
||||
res = strcmp(ksuccess_body, body);
|
||||
ASSERT_EQUAL_INT(res, 0);
|
||||
|
||||
/* cleanup curl stuff */
|
||||
curl_easy_cleanup(curl_handle);
|
||||
|
||||
free(body);
|
||||
|
||||
/* we're done with libcurl, so clean it up */
|
||||
curl_global_cleanup();
|
||||
ret = exec_request(kGetMethod, "http://localhost:"kServerPort kApiTestGet, 200, ksuccesGet_body);
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
|
||||
restd_server_free(rest_server);
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
TEST("Rest - create start access rest hook 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", kServerPort);
|
||||
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, "GET", kApiTestGet, my_success_rest_handler, NULL);
|
||||
//restd_server_register_hook_on_path(rest_server, "PUT", kApiTestGet, my_success_rest_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, "DELETE", kApiTestGet, my_success_rest_delete_handler, NULL);
|
||||
|
||||
ret = restd_server_start(rest_server);
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
|
||||
|
||||
ret = exec_request(kGetMethod, "http://localhost:"kServerPort kApiTestGet, 200, ksuccesGet_body);
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
printf("======================\n");
|
||||
ret = exec_request(kDeleteMethod, "http://localhost:"kServerPort kApiTestGet, 200, ksuccesDelete_body);
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
|
||||
// ret = exec_request(kPutMethod, "http://localhost:"kServerPort kApiTestGet, 200, ksuccess_body);
|
||||
// ASSERT_EQUAL_INT(ret, 0);
|
||||
|
||||
|
||||
|
||||
|
||||
restd_server_free(rest_server);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user