1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-17 22:21:06 +03:00

Add unit test for CGI

This commit is contained in:
bel
2016-03-16 23:13:23 +01:00
parent 6506e38923
commit 54c6b08917
3 changed files with 64 additions and 5 deletions

View File

@@ -6490,7 +6490,6 @@ void
mg_send_file(struct mg_connection *conn, const char *path) mg_send_file(struct mg_connection *conn, const char *path)
{ {
mg_send_mime_file(conn, path, NULL); mg_send_mime_file(conn, path, NULL);
} }
void void

View File

@@ -747,6 +747,8 @@ START_TEST(test_request_handlers)
FILE *f; FILE *f;
const char *plain_file_content; const char *plain_file_content;
const char *encoded_file_content; const char *encoded_file_content;
const char *cgi_script_content;
const char *expected_cgi_result;
int opt_idx = 0; int opt_idx = 0;
#if !defined(NO_SSL) #if !defined(NO_SSL)
@@ -964,6 +966,33 @@ START_TEST(test_request_handlers)
fwrite(encoded_file_content, 1, 52, f); fwrite(encoded_file_content, 1, 52, f);
fclose(f); fclose(f);
#ifdef _WIN32
f = fopen("test.cgi", "wb");
cgi_script_content = "#!test.cgi.cmd\r\n";
fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
fclose(f);
f = fopen("test.cgi.cmd", "w");
cgi_script_content = "@echo off\r\n"
"echo Connection: close\r\n"
"echo Content-Type: text/plain\r\n"
"echo.\r\n"
"echo CGI test\r\n"
"\r\n";
fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
fclose(f);
#else
f = fopen("test.cgi", "w");
cgi_script_content = "#!/bin/sh\n"
"echo \"Connection: close\"\n"
"echo \"Content-Type: text/plain\"\n"
"echo \n"
"echo \"CGI test\"\n"
"\n";
fwrite(cgi_script_content, strlen(cgi_script_content), 1, f);
fclose(f);
#endif
expected_cgi_result = "CGI test";
/* Get static data */ /* Get static data */
client_conn = mg_download("localhost", client_conn = mg_download("localhost",
ipv4_port, ipv4_port,
@@ -1034,6 +1063,37 @@ START_TEST(test_request_handlers)
#endif #endif
mg_close_connection(client_conn); mg_close_connection(client_conn);
/* Get CGI generated data */
#if !defined(NO_CGI)
client_conn = mg_download("localhost",
ipv4_port,
0,
ebuf,
sizeof(ebuf),
"%s",
"GET /test.cgi HTTP/1.0\r\n\r\n");
ck_assert(client_conn != NULL);
ri = mg_get_request_info(client_conn);
ck_assert(ri != NULL);
ck_assert_str_eq(ri->uri, "200");
i = mg_read(client_conn, buf, sizeof(buf));
ck_assert_int_ge(i, strlen(expected_cgi_result));
if ((i >= 0) && (i < (int)sizeof(buf))) {
while ((i > 0) && ((buf[i - 1] == '\r') || (buf[i - 1] == '\n'))) {
i--;
}
buf[i] = 0;
}
ck_assert_int_eq(i, strlen(expected_cgi_result));
ck_assert_str_eq(buf, expected_cgi_result);
mg_close_connection(client_conn);
#else
(void)expected_cgi_result;
(void)cgi_script_content;
#endif
/* Get directory listing */ /* Get directory listing */
client_conn = mg_download("localhost", client_conn = mg_download("localhost",
ipv4_port, ipv4_port,