-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathresponse_test.cpp
More file actions
94 lines (77 loc) · 2.4 KB
/
response_test.cpp
File metadata and controls
94 lines (77 loc) · 2.4 KB
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
#include "gtest/gtest.h"
#include "response.hpp"
TEST(ResponseTest, StatusCheck) {
http::server::Response resp_;
resp_.SetStatus(http::server::Response::ok);
EXPECT_EQ(resp_.getResponseCode(), 200);
}
TEST(ResponseTest, HeaderStruct) {
http::server::Response resp_;
resp_.SetStatus(http::server::Response::ok);
resp_.SetBody("Test content");
http::server::header head_;
head_.name = "Content-Length";
head_.value = std::to_string(12);
resp_.AddHeader(head_.name, head_.value);
std::string body = "HTTP/1.1 200 OK\r\n";
body += "Content-Length: 12\r\n";
body += "Test content";
EXPECT_EQ(resp_.ToString(), body);
}
TEST(ResponseTest, HeaderVector) {
http::server::Response resp_;
resp_.SetStatus(http::server::Response::ok);
resp_.SetBody("Test content");
http::server::header head_;
head_.name = "Content-Length";
head_.value = std::to_string(12);
resp_.AddHeader(head_.name, head_.value);
head_.name = "Content-Type";
head_.value = "text/plain";
resp_.AddHeader(head_.name, head_.value);
std::string body = "HTTP/1.1 200 OK\r\n";
body += "Content-Length: 12\r\n";
body += "Content-Type: text/plain\r\n";
body += "Test content";
EXPECT_EQ(resp_.ToString(), body);
}
class ResponseHeaderTest : public ::testing::Test {
protected:
bool AnalyzeHeader(std::string rcontent) {
resp_.SetStatus(http::server::Response::not_found);
resp_.SetBody(rcontent);
buffers = resp_.to_buffers();
std::string body = "HTTP/1.1 404 Not Found\r\n";
body += rcontent;
if (resp_.ToString() == body)
{
return true;
}
return false;
}
void loadHeader() {
resp_.SetStatus(http::server::Response::not_found);
resp_.SetBody("Test content");
head1_.name = "Content-Length";
head1_.value = std::to_string(12);
resp_.AddHeader(head1_.name, head1_.value);
head2_.name = "Content-Type";
head2_.value = "text/plain";
resp_.AddHeader(head2_.name, head2_.value);
buffers = resp_.to_buffers();
}
http::server::Response resp_;
http::server::header head1_, head2_;
std::vector<boost::asio::const_buffer> buffers;
};
TEST_F(ResponseHeaderTest, HeaderParsing) {
loadHeader();
std::string body = "HTTP/1.1 404 Not Found\r\n";
body += "Content-Length: 12\r\n";
body += "Content-Type: text/plain\r\n";
body += "Test content";
EXPECT_EQ(body, resp_.ToString());
}
TEST_F(ResponseHeaderTest, ContentPassedInBuffer) {
EXPECT_TRUE(AnalyzeHeader("Test Content"));
}