restd: delete and put are functional
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2020-01-22 23:35:59 +01:00
parent 09d7cce817
commit 4694f4e3f0
3 changed files with 62 additions and 59 deletions

View File

@@ -681,18 +681,15 @@ static int parse_requestline(restd_http_t *http, char *line)
char *uri = strtok_r(NULL, " ", &saveptr); char *uri = strtok_r(NULL, " ", &saveptr);
char *httpver = strtok_r(NULL, " ", &saveptr); char *httpver = strtok_r(NULL, " ", &saveptr);
char *tmp = strtok_r(NULL, " ", &saveptr); char *tmp = strtok_r(NULL, " ", &saveptr);
printf("=============parse_requestline\n");
if (method == NULL || uri == NULL || httpver == NULL || tmp != NULL) if (method == NULL || uri == NULL || httpver == NULL || tmp != NULL)
{ {
DEBUG("Invalid request line. %s", line); DEBUG("Invalid request line. %s", line);
printf("=============pInvalid request line. %s\n", line);
return RESTD_HTTP_ERROR; return RESTD_HTTP_ERROR;
} }
// Set request method // Set request method
http->request.method = qstrupper(strdup(method)); http->request.method = qstrupper(strdup(method));
printf("============= SAVE METHOD HTTP: %s\n", http->request.method);
// Set HTTP version // Set HTTP version
http->request.httpver = qstrupper(strdup(httpver)); http->request.httpver = qstrupper(strdup(httpver));

View File

