فهرست منبع

Add more unit tests

bel 9 سال پیش
والد
کامیت
86159dce25
3فایلهای تغییر یافته به همراه70 افزوده شده و 13 حذف شده
  1. 8 1
      test/private.c
  2. 1 1
      test/public_func.c
  3. 61 11
      test/public_server.c

+ 8 - 1
test/private.c

@@ -617,6 +617,13 @@ START_TEST(test_parse_date_string)
 	        tm->tm_sec);
 	ck_assert_uint_eq((uintmax_t)parse_date_string(date), (uintmax_t)now);
 
+	gmt_time_string(date, 1, NULL);
+	ck_assert_str_eq(date, "");
+	gmt_time_string(date, 6, NULL);
+	ck_assert_str_eq(date, "Tue, ");
+	gmt_time_string(date, sizeof(date), NULL);
+	ck_assert_str_eq(date, "Thu, 01 Jan 1970 00:00:00 GMT");
+
 	for (i = 2ul; i < 0x8000000ul; i += i / 2) {
 		now = (time_t)i;
 
@@ -692,7 +699,7 @@ make_private_suite(void)
 /* Used to debug test cases without using the check framework */
 
 void
-main(void)
+xmain(void)
 {
 	test_alloc_vprintf(0);
 	test_mg_vsnprintf(0);

+ 1 - 1
test/public_func.c

@@ -438,7 +438,7 @@ START_TEST(test_mg_get_response_code_text)
 	const char *resp;
 
 	for (i = 100; i < 600; i++) {
-		resp = mg_get_response_code_text(i, NULL);
+		resp = mg_get_response_code_text(NULL, i);
 		ck_assert_ptr_ne(resp, NULL);
 		len = strlen(resp);
 		ck_assert_uint_gt(len, 1);

+ 61 - 11
test/public_server.c

@@ -838,7 +838,7 @@ START_TEST(test_request_handlers)
 	short ipv4r_port = 8194;
 #endif
 
-	const char *OPTIONS[8]; /* initializer list here is rejected by CI test */
+	const char *OPTIONS[16];
 	const char *opt;
 	FILE *f;
 	const char *plain_file_content;
@@ -865,6 +865,8 @@ START_TEST(test_request_handlers)
 	memset((void *)OPTIONS, 0, sizeof(OPTIONS));
 	OPTIONS[opt_idx++] = "listening_ports";
 	OPTIONS[opt_idx++] = HTTP_PORT;
+	OPTIONS[opt_idx++] = "authentication_domain";
+	OPTIONS[opt_idx++] = "test.domain";
 #if !defined(NO_FILES)
 	OPTIONS[opt_idx++] = "document_root";
 	OPTIONS[opt_idx++] = ".";
@@ -1156,7 +1158,6 @@ START_TEST(test_request_handlers)
 	mg_close_connection(client_conn);
 #endif
 
-
 	/* Get zipped static data - will not work if Accept-Encoding is not set */
 	client_conn = mg_download("localhost",
 	                          ipv4_port,
@@ -1532,6 +1533,54 @@ START_TEST(test_request_handlers)
 
 	ck_assert_int_eq(ws_client3_data.closed, 1);
 #endif
+
+	/* Get data from callback using mg_connect_client instead of mg_download */
+	memset(ebuf, 0, sizeof(ebuf));
+	client_conn =
+	    mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
+	ck_assert(client_conn != NULL);
+	ck_assert_str_eq(ebuf, "");
+
+	mg_printf(client_conn, "%s", request);
+
+	i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
+	ck_assert_int_ge(i, 0);
+	ck_assert_str_eq(ebuf, "");
+
+	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_eq(i, (int)strlen(expected));
+	buf[i] = 0;
+	ck_assert_str_eq(buf, expected);
+	mg_close_connection(client_conn);
+
+	/* Get data from callback using mg_connect_client and absolute URI */
+	memset(ebuf, 0, sizeof(ebuf));
+	client_conn =
+	    mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf));
+	ck_assert(client_conn != NULL);
+	ck_assert_str_eq(ebuf, "");
+
+	mg_printf(client_conn,
+	          "GET http://test.domain:%d/U7 HTTP/1.0\r\n\r\n",
+	          ipv4_port);
+
+	i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000);
+	ck_assert_int_ge(i, 0);
+	ck_assert_str_eq(ebuf, "");
+
+	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_eq(i, (int)strlen(expected));
+	buf[i] = 0;
+	ck_assert_str_eq(buf, expected);
+	mg_close_connection(client_conn);
 }
 END_TEST
 
@@ -1721,7 +1770,7 @@ START_TEST(test_handle_form)
 	int opt_idx = 0;
 	char ebuf[100];
 	const char *multipart_body;
-	size_t body_len, chunk_len;
+	size_t body_len, body_sent, chunk_len;
 
 	memset((void *)OPTIONS, 0, sizeof(OPTIONS));
 	OPTIONS[opt_idx++] = "listening_ports";
@@ -1937,14 +1986,15 @@ START_TEST(test_handle_form)
 
 	body_len = strlen(multipart_body);
 	chunk_len = 1;
-	while (body_len > 0) {
-		if (chunk_len > body_len) {
-			chunk_len = body_len;
+	body_sent = 0;
+	while (body_len < body_sent) {
+		if (chunk_len > (body_len - body_sent)) {
+			chunk_len = body_len - body_sent;
 		}
 		mg_printf(client_conn, "%x\r\n", chunk_len);
-		mg_write(client_conn, multipart_body + i, chunk_len);
+		mg_write(client_conn, multipart_body + body_len, chunk_len);
 		mg_printf(client_conn, "\r\n");
-		body_len -= chunk_len;
+		body_sent += chunk_len;
 		chunk_len = (chunk_len % 40) + 1;
 	}
 
@@ -2016,16 +2066,16 @@ static int chk_failed = 0;
 
 
 void
-xmain(void)
+main(void)
 {
 	/*
 	    test_the_test_environment(0);
 	    test_threading(0);
 	    test_mg_start_stop_http_server(0);
 	    test_mg_start_stop_https_server(0);
-	    test_request_handlers(0);
-	    test_mg_server_and_client_tls(0);
 	*/
+	test_request_handlers(0);
+	test_mg_server_and_client_tls(0);
 	test_handle_form(0);
 
 	printf("\nok: %i\nfailed: %i\n\n", chk_ok, chk_failed);