From 54c6b089174c704c5d4a09e94d7537ba87ea6cf7 Mon Sep 17 00:00:00 2001 From: bel Date: Wed, 16 Mar 2016 23:13:23 +0100 Subject: [PATCH] Add unit test for CGI --- include/civetweb.h | 4 +-- src/civetweb.c | 5 ++-- test/public_server.c | 60 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) diff --git a/include/civetweb.h b/include/civetweb.h index 58f884f9..f23b381c 100644 --- a/include/civetweb.h +++ b/include/civetweb.h @@ -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, diff --git a/src/civetweb.c b/src/civetweb.c index eeb213d0..78dba281 100644 --- a/src/civetweb.c +++ b/src/civetweb.c @@ -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)) { diff --git a/test/public_server.c b/test/public_server.c index de38d99b..29f1cfa5 100644 --- a/test/public_server.c +++ b/test/public_server.c @@ -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,