This commit is contained in:
@@ -34,7 +34,7 @@
|
||||
|
||||
#include <curl/curl.h>
|
||||
|
||||
#include <restd.h>
|
||||
#include <rest/restd.h>
|
||||
|
||||
#include "qunit.h"
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_error_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
int my_error_handler(struct evhttp_request *req, void *arg)
|
||||
{
|
||||
//restd_http_response(conn, 200, "application/json", "{\"status\":\"error\"}", 18);
|
||||
//return RESTD_CLOSE; // Close connection.
|
||||
@@ -47,43 +47,35 @@ int my_error_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_success_http_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
int my_success_http_handler(struct evhttp_request *req, void *arg)
|
||||
{
|
||||
#if 0
|
||||
|
||||
if (event & RESTD_EVENT_READ)
|
||||
{
|
||||
restd_http_response(conn, 200, "application/json", ksuccess_get_body, strlen(ksuccess_get_body));
|
||||
return RESTD_CLOSE; // Close connection.
|
||||
}
|
||||
restd_http_response(req, 200, "application/json", ksuccess_get_body);
|
||||
return RESTD_OK;
|
||||
#endif
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_success_rest_get_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
int my_success_rest_get_handler(struct evhttp_request *req, void *arg)
|
||||
{
|
||||
restd_http_response(conn, 200, "application/json", ksuccess_get_body, strlen(ksuccess_get_body));
|
||||
return RESTD_CLOSE; // Close connection.
|
||||
restd_http_response(req, 200, "application/json", ksuccess_get_body);
|
||||
return RESTD_OK;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_success_rest_delete_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
int my_success_rest_delete_handler(struct evhttp_request *req, void *arg)
|
||||
{
|
||||
restd_http_response(conn, 200, "application/json", ksuccess_delete_body, strlen(ksuccess_delete_body));
|
||||
return RESTD_CLOSE; // Close connection.
|
||||
restd_http_response(req, 200, "application/json", ksuccess_delete_body);
|
||||
return RESTD_OK;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_success_rest_put_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
int my_success_rest_put_handler(struct evhttp_request *req, void *arg)
|
||||
{
|
||||
printf("put event: %d\n", event);
|
||||
restd_http_response(conn, 200, "application/json", ksuccess_put_body, strlen(ksuccess_put_body));
|
||||
return RESTD_CLOSE; // Close connection.
|
||||
restd_http_response(req, 200, "application/json", ksuccess_put_body);
|
||||
return RESTD_OK;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
@@ -154,6 +146,8 @@ int exec_request(const char *request, const char *path, int expected_code, const
|
||||
curl_easy_setopt(curl_handle, CURLOPT_POST, 0);
|
||||
}
|
||||
|
||||
curl_easy_setopt(curl_handle, CURLOPT_TIMEOUT, 2);
|
||||
|
||||
// Debug Only
|
||||
//curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);
|
||||
|
||||
@@ -169,11 +163,18 @@ int exec_request(const char *request, const char *path, int expected_code, const
|
||||
printf("Error: %d\n", res);
|
||||
}
|
||||
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);
|
||||
if (body == NULL)
|
||||
{
|
||||
res = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
res = strcmp(expected_body, body);
|
||||
}
|
||||
//printf ("body: %s != expected_body: %s\n", body, expected_body);
|
||||
if (res != 0)
|
||||
{
|
||||
@@ -193,10 +194,10 @@ exit:
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
bool found_route(restd_server_t *server, const char *method, const char *path)
|
||||
bool found_route(restd_server_t *server, enum evhttp_cmd_type method, const char *path)
|
||||
{
|
||||
qlist_t *hooks = server->hooks;
|
||||
|
||||
@@ -205,7 +206,7 @@ bool found_route(restd_server_t *server, const char *method, const char *path)
|
||||
while (hooks->getnext(hooks, &obj, false) == true)
|
||||
{
|
||||
restd_hook_t *hook = (restd_hook_t *)obj.data;
|
||||
if (hook->method && method && strcmp(hook->method, method) == 0)
|
||||
if (hook->method == method)
|
||||
{
|
||||
if (hook->path && path && strcmp(hook->path, path) == 0)
|
||||
return true;
|
||||
@@ -242,7 +243,7 @@ bool contain(const char *src, const char *dest, int len)
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
bool found_special_route(restd_server_t *server, const char *method, const char *path, int *id)
|
||||
bool found_special_route(restd_server_t *server, enum evhttp_cmd_type method, const char *path, int *id)
|
||||
{
|
||||
qlist_t *hooks = server->hooks;
|
||||
|
||||
@@ -251,7 +252,7 @@ bool found_special_route(restd_server_t *server, const char *method, const char
|
||||
while (hooks->getnext(hooks, &obj, false) == true)
|
||||
{
|
||||
restd_hook_t *hook = (restd_hook_t *)obj.data;
|
||||
if (hook->method && method && strcmp(hook->method, method) == 0)
|
||||
if (hook->method == method)
|
||||
{
|
||||
if (hook->path && path)
|
||||
{
|
||||
@@ -293,7 +294,7 @@ TEST("Rest - create free\t")
|
||||
|
||||
restd_server_set_option(rest_server, "server.port", kserver_port);
|
||||
|
||||
restd_server_register_hook_on_path(rest_server, "POST", "/api/v1/test", my_error_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_POST, "/api/v1/test", my_error_handler, NULL);
|
||||
|
||||
restd_server_free(rest_server);
|
||||
}
|
||||
@@ -309,21 +310,21 @@ TEST("Rest - create access regular route free\t")
|
||||
|
||||
restd_server_set_option(rest_server, "server.port", kserver_port);
|
||||
|
||||
restd_server_register_hook_on_path(rest_server, "POST", "/api/v1/test", my_success_http_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_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"));
|
||||
ASSERT_TRUE(found_route(rest_server, EVHTTP_REQ_POST, "/api/v1/test"));
|
||||
ASSERT_FALSE(found_route(rest_server, EVHTTP_REQ_POST, "/api/v1/notfound"));
|
||||
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test", my_success_http_handler, NULL);
|
||||
ASSERT_TRUE(found_route(rest_server, kget_method, "/api/v1/test"));
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test", my_success_http_handler, NULL);
|
||||
ASSERT_TRUE(found_route(rest_server, kget_method, "/api/v1/test"));
|
||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, "/api/v1/test", my_success_http_handler, NULL);
|
||||
ASSERT_TRUE(found_route(rest_server, EVHTTP_REQ_GET, "/api/v1/test"));
|
||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, "/api/v1/test", my_success_http_handler, NULL);
|
||||
ASSERT_TRUE(found_route(rest_server, EVHTTP_REQ_GET, "/api/v1/test"));
|
||||
|
||||
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, EVHTTP_REQ_PUT, "/api/v1/test", my_success_http_handler, NULL);
|
||||
ASSERT_TRUE(found_route(rest_server, EVHTTP_REQ_PUT, "/api/v1/test"));
|
||||
|
||||
restd_server_register_hook_on_path(rest_server, kdelete_method, "/api/v1/test", my_success_http_handler, NULL);
|
||||
ASSERT_TRUE(found_route(rest_server, kdelete_method, "/api/v1/test"));
|
||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_DELETE, "/api/v1/test", my_success_http_handler, NULL);
|
||||
ASSERT_TRUE(found_route(rest_server, EVHTTP_REQ_DELETE, "/api/v1/test"));
|
||||
|
||||
restd_server_free(rest_server);
|
||||
}
|
||||
@@ -336,13 +337,13 @@ 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, kget_method, "/api/v1/klong/:id", my_success_http_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, kget_method, "/api/v1/donkey", my_success_http_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, kget_method, "/api/v1/test/:id", my_success_http_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, "/api/v1/klong/:id", my_success_http_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, "/api/v1/donkey", my_success_http_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, "/api/v1/test/:id", my_success_http_handler, NULL);
|
||||
|
||||
ASSERT_FALSE(found_special_route(rest_server, kget_method, "/api/v1/notfound/77", &id));
|
||||
ASSERT_FALSE(found_special_route(rest_server, EVHTTP_REQ_GET, "/api/v1/notfound/77", &id));
|
||||
|
||||
ASSERT_TRUE(found_special_route(rest_server, kget_method, "/api/v1/test/77", &id));
|
||||
ASSERT_TRUE(found_special_route(rest_server, EVHTTP_REQ_GET, "/api/v1/test/77", &id));
|
||||
ASSERT_EQUAL_INT(id, 77);
|
||||
|
||||
restd_server_free(rest_server);
|
||||
@@ -361,7 +362,7 @@ TEST("Rest - create start free\t")
|
||||
restd_server_set_option(rest_server, "server.port", kserver_port);
|
||||
restd_server_set_option(rest_server, "server.thread", "1");
|
||||
|
||||
restd_server_register_hook_on_path(rest_server, kget_method, "/api/v1/test/:id", my_success_http_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, "/api/v1/test/:id", my_success_http_handler, NULL);
|
||||
|
||||
ret = restd_server_start(rest_server);
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
@@ -370,7 +371,7 @@ TEST("Rest - create start free\t")
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
#if 0
|
||||
|
||||
TEST("Rest - create start access http hook free\t")
|
||||
{
|
||||
int ret;
|
||||
@@ -382,8 +383,8 @@ TEST("Rest - create start access http hook free\t")
|
||||
restd_server_set_option(rest_server, "server.port", kserver_port);
|
||||
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", kapi_test_get, my_success_http_handler, NULL);
|
||||
// restd_server_register_hook(rest_server, restd_http_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_GET, kapi_test_get, my_success_http_handler, NULL);
|
||||
|
||||
ret = restd_server_start(rest_server);
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
@@ -409,11 +410,11 @@ TEST("Rest - create start access rest hook free\t")
|
||||
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_call_hooks_handler(rest_server, restd_rest_handler);
|
||||
|
||||
restd_server_register_hook_on_path(rest_server, kget_method, kapi_test_get, my_success_rest_get_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, kput_method, kapi_test_get, my_success_rest_put_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, kdelete_method, kapi_test_get, my_success_rest_delete_handler, NULL);
|
||||
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_get, my_success_rest_put_handler, NULL);
|
||||
restd_server_register_hook_on_path(rest_server, EVHTTP_REQ_DELETE, kapi_test_get, my_success_rest_delete_handler, NULL);
|
||||
|
||||
ret = restd_server_start(rest_server);
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
@@ -423,17 +424,17 @@ TEST("Rest - create start access rest hook free\t")
|
||||
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);
|
||||
|
||||
#if 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);
|
||||
|
||||
#endif
|
||||
// TODO POST
|
||||
|
||||
restd_server_free(rest_server);
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user