static void test_mg_fetch(void) { static const char *options[] = { "document_root", ".", "listening_ports", "33796", NULL, }; char buf[2000], buf2[2000]; int length; struct mg_context *ctx; struct mg_request_info ri; const char *tmp_file = "temporary_file_name_for_unit_test.txt"; struct mgstat st; FILE *fp; ASSERT((ctx = mg_start(event_handler, NULL, options)) != NULL); // Failed fetch, pass invalid URL ASSERT(mg_fetch(ctx, "localhost", tmp_file, buf, sizeof(buf), &ri) == NULL); ASSERT(mg_fetch(ctx, "localhost:33796", tmp_file, buf, sizeof(buf), &ri) == NULL); ASSERT(mg_fetch(ctx, "http://$$$.$$$", tmp_file, buf, sizeof(buf), &ri) == NULL); // Failed fetch, pass invalid file name ASSERT(mg_fetch(ctx, "http://localhost:33796/data", "/this/file/must/not/exist/ever", buf, sizeof(buf), &ri) == NULL); // Successful fetch ASSERT((fp = mg_fetch(ctx, "http://localhost:33796/data", tmp_file, buf, sizeof(buf), &ri)) != NULL); ASSERT(ri.num_headers == 2); ASSERT(!strcmp(ri.request_method, "HTTP/1.1")); ASSERT(!strcmp(ri.uri, "200")); ASSERT(!strcmp(ri.http_version, "OK")); ASSERT((length = ftell(fp)) == (int) strlen(fetch_data)); fseek(fp, 0, SEEK_SET); ASSERT(fread(buf2, 1, length, fp) == (size_t) length); ASSERT(memcmp(buf2, fetch_data, length) == 0); fclose(fp); // Fetch big file, mongoose.c ASSERT((fp = mg_fetch(ctx, "http://localhost:33796/mongoose.c", tmp_file, buf, sizeof(buf), &ri)) != NULL); ASSERT(mg_stat("mongoose.c", &st) == 0); ASSERT(st.size == ftell(fp)); ASSERT(!strcmp(ri.request_method, "HTTP/1.1")); remove(tmp_file); mg_stop(ctx); }
static void test_mg_fetch(void) { static const char *options[] = { "document_root", ".", "listening_ports", UNUSED_PORT, NULL, }; char buf[2000], buf2[2000]; int n, length; struct mg_context *ctx; struct mg_request_info ri; const char *tmp_file = "temporary_file_name_for_unit_test.txt"; struct file file; FILE *fp; ASSERT((ctx = mg_start(event_handler, NULL, options)) != NULL); // Failed fetch, pass invalid URL ASSERT(mg_fetch(ctx, "localhost", tmp_file, buf, sizeof(buf), &ri) == NULL); ASSERT(mg_fetch(ctx, "localhost:" UNUSED_PORT, tmp_file, buf, sizeof(buf), &ri) == NULL); ASSERT(mg_fetch(ctx, "http://$$$.$$$", tmp_file, buf, sizeof(buf), &ri) == NULL); // Failed fetch, pass invalid file name ASSERT(mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/data", "/this/file/must/not/exist/ever", buf, sizeof(buf), &ri) == NULL); // Successful fetch ASSERT((fp = mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/data", tmp_file, buf, sizeof(buf), &ri)) != NULL); ASSERT(ri.num_headers == 2); ASSERT(!strcmp(ri.request_method, "HTTP/1.1")); ASSERT(!strcmp(ri.uri, "200")); ASSERT(!strcmp(ri.http_version, "OK")); ASSERT((length = ftell(fp)) == (int) strlen(fetch_data)); fseek(fp, 0, SEEK_SET); ASSERT(fread(buf2, 1, length, fp) == (size_t) length); ASSERT(memcmp(buf2, fetch_data, length) == 0); fclose(fp); // Fetch big file, mongoose.c ASSERT((fp = mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/mongoose.c", tmp_file, buf, sizeof(buf), &ri)) != NULL); ASSERT(mg_stat(fc(ctx), "mongoose.c", &file)); ASSERT(file.size == ftell(fp)); ASSERT(!strcmp(ri.request_method, "HTTP/1.1")); // Fetch nonexistent file, /blah ASSERT((fp = mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/boo", tmp_file, buf, sizeof(buf), &ri)) != NULL); ASSERT(!mg_strcasecmp(ri.uri, "404")); fclose(fp); // Fetch existing inmemory file ASSERT((fp = mg_fetch(ctx, "http://localhost:" UNUSED_PORT "/blah", tmp_file, buf, sizeof(buf), &ri)) != NULL); ASSERT(!mg_strcasecmp(ri.uri, "200")); n = ftell(fp); fseek(fp, 0, SEEK_SET); printf("%s %d %d [%.*s]\n", __func__, n, (int) feof(fp), n, buf2); n = fread(buf2, 1, n, fp); printf("%s %d %d [%.*s]\n", __func__, n, (int) feof(fp), n, buf2); ASSERT((size_t) ftell(fp) == (size_t) strlen(inmemory_file_data)); ASSERT(!memcmp(inmemory_file_data, buf2, ftell(fp))); fclose(fp); remove(tmp_file); mg_stop(ctx); }