Add 1st Real Rest Test.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -2,7 +2,7 @@ cmake_minimum_required (VERSION 3.0)
|
||||
|
||||
project (librestd)
|
||||
|
||||
add_definitions (-DBUILD_DEBUG)
|
||||
add_definitions (-g -DBUILD_DEBUG)
|
||||
|
||||
include_directories (${CMAKE_SOURCE_DIR}/../libevent/include)
|
||||
include_directories (${CMAKE_BINARY_DIR}/libevent/include)
|
||||
|
||||
@@ -341,6 +341,9 @@ size_t restd_http_response(restd_conn_t *conn, int code, const char *contenttype
|
||||
const void *data, off_t size)
|
||||
{
|
||||
restd_http_t *http = (restd_http_t *)restd_conn_get_extra(conn);
|
||||
// Sanity Check
|
||||
if (http == NULL)
|
||||
return -1;
|
||||
if (http->response.frozen_header)
|
||||
{
|
||||
return 0;
|
||||
|
||||
@@ -536,7 +536,7 @@ static restd_conn_t *conn_new(restd_server_t *server, struct bufferevent *buffer
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
printf("conn_new\n");
|
||||
|
||||
// Create a new connection container.
|
||||
restd_conn_t *conn = NEW_OBJECT(restd_conn_t);
|
||||
if (conn == NULL)
|
||||
|
||||
@@ -32,6 +32,8 @@
|
||||
#include <qlibc/extensions/qhttpclient.h>
|
||||
#include <json.h>
|
||||
|
||||
#include <curl/curl.h>
|
||||
|
||||
#include <restd.h>
|
||||
|
||||
#include "qunit.h"
|
||||
|
||||
@@ -26,6 +26,9 @@
|
||||
// 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 ksuccess_body "{\"status\":\"ok\"}"
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
int my_error_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
@@ -38,8 +41,42 @@ int my_error_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
|
||||
int my_success_handler(short event, restd_conn_t *conn, void *userdata)
|
||||
{
|
||||
restd_http_response(conn, 200, "application/json", "{\"status\":\"ok\"}", 15);
|
||||
return RESTD_CLOSE; // Close connection.
|
||||
if (event & RESTD_EVENT_READ)
|
||||
{
|
||||
restd_http_response(conn, 200, "application/json", ksuccess_body, 15);
|
||||
return RESTD_CLOSE; // Close connection.
|
||||
}
|
||||
return RESTD_OK;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
size_t write_callback(void *ptr, size_t size, size_t nmemb, void *stream)
|
||||
{
|
||||
size_t body_size = 0;
|
||||
size_t curent_size = size * nmemb;
|
||||
|
||||
char *body = *((char **)stream);
|
||||
char *new_body;
|
||||
|
||||
if (body != 0)
|
||||
{
|
||||
body_size = strlen(body);
|
||||
}
|
||||
|
||||
new_body = (char *)realloc(body, curent_size + body_size + 1);
|
||||
if (new_body == NULL)
|
||||
{
|
||||
free(body);
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(new_body + body_size, ptr, curent_size);
|
||||
new_body[curent_size + body_size] = 0;
|
||||
|
||||
*((char **)stream) = new_body;
|
||||
|
||||
return curent_size;
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
@@ -219,41 +256,59 @@ TEST("Rest - create start free\t")
|
||||
}
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
#if 0
|
||||
TEST("Rest - create start make request free\t")
|
||||
|
||||
TEST("Rest - create start access route free\t")
|
||||
{
|
||||
restd_server_t *rest_server;
|
||||
int ret, nFd;
|
||||
qhttpclient_t *httpclient;
|
||||
|
||||
int ret;
|
||||
CURL *curl_handle;
|
||||
CURLcode res;
|
||||
char *body = NULL;
|
||||
long http_result_code;
|
||||
|
||||
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.thread", "1");
|
||||
|
||||
restd_server_register_hook_on_path(rest_server, "GET", "/api/v1/test/:id", my_test_handler, NULL);
|
||||
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);
|
||||
|
||||
ret = restd_server_start(rest_server);
|
||||
ASSERT_EQUAL_INT(ret, 0);
|
||||
|
||||
httpclient = qhttpclient("http://localhost", 7777);
|
||||
nFd = open("/tmp/test.data", O_WRONLY | O_CREAT, 0644);
|
||||
/* init libcurl */
|
||||
curl_global_init(CURL_GLOBAL_ALL);
|
||||
|
||||
off_t nSavesize = 0;
|
||||
int nRescode = 0;
|
||||
bool bRet = false;
|
||||
qlisttbl_t *resheaders = qlisttbl(QLISTTBL_UNIQUE | QLISTTBL_CASEINSENSITIVE);
|
||||
#if 1
|
||||
bRet = httpclient->get(httpclient, "/api/v1/test/1", nFd, &nSavesize,
|
||||
&nRescode, NULL, resheaders, NULL, NULL);
|
||||
#endif
|
||||
printf("%s %d, %d bytes saved\n", (bRet?"Success":"Failed"), nRescode,
|
||||
(int)nSavesize);
|
||||
// close file
|
||||
close(nFd);
|
||||
qlisttbl_free(resheaders);
|
||||
/* 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();
|
||||
|
||||
restd_server_free(rest_server);
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user