X Tutup
Skip to content

Commit faa23dd

Browse files
committed
Fix and test resources managing files
1 parent 019083b commit faa23dd

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/http_utils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,11 +548,11 @@ char* load_file (const char *filename)
548548
int size = fp.tellg();
549549
fp.seekg(0, fp.beg);
550550

551-
char* content = new char[size];
551+
char* content = new char[size + 1];
552552
fp.read(content, size);
553553
fp.close();
554554

555-
content[size - 1] = 0;
555+
content[size] = '\0';
556556
return content;
557557
}
558558
else

test/integ/basic.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ class no_response_resource : public http_resource
178178
}
179179
};
180180

181+
class file_response_resource : public http_resource
182+
{
183+
public:
184+
const http_response render_GET(const http_request& req)
185+
{
186+
return http_response_builder("test_content", 200, "text/plain").file_response();
187+
}
188+
};
189+
181190
LT_BEGIN_SUITE(basic_suite)
182191

183192
webserver* ws;
@@ -697,6 +706,24 @@ LT_BEGIN_AUTO_TEST(basic_suite, register_unregister)
697706
}
698707
LT_END_AUTO_TEST(register_unregister)
699708

709+
LT_BEGIN_AUTO_TEST(basic_suite, file_serving_resource)
710+
file_response_resource* resource = new file_response_resource();
711+
ws->register_resource("base", resource);
712+
curl_global_init(CURL_GLOBAL_ALL);
713+
714+
std::string s;
715+
CURL *curl = curl_easy_init();
716+
CURLcode res;
717+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base");
718+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
719+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
720+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
721+
res = curl_easy_perform(curl);
722+
LT_ASSERT_EQ(res, 0);
723+
LT_CHECK_EQ(s, "test content of file\n");
724+
curl_easy_cleanup(curl);
725+
LT_END_AUTO_TEST(file_serving_resource)
726+
700727
LT_BEGIN_AUTO_TEST_ENV()
701728
AUTORUN_TESTS()
702729
LT_END_AUTO_TEST_ENV()

test/unit/http_utils_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ LT_BEGIN_AUTO_TEST(http_utils_suite, ip_representation6_sockaddr)
496496
LT_END_AUTO_TEST(ip_representation6_sockaddr)
497497

498498
LT_BEGIN_AUTO_TEST(http_utils_suite, load_file)
499-
LT_CHECK_EQ(std::string(http::load_file("test_content")), "test content of file");
499+
LT_CHECK_EQ(std::string(http::load_file("test_content")), "test content of file\n");
500500
LT_END_AUTO_TEST(load_file)
501501

502502
LT_BEGIN_AUTO_TEST(http_utils_suite, load_file_invalid)

0 commit comments

Comments
 (0)
X Tutup