Files
domo-iot/src/tests/test_rest.c
NADAL Jean-Baptiste c652d288e0
Some checks failed
continuous-integration/drone/push Build is failing
rest test wip.
2020-01-13 18:31:48 +01:00

191 lines
6.1 KiB
C

/*!
* test_rest.c
*
* Copyright (c) 2015-2020, NADAL Jean-Baptiste. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*
* @Author: NADAL Jean-Baptiste
* @Date: 08/01/2020
*
*/
// 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
/*--------------------------------------------------------------------------*/
int my_test_handler(short event, restd_conn_t *conn, void *userdata)
{
restd_http_response(conn, 200, "application/json", "{\"status\":\"error\"}", 18);
return RESTD_CLOSE; // Close connection.
}
/*--------------------------------------------------------------------------*/
bool found_route(restd_server_t *server, const char *method, const char *path)
{
qlist_t *hooks = server->hooks;
qlist_obj_t obj;
bzero((void *)&obj, sizeof(qlist_obj_t));
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->path && path && strcmp(hook->path, path)==0)
return true;
}
}
return false;
}
/*--------------------------------------------------------------------------*/
bool contain (const char *src, const char *dest, int len)
{
int len_src, len_dest;
int i = 0;
len_src = strlen(src);
if (len_src < len)
return false;
len_dest = strlen(dest);
if (len_dest < len)
return false;
while (i < len)
{
if (src[i] != dest[i])
return false;
i++;
}
return true;
}
/*--------------------------------------------------------------------------*/
bool found_special_route(restd_server_t *server, const char *method, const char *path, int *id)
{
qlist_t *hooks = server->hooks;
qlist_obj_t obj;
bzero((void *)&obj, sizeof(qlist_obj_t));
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->path && path)
{
int i = 0;
int pos = -1;
while (hook->path[i])
{
if (hook->path[i] == ':')
pos = i;
i++;
}
printf("pos: %d\n", pos);
printf("contain: %s\n", contain(hook->path, path, pos)?"TRUE":"FALSE");
if ( pos != -1 && contain(hook->path, path, pos))
{
const char *buffer = &path[i];
printf("buffer: %s\n", buffer);
return true;
}
else if (strcmp(hook->path, path)==0)
return true;
}
return true;
}
}
return false;
}
/*--------------------------------------------------------------------------*/
TEST("Rest - create free\t")
{
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_register_hook_on_path(rest_server, "POST", "/api/v1/test", my_test_handler, NULL);
restd_server_free(rest_server);
}
/*--------------------------------------------------------------------------*/
TEST("Rest - create access regular route free\t")
{
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_register_hook_on_path(rest_server, "POST", "/api/v1/test", my_test_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_test_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_test_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_test_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_test_handler, NULL);
ASSERT_TRUE(found_route(rest_server, "DELETE", "/api/v1/test"));
restd_server_free(rest_server);
}
/*--------------------------------------------------------------------------*/
TEST("Rest - create access regex route free\t")
{
restd_server_t *rest_server;
int id = 0;
rest_server = restd_server_new();
ASSERT_NOT_NULL(rest_server);
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/klong/:id", my_test_handler, NULL);
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/donkey", my_test_handler, NULL);
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test/:id", my_test_handler, NULL);
ASSERT_FALSE(found_special_route(rest_server, "GET", "/api/v1/notfound/77", &id));
ASSERT_TRUE(found_special_route(rest_server, "GET", "/api/v1/test/77", &id));
ASSERT_EQUAL_INT(id, 77);
restd_server_free(rest_server);
}