Cleaned unit test

This commit is contained in:
Sergey Lyubka 2014-03-03 10:54:35 +00:00
parent f521999ea6
commit 570c8213b6

View File

@ -400,91 +400,6 @@ static const char *test_server(void) {
return NULL;
}
// This http client sends two requests using one connection
static int cb2(struct mg_connection *conn) {
const char *file1 = "mongoose.h", *file2 = "mongoose.c", *file_name = file2;
char *buf = (char *) conn->connection_param, *file_data;
int file_len, ret_val = 1;
switch (conn->status_code) {
case MG_CONNECT_SUCCESS:
mg_printf(conn, "GET /%s HTTP/1.1\r\n\r\n", file1);
strcat(buf, "a");
ret_val = 0;
break;
case MG_CONNECT_FAILURE: strcat(buf, "b"); break;
case MG_DOWNLOAD_FAILURE: strcat(buf, "c"); break;
case MG_DOWNLOAD_SUCCESS:
if (!strcmp(buf, "a")) {
// Send second request
mg_printf(conn, "GET /%s HTTP/1.1\r\n\r\n", file2);
file_name = file1;
}
if ((file_data = read_file(file_name, &file_len)) != NULL) {
if ((size_t) file_len == conn->content_len &&
memcmp(file_data, conn->content, file_len) == 0) {
strcat(buf, "d");
} else {
strcat(buf, "f");
}
free(file_data);
}
ret_val = !strcmp(buf, "ad") ? 0 : 1;
break;
default: strcat(buf, "e"); break;
}
return ret_val;
}
static int cb4h(struct mg_connection *conn) {
int result = MG_REQUEST_NOT_PROCESSED;
if (!strcmp(conn->uri, "/x")) {
mg_send_data(conn, ":-)", 3);
result = MG_REQUEST_PROCESSED;
}
return result;
}
static int cb4(struct mg_connection *conn) {
if (conn->status_code == MG_CONNECT_SUCCESS) {
mg_printf(conn, "%s", "POST /x HTTP/1.0\r\nContent-Length: 999999\r\n\r\n");
return MG_CLIENT_CONTINUE;
} else {
sprintf((char *) conn->connection_param, "%.*s",
(int) conn->content_len, conn->content);
}
return MG_CLIENT_CLOSE;
}
static int cb3(struct mg_connection *conn) {
sprintf((char *) conn->connection_param, "%d", conn->status_code);
return MG_CLIENT_CLOSE;
}
static const char *test_mg_connect(void) {
char buf2[40] = "", buf3[40] = "", buf4[40] = "";
struct mg_server *server = mg_create_server(NULL, NULL); // cb4h
struct mg_connection *conn;
ASSERT(mg_set_option(server, "listening_port", LISTENING_ADDR) == NULL);
ASSERT(mg_set_option(server, "document_root", ".") == NULL);
ASSERT(mg_connect(server, "", 0, 0) == NULL);
ASSERT((conn = mg_connect(server, "127.0.0.1", atoi(HTTP_PORT), 0)) != NULL);
ASSERT((conn = mg_connect(server, "127.0.0.1", 29, 0)) != NULL);
ASSERT((conn = mg_connect(server, "127.0.0.1", atoi(HTTP_PORT), 0)) != NULL);
{ int i; for (i = 0; i < 50; i++) mg_poll_server(server, 1); }
printf("buf2: [%s]\n", buf2);
//ASSERT(strcmp(buf2, "add") == 0);
ASSERT(strcmp(buf3, "1") == 0);
ASSERT(strcmp(buf4, "500 Server Error\nPOST size > 999") == 0);
mg_destroy_server(&server);
return NULL;
}
#define DISP "Content-Disposition: form/data; "
#define CRLF "\r\n"
#define BOUNDARY "--xyz"
@ -520,47 +435,34 @@ static const char *test_parse_multipart(void) {
return NULL;
}
static int us1(struct mg_connection *conn) {
static const char *file_name = "mongoose.h";
char *cp = (char *) conn->connection_param;
int file_size;
static int evh2(struct mg_connection *conn, enum mg_event ev) {
char *file_data, *cp = (char *) conn->connection_param;
int file_size, result = MG_FALSE;
if (conn->status_code == MG_CONNECT_SUCCESS) {
mg_printf(conn, "GET /%s HTTP/1.0\r\n\r\n", cp);
return 0;
} else if (conn->status_code == MG_DOWNLOAD_SUCCESS) {
char *file_data = read_file(file_name, &file_size);
sprintf(cp, "%d %s", (size_t) file_size == conn->content_len &&
memcmp(file_data, conn->content, file_size) == 0 ? 1 : 0,
conn->query_string == NULL ? "?" : conn->query_string);
free(file_data);
} else if (conn->status_code == MG_CONNECT_FAILURE) {
sprintf(cp, "%s", "cf");
} else {
sprintf(cp, "%s", "df");
switch (ev) {
case MG_AUTH:
result = MG_TRUE;
break;
case MG_CONNECT:
mg_printf(conn, "GET /%s HTTP/1.0\r\n\r\n", cp);
result = MG_TRUE;
break;
case MG_REQUEST:
break;
case MG_REPLY:
file_data = read_file("unit_test.c", &file_size);
sprintf(cp, "%d %s", (size_t) file_size == conn->content_len &&
memcmp(file_data, conn->content, file_size) == 0 ? 1 : 0,
conn->query_string == NULL ? "?" : conn->query_string);
free(file_data);
break;
default:
break;
}
return 1;
return result;
}
#ifdef MONGOOSE_USE_SSL
static const char *test_ssl(void) {
static const char *ssl_cert = "examples/ssl_cert.pem";
char buf1[100] = "mongoose.h";
struct mg_server *server = mg_create_server(NULL);
ASSERT(mg_set_option(server, "listening_port", LISTENING_ADDR) == NULL);
ASSERT(mg_set_option(server, "document_root", ".") == NULL);
ASSERT(mg_set_option(server, "ssl_certificate", ssl_cert) == NULL);
ASSERT(mg_connect(server, "127.0.0.1", atoi(HTTP_PORT), 1, us1, buf1) == 1);
{ int i; for (i = 0; i < 50; i++) mg_poll_server(server, 1); }
ASSERT(strcmp(buf1, "1 ?") == 0);
mg_destroy_server(&server);
return NULL;
}
#endif
static const char *test_mg_set_option(void) {
struct mg_server *server = mg_create_server(NULL, NULL);
ASSERT(mg_set_option(server, "listening_port", "0") == NULL);
@ -571,17 +473,19 @@ static const char *test_mg_set_option(void) {
static const char *test_rewrites(void) {
char buf1[100] = "xx";
struct mg_server *server = mg_create_server(NULL, NULL);
struct mg_server *server = mg_create_server(NULL, evh2);
struct mg_connection *conn;
const char *port;
ASSERT(mg_set_option(server, "listening_port", "0") == NULL);
ASSERT(mg_set_option(server, "document_root", ".") == NULL);
ASSERT(mg_set_option(server, "url_rewrites", "/xx=./mongoose.h") == NULL);
ASSERT(mg_connect(server, "127.0.0.1",
atoi(mg_get_option(server, "listening_port")), 0) != NULL);
ASSERT(mg_set_option(server, "url_rewrites", "/xx=unit_test.c") == NULL);
ASSERT((port = mg_get_option(server, "listening_port")) != NULL);
ASSERT((conn = mg_connect(server, "127.0.0.1", atoi(port), 0)) != NULL);
conn->connection_param = buf1;
{ int i; for (i = 0; i < 50; i++) mg_poll_server(server, 1); }
//printf("[%s]\n", buf1);
ASSERT(strcmp(buf1, "1 ?") == 0);
mg_destroy_server(&server);
return NULL;
@ -601,11 +505,7 @@ static const char *run_all_tests(void) {
RUN_TEST(test_parse_multipart);
RUN_TEST(test_mg_set_option);
RUN_TEST(test_server);
//RUN_TEST(test_mg_connect);
//RUN_TEST(test_rewrites);
#ifdef MONGOOSE_USE_SSL
RUN_TEST(test_ssl);
#endif
RUN_TEST(test_rewrites);
return NULL;
}