@@ -213,14 +213,14 @@ int restd_rest_handler(short event, restd_conn_t *conn)
restd_hook_t *hook = (restd_hook_t *)obj.data; restd_hook_t *hook = (restd_hook_t *)obj.data;
if (hook->cb) if (hook->cb)
{ {
DEBUG("ZOB call_hooks: method: %s - %s \n", hook->method, conn->method); // printf("==== ZOB call_hooks: method: %s - %s \n", hook->method, conn->method);
DEBUG("call_hooks: path: %s - %s\n", hook->path, http->request.path); // printf("==== call_hooks: path: %s - %s\n", hook->path, http->request.path);
DEBUG("HOOK FOUND !!!!\n"); // printf("==== HOOK FOUND !!!!\n");
if ((hook->method == NULL) || (conn->method == NULL) || (strcmp(hook->method, conn->method) != 0)) if ((hook->method == NULL) || (conn->method == NULL) || (strcmp(hook->method, conn->method) != 0))
{ {
DEBUG("Hook found but method failed.\n"); // printf("==== Hook found but method failed -> next.\n");
reason = RESTD_ERROR_METHOD_NOT_ALLOWED; reason = RESTD_ERROR_METHOD_NOT_ALLOWED;
break; continue;
} }
if ((hook->path != NULL) && (http->request.path != NULL)) if ((hook->path != NULL) && (http->request.path != NULL))

View File

@@ -25,16 +25,17 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
#define ksuccesGet_body "{\"status\":\"ok\"}" #define ksuccess_get_body "{\"status\":\"ok\"}"
#define ksuccesDelete_body "{\"status\":\"delete\"}" #define ksuccess_delete_body "{\"status\":\"delete\"}"
#define ksuccess_put_body "{\"status\":\"put\"}"
#define kServerPort "7777" #define kserver_port "7777"
#define kApiTestGet "/api/v1/test_get" #define kapi_test_get "/api/v1/test_get"
#define kPostMethod "POST" #define kpost_method "POST"
#define kGetMethod "GET" #define kget_method "GET"
#define kPutMethod "PUT" #define kput_method "PUT"
#define kDeleteMethod "DELETE" #define kdelete_method "DELETE"
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -50,7 +51,7 @@ int my_success_http_handler(short event, restd_conn_t *conn, void *userdata)
{ {
if (event & RESTD_EVENT_READ) if (event & RESTD_EVENT_READ)
{ {
restd_http_response(conn, 200, "application/json", ksuccesGet_body, strlen(ksuccesGet_body)); restd_http_response(conn, 200, "application/json", ksuccess_get_body, strlen(ksuccess_get_body));
return RESTD_CLOSE; // Close connection. return RESTD_CLOSE; // Close connection.
} }
return RESTD_OK; return RESTD_OK;
@@ -58,9 +59,9 @@ int my_success_http_handler(short event, restd_conn_t *conn, void *userdata)
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
int my_success_rest_handler(short event, restd_conn_t *conn, void *userdata) int my_success_rest_get_handler(short event, restd_conn_t *conn, void *userdata)
{ {
restd_http_response(conn, 200, "application/json", ksuccesGet_body, strlen(ksuccesDelete_body)); restd_http_response(conn, 200, "application/json", ksuccess_get_body, strlen(ksuccess_get_body));
return RESTD_CLOSE; // Close connection. return RESTD_CLOSE; // Close connection.
} }
@@ -68,7 +69,15 @@ int my_success_rest_handler(short event, restd_conn_t *conn, void *userdata)
int my_success_rest_delete_handler(short event, restd_conn_t *conn, void *userdata) 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)); restd_http_response(conn, 200, "application/json", ksuccess_delete_body, strlen(ksuccess_delete_body));
return RESTD_CLOSE; // Close connection.
}
/*--------------------------------------------------------------------------*/
int my_success_rest_put_handler(short event, restd_conn_t *conn, void *userdata)
{
restd_http_response(conn, 200, "application/json", ksuccess_put_body, strlen(ksuccess_put_body));
return RESTD_CLOSE; // Close connection. return RESTD_CLOSE; // Close connection.
} }
@@ -112,7 +121,6 @@ int exec_request(const char *request, const char *path, int expected_code, const
char *body = NULL; char *body = NULL;
long http_result_code; long http_result_code;
printf ("\n\nexec_request: %s\n", request);
/* init libcurl */ /* init libcurl */
curl_global_init(CURL_GLOBAL_ALL); curl_global_init(CURL_GLOBAL_ALL);
@@ -122,29 +130,27 @@ int exec_request(const char *request, const char *path, int expected_code, const
/* specify URL and method */ /* specify URL and method */
curl_easy_setopt(curl_handle, CURLOPT_URL, path); curl_easy_setopt(curl_handle, CURLOPT_URL, path);
if (strcmp(request, kGetMethod) == 0) if (strcmp(request, kget_method) == 0)
{ {
printf("method GET\n");
curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1); curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 1);
} }
else if (strcmp(request, kDeleteMethod) == 0) else if (strcmp(request, kdelete_method) == 0)
{ {
printf("method Delete\n");
curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 0); curl_easy_setopt(curl_handle, CURLOPT_HTTPGET, 0);
curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, kDeleteMethod); curl_easy_setopt(curl_handle, CURLOPT_CUSTOMREQUEST, kdelete_method);
} }
else if (strcmp(request, kPutMethod) == 0) else if (strcmp(request, kput_method) == 0)
{ {
printf("method PUT\n"); curl_easy_setopt(curl_handle, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl_handle, CURLOPT_PUT, 0);
} }
else if (strcmp(request, kPostMethod) == 0) else if (strcmp(request, kpost_method) == 0)
{ {
printf("method POST\n"); printf("method POST\n");
curl_easy_setopt(curl_handle, CURLOPT_POST, 0); curl_easy_setopt(curl_handle, CURLOPT_POST, 0);
} }
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1); // Debug Only
//curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, 1);
/* send all data to this function */ /* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_callback);
@@ -159,6 +165,7 @@ int exec_request(const char *request, const char *path, int expected_code, const
ASSERT_EQUAL_INT(http_result_code, expected_code); ASSERT_EQUAL_INT(http_result_code, expected_code);
res = strcmp(expected_body, body); res = strcmp(expected_body, body);
//printf ("body: %s != expected_body: %s\n", body, expected_body);
ASSERT_EQUAL_INT(res, 0); ASSERT_EQUAL_INT(res, 0);
free(body); free(body);
@@ -269,7 +276,7 @@ TEST("Rest - create free\t")
rest_server = restd_server_new(); rest_server = restd_server_new();
ASSERT_NOT_NULL(rest_server); ASSERT_NOT_NULL(rest_server);
restd_server_set_option(rest_server, "server.port", "7777"); 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, "POST", "/api/v1/test", my_error_handler, NULL);
@@ -285,7 +292,7 @@ TEST("Rest - create access regular route free\t")
rest_server = restd_server_new(); rest_server = restd_server_new();
ASSERT_NOT_NULL(rest_server); ASSERT_NOT_NULL(rest_server);
restd_server_set_option(rest_server, "server.port", "7777"); 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, "POST", "/api/v1/test", my_success_http_handler, NULL);
@@ -293,15 +300,15 @@ TEST("Rest - create access regular route free\t")
ASSERT_FALSE(found_route(rest_server, "POST", "/api/v1/notfound")); ASSERT_FALSE(found_route(rest_server, "POST", "/api/v1/notfound"));
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test", my_success_http_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")); 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); 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")); ASSERT_TRUE(found_route(rest_server, kget_method, "/api/v1/test"));
restd_server_register_hook_on_path(rest_server, "PUT", "/api/v1/test", my_success_http_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")); ASSERT_TRUE(found_route(rest_server, "PUT", "/api/v1/test"));
restd_server_register_hook_on_path(rest_server, "DELETE", "/api/v1/test", my_success_http_handler, NULL); restd_server_register_hook_on_path(rest_server, kdelete_method, "/api/v1/test", my_success_http_handler, NULL);
ASSERT_TRUE(found_route(rest_server, "DELETE", "/api/v1/test")); ASSERT_TRUE(found_route(rest_server, kdelete_method, "/api/v1/test"));
restd_server_free(rest_server); restd_server_free(rest_server);
} }
@@ -315,13 +322,13 @@ TEST("Rest - create access route with param free\t")
rest_server = restd_server_new(); rest_server = restd_server_new();
ASSERT_NOT_NULL(rest_server); ASSERT_NOT_NULL(rest_server);
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, kget_method, "/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, kget_method, "/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); restd_server_register_hook_on_path(rest_server, kget_method, "/api/v1/test/:id", my_success_http_handler, NULL);
ASSERT_FALSE(found_special_route(rest_server, "GET", "/api/v1/notfound/77", &id)); ASSERT_FALSE(found_special_route(rest_server, kget_method, "/api/v1/notfound/77", &id));
ASSERT_TRUE(found_special_route(rest_server, "GET", "/api/v1/test/77", &id)); ASSERT_TRUE(found_special_route(rest_server, kget_method, "/api/v1/test/77", &id));
ASSERT_EQUAL_INT(id, 77); ASSERT_EQUAL_INT(id, 77);
restd_server_free(rest_server); restd_server_free(rest_server);
@@ -337,10 +344,10 @@ TEST("Rest - create start free\t")
rest_server = restd_server_new(); rest_server = restd_server_new();
ASSERT_NOT_NULL(rest_server); ASSERT_NOT_NULL(rest_server);
restd_server_set_option(rest_server, "server.port", "7777"); 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.thread", "1");
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test/:id", my_success_http_handler, NULL); restd_server_register_hook_on_path(rest_server, kget_method, "/api/v1/test/:id", my_success_http_handler, NULL);
ret = restd_server_start(rest_server); ret = restd_server_start(rest_server);
ASSERT_EQUAL_INT(ret, 0); ASSERT_EQUAL_INT(ret, 0);
@@ -358,16 +365,16 @@ TEST("Rest - create start access http hook free\t")
rest_server = restd_server_new(); rest_server = restd_server_new();
ASSERT_NOT_NULL(rest_server); ASSERT_NOT_NULL(rest_server);
restd_server_set_option(rest_server, "server.port", kServerPort); 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.thread", "1");
restd_server_register_hook(rest_server, restd_http_handler, NULL); restd_server_register_hook(rest_server, restd_http_handler, NULL);
restd_server_register_hook_on_path(rest_server, "GET", kApiTestGet, my_success_http_handler, NULL); restd_server_register_hook_on_path(rest_server, "GET", kapi_test_get, my_success_http_handler, NULL);
ret = restd_server_start(rest_server); ret = restd_server_start(rest_server);
ASSERT_EQUAL_INT(ret, 0); ASSERT_EQUAL_INT(ret, 0);
ret = exec_request(kGetMethod, "http://localhost:"kServerPort kApiTestGet, 200, ksuccesGet_body); ret = exec_request(kget_method, "http://localhost:"kserver_port kapi_test_get, 200, ksuccess_get_body);
ASSERT_EQUAL_INT(ret, 0); ASSERT_EQUAL_INT(ret, 0);
restd_server_free(rest_server); restd_server_free(rest_server);
@@ -383,32 +390,31 @@ TEST("Rest - create start access rest hook free\t")
rest_server = restd_server_new(); rest_server = restd_server_new();
ASSERT_NOT_NULL(rest_server); ASSERT_NOT_NULL(rest_server);
restd_log_level(RESTD_LOG_DEBUG); // restd_log_level(RESTD_LOG_DEBUG);
restd_server_set_option(rest_server, "server.port", kServerPort); 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.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, "GET", kApiTestGet, my_success_rest_handler, NULL); 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, "PUT", kApiTestGet, my_success_rest_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, "DELETE", kApiTestGet, my_success_rest_delete_handler, NULL); restd_server_register_hook_on_path(rest_server, kdelete_method, kapi_test_get, my_success_rest_delete_handler, NULL);
ret = restd_server_start(rest_server); ret = restd_server_start(rest_server);
ASSERT_EQUAL_INT(ret, 0); ASSERT_EQUAL_INT(ret, 0);
ret = exec_request(kGetMethod, "http://localhost:"kServerPort kApiTestGet, 200, ksuccesGet_body); ret = exec_request(kget_method, "http://localhost:"kserver_port kapi_test_get, 200, ksuccess_get_body);
ASSERT_EQUAL_INT(ret, 0);
printf("======================\n");
ret = exec_request(kDeleteMethod, "http://localhost:"kServerPort kApiTestGet, 200, ksuccesDelete_body);
ASSERT_EQUAL_INT(ret, 0); ASSERT_EQUAL_INT(ret, 0);
// ret = exec_request(kPutMethod, "http://localhost:"kServerPort kApiTestGet, 200, ksuccess_body); ret = exec_request(kdelete_method, "http://localhost:"kserver_port kapi_test_get, 200, ksuccess_delete_body);
// ASSERT_EQUAL_INT(ret, 0); ASSERT_EQUAL_INT(ret, 0);
ret = exec_request(kput_method, "http://localhost:"kserver_port kapi_test_get, 200, ksuccess_put_body);
ASSERT_EQUAL_INT(ret, 0);
// TODO POST
restd_server_free(rest_server); restd_server_free(rest_server);
} }