瀏覽代碼

Add unit test for CGI

bel 9 年之前
父節點
當前提交
54c6b08917
共有 3 個文件被更改,包括 64 次插入5 次删除
  1. 2 2
      include/civetweb.h
  2. 2 3
      src/civetweb.c
  3. 60 0
      test/public_server.c

+ 2 - 2
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.
                 looked up by the file extension.
 */
 */
 CIVETWEB_API void mg_send_mime_file(struct mg_connection *conn,
 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. */
 /* Store body data into a file. */
 CIVETWEB_API long long mg_store_body(struct mg_connection *conn,
 CIVETWEB_API long long mg_store_body(struct mg_connection *conn,

+ 2 - 3
src/civetweb.c

@@ -6490,13 +6490,12 @@ 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
 mg_send_mime_file(struct mg_connection *conn,
 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;
 	struct file file = STRUCT_FILE_INITIALIZER;
 	if (mg_stat(conn, path, &file)) {
 	if (mg_stat(conn, path, &file)) {

+ 60 - 0
test/public_server.c

@@ -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,