diff --git a/test/unit_test.c b/test/unit_test.c index 81357969..e51e7551 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -214,7 +214,8 @@ static char *read_file(const char *path, int *size) { return data; } -static const char *fetch_data = "hello world!\n"; +static long fetch_data_size = 1024*1024; +static char *fetch_data; static const char *inmemory_file_data = "hi there"; static const char *upload_filename = "upload_test.txt"; static const char *upload_filename2 = "upload_test2.txt"; @@ -275,16 +276,28 @@ static int begin_request_handler_cb(struct mg_connection *conn) { int req_len = (int)(ri->content_length); const char * s_req_len = mg_get_header(conn, "Content-Length"); char *data; - int bytes_read; + long to_write, write_now; + int bytes_read, bytes_written; ASSERT( ((req_len == -1) && (s_req_len==NULL)) || ((s_req_len != NULL) && (req_len = atol(s_req_len))) ); - if (!strcmp(ri->uri, "/data")) { + if (!strncmp(ri->uri, "/data/", 6)) { + if (!strcmp(ri->uri+6, "all")) { + to_write = fetch_data_size; + } else { + to_write = atol(ri->uri+6); + } mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Connection: close\r\n" - "Content-Length: %d\r\n" - "Content-Type: text/plain\r\n\r\n" - "%s", strlen(fetch_data), fetch_data); + "Content-Length: %li\r\n" + "Content-Type: text/plain\r\n\r\n", + to_write); + while (to_write>0) { + write_now = to_write > fetch_data_size ? fetch_data_size : to_write; + bytes_written = mg_write(conn, fetch_data, write_now); + ASSERT(bytes_written == write_now); + to_write -= bytes_written; + } close_connection(conn); return 1; } @@ -384,7 +397,7 @@ static void test_mg_download(int use_ssl) { char *p1, *p2, ebuf[100]; const char *h; - int len1, len2, port; + int i, len1, len2, port; struct mg_connection *conn; struct mg_context *ctx; if (use_ssl) port = atoi(HTTPS_PORT); else port = atoi(HTTP_PORT); @@ -414,6 +427,7 @@ static void test_mg_download(int use_ssl) { /* Fetch unit_test.c, should succeed */ ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s", "GET /unit_test.c HTTP/1.0\r\n\r\n")) != NULL); + ASSERT(&conn->request_info == mg_get_request_info(conn)); ASSERT(!strcmp(conn->request_info.uri, "200")); ASSERT((p1 = read_conn(conn, &len1)) != NULL); ASSERT((p2 = read_file("unit_test.c", &len2)) != NULL); @@ -433,13 +447,38 @@ static void test_mg_download(int use_ssl) { /* Fetch in-memory data with no Content-Length, should succeed. */ ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s", - "GET /data HTTP/1.1\r\n\r\n")) != NULL); + "GET /data/all HTTP/1.1\r\n\r\n")) != NULL); + ASSERT(conn->request_info.content_length == fetch_data_size); ASSERT((p1 = read_conn(conn, &len1)) != NULL); - ASSERT(len1 == (int) strlen(fetch_data)); + ASSERT(len1 == (int) fetch_data_size); ASSERT(memcmp(p1, fetch_data, len1) == 0); mg_free(p1); mg_close_connection(conn); + /* Fetch in-memory data with no Content-Length, should succeed. */ + for (i=0; i<=1024*1024*8; i += (i<2 ? 1 : i)) { + ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), + "GET /data/%i HTTP/1.1\r\n\r\n", i)) != NULL); + ASSERT(conn->request_info.content_length == i); + len1 = -1; + p1 = read_conn(conn, &len1); + if (i==0) { + ASSERT(len1 == 0); + ASSERT(p1 == 0); + } else if (i<=fetch_data_size) { + ASSERT(p1 != NULL); + ASSERT(len1 == i); + ASSERT(memcmp(p1, fetch_data, len1) == 0); + } else { + ASSERT(p1 != NULL); + ASSERT(len1 == i); + ASSERT(memcmp(p1, fetch_data, fetch_data_size) == 0); + } + + mg_free(p1); + mg_close_connection(conn); + } + /* Fetch data with Content-Length, should succeed and return the defined length. */ ASSERT((conn = mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "POST /content_length HTTP/1.1\r\nContent-Length: %u\r\n\r\n%s", @@ -448,7 +487,11 @@ static void test_mg_download(int use_ssl) { ASSERT((h != NULL) && (atoi(h)==strlen(test_data))); ASSERT((p1 = read_conn(conn, &len1)) != NULL); ASSERT(len1 == (int) strlen(test_data)); + ASSERT(conn->request_info.content_length == strlen(test_data)); ASSERT(memcmp(p1, test_data, len1) == 0); + ASSERT(strcmp(conn->request_info.request_method, "HTTP/1.1") == 0); + ASSERT(strcmp(conn->request_info.uri, "200") == 0); + ASSERT(strcmp(conn->request_info.http_version, "OK") == 0); mg_free(p1); mg_close_connection(conn); @@ -459,31 +502,30 @@ static void test_mg_download(int use_ssl) { "POST /content_length HTTP/1.1\r\n\r\n%s", test_data)) != NULL); h = mg_get_header(conn, "Content-Length"); ASSERT(h == NULL); + ASSERT(conn->request_info.content_length == -1); ASSERT((p1 = read_conn(conn, &len1)) != NULL); ASSERT(len1 == (int) strlen(test_data)); ASSERT(memcmp(p1, test_data, len1) == 0); mg_free(p1); mg_close_connection(conn); - /* Test SSL redirect, IP address */ - /* TODO(bel): + /* Test non existent */ ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0, ebuf, sizeof(ebuf), "%s", - "GET /foo HTTP/1.1\r\n\r\n")) != NULL); - ASSERT(strcmp(conn->request_info.uri, "302") == 0); - ASSERT(strcmp(mg_get_header(conn, "Location"), - "https://127.0.0.1:" HTTPS_PORT "/foo") == 0); + "GET /redirect HTTP/1.1\r\n\r\n")) != NULL); + ASSERT(strcmp(conn->request_info.request_method, "HTTP/1.1") == 0); + ASSERT(strcmp(conn->request_info.uri, "404") == 0); + ASSERT(strcmp(conn->request_info.http_version, "Not Found") == 0); mg_close_connection(conn); - */ - /* Test SSL redirect, Host: */ + /* Test SSL redirect */ /* TODO(bel): ASSERT((conn = mg_download("localhost", atoi(HTTP_PORT), 0, ebuf, sizeof(ebuf), "%s", - "GET /foo HTTP/1.1\r\nHost: a.b:77\n\n")) != NULL); + "GET /redirect HTTP/1.1\r\n\r\n")) != NULL); ASSERT(strcmp(conn->request_info.uri, "302") == 0); ASSERT(strcmp(mg_get_header(conn, "Location"), - "https://a.b:" HTTPS_PORT "/foo") == 0); + "https://127.0.0.1:" HTTPS_PORT "/redirect") == 0); mg_close_connection(conn); */ @@ -922,6 +964,7 @@ int __cdecl main(void) { char buffer[512]; FILE * f; struct mg_context *ctx; + int i; /* print headline */ printf("Civetweb %s unit test\n", mg_version()); @@ -969,6 +1012,12 @@ int __cdecl main(void) { mg_sleep(1000); mg_stop(ctx); + /* create test data */ + fetch_data = (char *) mg_malloc(fetch_data_size); + for (i=0; i