-
Notifications
You must be signed in to change notification settings - Fork 1
/
response_test.cc
142 lines (95 loc) · 4.17 KB
/
response_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "gtest/gtest.h"
#include "response.h"
class ResponseDefaultResponseTest : public ::testing::Test {
protected:
void VerifyContents(Response::ResponseCode status) {
r = Response::DefaultResponse(status);
ASSERT_EQ(status, r.GetStatus());
ASSERT_EQ(DefaultResponse::ToHtml(r.GetStatus()), r.GetBody());
ASSERT_EQ(2, r.GetHeaders().size());
EXPECT_EQ("Content-Length", r.GetHeaders()[0].first);
EXPECT_EQ(std::to_string(r.GetBody().size()), r.GetHeaders()[0].second);
EXPECT_EQ("Content-Type", r.GetHeaders()[1].first);
EXPECT_EQ("text/html", r.GetHeaders()[1].second);
}
Response r;
};
TEST_F(ResponseDefaultResponseTest, OkStatus) {
VerifyContents(Response::ResponseCode::ok);
}
TEST_F(ResponseDefaultResponseTest, CreatedStatus) {
VerifyContents(Response::ResponseCode::created);
}
TEST_F(ResponseDefaultResponseTest, AcceptedStatus) {
VerifyContents(Response::ResponseCode::accepted);
}
TEST_F(ResponseDefaultResponseTest, NoContentStatus) {
VerifyContents(Response::ResponseCode::no_content);
}
TEST_F(ResponseDefaultResponseTest, MovedPermanentlyStatus) {
VerifyContents(Response::ResponseCode::moved_permanently);
}
TEST_F(ResponseDefaultResponseTest, MovedTemporarilyStatus) {
VerifyContents(Response::ResponseCode::moved_temporarily);
}
TEST_F(ResponseDefaultResponseTest, NotModifiedStatus) {
VerifyContents(Response::ResponseCode::not_modified);
}
TEST_F(ResponseDefaultResponseTest, BadRequestStatus) {
VerifyContents(Response::ResponseCode::bad_request);
}
TEST_F(ResponseDefaultResponseTest, UnauthorizedStatus) {
VerifyContents(Response::ResponseCode::unauthorized);
}
TEST_F(ResponseDefaultResponseTest, ForbiddenStatus) {
VerifyContents(Response::ResponseCode::forbidden);
}
TEST_F(ResponseDefaultResponseTest, NotFoundStatus) {
VerifyContents(Response::ResponseCode::not_found);
}
TEST_F(ResponseDefaultResponseTest, InternalServerErrorStatus) {
VerifyContents(Response::ResponseCode::internal_server_error);
}
TEST_F(ResponseDefaultResponseTest, NotImplementederrorStatus) {
VerifyContents(Response::ResponseCode::not_implemented);
}
TEST_F(ResponseDefaultResponseTest, BadGatewayStatus) {
VerifyContents(Response::ResponseCode::bad_gateway);
}
TEST_F(ResponseDefaultResponseTest, ServiceUnavailableStatus) {
VerifyContents(Response::ResponseCode::service_unavailable);
}
TEST(ResponseTest, html) {
Response r = Response::HtmlResponse(
DefaultResponse::ToHtml(Response::ResponseCode::ok));
ASSERT_EQ(Response::ResponseCode::ok, r.GetStatus());
ASSERT_EQ(DefaultResponse::ToHtml(Response::ResponseCode::ok), r.GetBody());
ASSERT_EQ(2, r.GetHeaders().size());
EXPECT_EQ("Content-Length", r.GetHeaders()[0].first);
EXPECT_EQ(std::to_string(r.GetBody().size()), r.GetHeaders()[0].second);
EXPECT_EQ("Content-Type", r.GetHeaders()[1].first);
EXPECT_EQ("text/html", r.GetHeaders()[1].second);
}
TEST(ResponseTest, plain) {
Response r = Response::PlainTextResponse(
DefaultResponse::ToHtml(Response::ResponseCode::ok));
ASSERT_EQ(Response::ResponseCode::ok, r.GetStatus());
ASSERT_EQ(DefaultResponse::ToHtml(Response::ResponseCode::ok), r.GetBody());
ASSERT_EQ(2, r.GetHeaders().size());
EXPECT_EQ("Content-Length", r.GetHeaders()[0].first);
EXPECT_EQ(std::to_string(r.GetBody().size()), r.GetHeaders()[0].second);
EXPECT_EQ("Content-Type", r.GetHeaders()[1].first);
EXPECT_EQ("text/plain", r.GetHeaders()[1].second);
}
TEST(ToStringTest, generaltest) {
Response r = Response::PlainTextResponse(
DefaultResponse::ToHtml(Response::ResponseCode::ok));
ASSERT_EQ(Response::ResponseCode::ok, r.GetStatus());
ASSERT_EQ(DefaultResponse::ToHtml(Response::ResponseCode::ok), r.GetBody());
ASSERT_EQ(2, r.GetHeaders().size());
EXPECT_EQ("Content-Length", r.GetHeaders()[0].first);
EXPECT_EQ(std::to_string(r.GetBody().size()), r.GetHeaders()[0].second);
EXPECT_EQ("Content-Type", r.GetHeaders()[1].first);
EXPECT_EQ("text/plain", r.GetHeaders()[1].second);
EXPECT_EQ("HTTP/1.0 200 OK\r\nContent-Length: 0\r\nContent-Type: text/plain\r\n\r\n", r.ToString());
}