mirror of
https://github.com/lammertb/libhttp.git
synced 2025-08-16 11:21:57 +03:00
Add unit test for CGI
This commit is contained in:
@@ -596,8 +596,8 @@ CIVETWEB_API void mg_send_file(struct mg_connection *conn, const char *path);
|
||||
looked up by the file extension.
|
||||
*/
|
||||
CIVETWEB_API void mg_send_mime_file(struct mg_connection *conn,
|
||||
const char *path,
|
||||
const char *mime_type);
|
||||
const char *path,
|
||||
const char *mime_type);
|
||||
|
||||
/* Store body data into a file. */
|
||||
CIVETWEB_API long long mg_store_body(struct mg_connection *conn,
|
||||
|
@@ -6490,13 +6490,12 @@ void
|
||||
mg_send_file(struct mg_connection *conn, const char *path)
|
||||
{
|
||||
mg_send_mime_file(conn, path, NULL);
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
mg_send_mime_file(struct mg_connection *conn,
|
||||
const char *path,
|
||||
const char *mime_type)
|
||||
const char *path,
|
||||
const char *mime_type)
|
||||
{
|
||||
struct file file = STRUCT_FILE_INITIALIZER;
|
||||
if (mg_stat(conn, path, &file)) {
|
||||
|
@@ -747,6 +747,8 @@ START_TEST(test_request_handlers)
|
||||
FILE *f;
|
||||
const char *plain_file_content;
|
||||
const char *encoded_file_content;
|
||||
const char *cgi_script_content;
|
||||
const char *expected_cgi_result;
|
||||
int opt_idx = 0;
|
||||
|
||||
#if !defined(NO_SSL)
|
||||
@@ -964,6 +966,33 @@ START_TEST(test_request_handlers)
|
||||
fwrite(encoded_file_content, 1, 52, 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 */
|
||||
client_conn = mg_download("localhost",
|
||||
ipv4_port,
|
||||
@@ -1034,6 +1063,37 @@ START_TEST(test_request_handlers)
|
||||
#endif
|
||||
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 */
|
||||
client_conn = mg_download("localhost",
|
||||
ipv4_port,
|
||||
|
Reference in New Issue
Block a user