-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatic_file_handler_test.cc
82 lines (69 loc) · 2.5 KB
/
static_file_handler_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "gtest/gtest.h"
#include "static_file_handler.h"
class StaticFileHandlerTest : public ::testing::Test {
protected:
std::string GetName() {
return static_file_handler_.GetName();
}
std::string GetMimeType(const std::string& path) {
return static_file_handler_.GetMimeType(path);
}
bool ReadFile(const std::string& path, std::string& content) {
return static_file_handler_.ReadFile(path, content);
}
std::string HandleRequest(const std::string& raw) {
auto request = Request::Parse(raw);
Response response;
static_file_handler_.HandleRequest(*request, &response);
return response.ToString();
}
std::string ProcessMarkdown(const std::string& content) {
return static_file_handler_.ProcessMarkdown(content);
}
bool IsMarkdown(const std::string& file_path) {
return static_file_handler_.IsMarkdown(file_path);
}
StaticFileHandler static_file_handler_;
};
TEST_F(StaticFileHandlerTest, GetNameCheck) {
EXPECT_EQ("StaticFileHandler", GetName());
}
TEST_F(StaticFileHandlerTest, GetMimeTypeTestJPEG) {
EXPECT_EQ("image/jpeg", GetMimeType("cat.jpeg"));
}
TEST_F(StaticFileHandlerTest, GetMimeTypeTestTXT) {
EXPECT_EQ("text/plain", GetMimeType("dog.txt"));
}
TEST_F(StaticFileHandlerTest, GetMimeTypeTestHTML) {
EXPECT_EQ("text/html", GetMimeType("bird.html"));
}
TEST_F(StaticFileHandlerTest, GetMimeTypeTestDefault) {
EXPECT_EQ("text/plain", GetMimeType("turtle"));
}
TEST_F(StaticFileHandlerTest, GetMimeTypeTestMarkdown) {
EXPECT_EQ("text/html", GetMimeType("lama.md"));
}
TEST_F(StaticFileHandlerTest, IsMarkdownTestTrue) {
EXPECT_TRUE(IsMarkdown("a.md"));
}
TEST_F(StaticFileHandlerTest, IsMarkdownTestFalse) {
EXPECT_FALSE(IsMarkdown("a.txt"));
}
TEST_F(StaticFileHandlerTest, ReadFileSuccessTest) {
std::string file_content;
EXPECT_TRUE(ReadFile("test.txt", file_content));
EXPECT_EQ("HEY!\n\n", file_content);
}
TEST_F(StaticFileHandlerTest, ReadFileErrorTest) {
std::string file_content;
EXPECT_FALSE(ReadFile("fail.txt", file_content));
EXPECT_EQ("", file_content);
}
TEST_F(StaticFileHandlerTest, HandleFailFileRequest) {
EXPECT_EQ("HTTP/1.1 404 Not Found\r\n\r\n", HandleRequest("GET /fail.txt HTTP/1.1"));
}
TEST_F(StaticFileHandlerTest, ProcessMarkdownTest) {
std::string content = "# Title\n## Heading\nBody";
std::string expected = "<h1>Title</h1>\n<h2>Heading</h2>\n<p>Body</p>\n\n";
EXPECT_EQ(expected, ProcessMarkdown(content));
}