Skip to content

Commit c08e137

Browse files
adding tests for upload_handler
1 parent e50bf98 commit c08e137

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

tests/upload_handler_unittest.cpp

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
2+
#include "handler/upload_handler.hpp"
3+
#include "http/http_response.hpp"
4+
#include "stdio.h"
5+
#include "utest/utest.h"
6+
7+
static void write_all(Handler* handler, const std::string& content)
8+
{
9+
size_t written = 0;
10+
11+
while (written < content.size()) {
12+
size_t n = handler->write_data(content.c_str() + written, content.size() - written);
13+
14+
// write_data returns 0 when upload is complete OR on error.
15+
// If upload is not done but 0 is returned, break to avoid infinite loop.
16+
if (n == 0 && handler->needs_input())
17+
break;
18+
19+
written += n;
20+
}
21+
}
22+
23+
UTEST(UploadHandlerTest, needs_input_upload_ongoing)
24+
{
25+
std::string upload_path = "www/example/needs_input_upload_ongoing.txt";
26+
std::string content = "some content to be written to the file";
27+
Handler* handler = new UploadHandler(upload_path, content.size());
28+
ASSERT_TRUE(handler->needs_input());
29+
delete (handler);
30+
remove(upload_path.c_str());
31+
}
32+
33+
UTEST(UploadHandlerTest, has_output_upload_ongoing)
34+
{
35+
std::string upload_path = "www/example/has_output_upload_ongoing.txt";
36+
std::string content = "some content to be written to the file";
37+
Handler* handler = new UploadHandler(upload_path, content.size());
38+
ASSERT_FALSE(handler->has_output());
39+
delete (handler);
40+
remove(upload_path.c_str());
41+
}
42+
43+
UTEST(UploadHandlerTest, needs_input_upload_done)
44+
{
45+
std::string upload_path = "www/example/needs_input_upload_done.txt";
46+
std::string content = "some content to be written to the file";
47+
Handler* handler = new UploadHandler(upload_path, content.size());
48+
write_all(handler, content);
49+
ASSERT_TRUE(handler->has_output());
50+
delete (handler);
51+
remove(upload_path.c_str());
52+
}
53+
54+
UTEST(UploadHandlerTest, has_output_upload_done)
55+
{
56+
std::string upload_path = "www/example/has_output_upload_done.txt";
57+
std::string content = "some content to be written to the file";
58+
Handler* handler = new UploadHandler(upload_path, content.size());
59+
write_all(handler, content);
60+
ASSERT_TRUE(handler->has_output());
61+
delete (handler);
62+
remove(upload_path.c_str());
63+
}
64+
65+
UTEST(UploadHandlerTest, check_content_uploaded)
66+
{
67+
std::string upload_path = "www/example/check_content_uploaded.txt";
68+
std::string content = "some content to be written to the file";
69+
Handler* handler = new UploadHandler(upload_path, content.size());
70+
write_all(handler, content);
71+
ASSERT_TRUE(handler->has_output());
72+
delete (handler);
73+
74+
// validating content
75+
FILE* f = fopen(upload_path.c_str(), "r");
76+
ASSERT_TRUE(f != NULL);
77+
char buffer[1028];
78+
size_t n = fread(buffer, 1, sizeof(buffer), f);
79+
fclose(f);
80+
std::string file_content(buffer, n);
81+
ASSERT_EQ(content.c_str(), file_content.c_str());
82+
remove(upload_path.c_str());
83+
}
84+
85+
UTEST(UploadHandlerTest, return_201)
86+
{
87+
std::string upload_path = "www/example/return_201.txt";
88+
std::string content = "some content to be written to the file";
89+
Handler* handler = new UploadHandler(upload_path, content.size());
90+
ASSERT_FALSE(handler->has_output());
91+
write_all(handler, content);
92+
ASSERT_TRUE(handler->has_output());
93+
char buffer[1028];
94+
size_t n = handler->read_data(buffer, sizeof(buffer));
95+
std::string http_res(buffer, n);
96+
ASSERT_TRUE(http_res.find("201") != std::string::npos);
97+
delete (handler);
98+
remove(upload_path.c_str());
99+
}
100+
101+
UTEST(UploadHandlerTest, return_409)
102+
{
103+
std::string upload_path = "www/example/return_409.txt";
104+
std::string content = "some content to be written to the file";
105+
{
106+
Handler* handler1 = new UploadHandler(upload_path, content.size());
107+
write_all(handler1, content);
108+
ASSERT_TRUE(handler1->has_output());
109+
delete handler1;
110+
}
111+
Handler* handler2 = new UploadHandler(upload_path, content.size());
112+
ASSERT_TRUE(handler2->has_output());
113+
char buffer[1028];
114+
size_t n = handler2->read_data(buffer, sizeof(buffer));
115+
std::string http_res(buffer, n);
116+
ASSERT_TRUE(http_res.find("409") != std::string::npos);
117+
delete (handler2);
118+
remove(upload_path.c_str());
119+
}
120+
121+
UTEST(UploadHandlerTest, return_507)
122+
{
123+
UTEST_SKIP("TODO: Test Disk full without making your machine explode");
124+
}

0 commit comments

Comments
 (0)