Refactoring. Removed some client methods.

This commit is contained in:
yhirose 2012-10-05 13:58:56 -04:00
parent 3c8c835489
commit 6062ea592b
5 changed files with 145 additions and 197 deletions

View file

@ -12,9 +12,9 @@ TEST(SplitTest, ParseQueryString)
string s = "key1=val1&key2=val2&key3=val3";
map<string, string> dic;
split(&s[0], &s[s.size()], '&', [&](const char* b, const char* e) {
detail::split(&s[0], &s[s.size()], '&', [&](const char* b, const char* e) {
string key, val;
split(b, e, '=', [&](const char* b, const char* e) {
detail::split(b, e, '=', [&](const char* b, const char* e) {
if (key.empty()) {
key.assign(b, e);
} else {
@ -34,7 +34,7 @@ TEST(ParseQueryTest, ParseQueryString)
string s = "key1=val1&key2=val2&key3=val3";
map<string, string> dic;
parse_query_text(&s[0], &s[s.size()], dic);
detail::parse_query_text(&s[0], &s[s.size()], dic);
EXPECT_EQ("val1", dic["key1"]);
EXPECT_EQ("val2", dic["key2"]);
@ -43,44 +43,44 @@ TEST(ParseQueryTest, ParseQueryString)
TEST(SocketTest, OpenClose)
{
socket_t sock = create_server_socket("localhost", 1914);
socket_t sock = detail::create_server_socket("localhost", 1914);
ASSERT_NE(-1, sock);
auto ret = close_socket(sock);
auto ret = detail::shutdown_and_close_socket(sock);
EXPECT_EQ(0, ret);
}
TEST(GetHeaderValueTest, DefaultValue)
{
MultiMap map = {{"Dummy","Dummy"}};
auto val = get_header_value_text(map, "Content-Type", "text/plain");
auto val = detail::get_header_value_text(map, "Content-Type", "text/plain");
ASSERT_STREQ("text/plain", val);
}
TEST(GetHeaderValueTest, DefaultValueInt)
{
MultiMap map = {{"Dummy","Dummy"}};
auto val = get_header_value_int(map, "Content-Length", 100);
auto val = detail::get_header_value_int(map, "Content-Length", 100);
EXPECT_EQ(100, val);
}
TEST(GetHeaderValueTest, RegularValue)
{
MultiMap map = {{"Content-Type","text/html"}, {"Dummy", "Dummy"}};
auto val = get_header_value_text(map, "Content-Type", "text/plain");
auto val = detail::get_header_value_text(map, "Content-Type", "text/plain");
ASSERT_STREQ("text/html", val);
}
TEST(GetHeaderValueTest, RegularValueInt)
{
MultiMap map = {{"Content-Length","100"}, {"Dummy", "Dummy"}};
auto val = get_header_value_int(map, "Content-Length", 0);
auto val = detail::get_header_value_int(map, "Content-Length", 0);
EXPECT_EQ(100, val);
}
class ServerTest : public ::testing::Test {
protected:
ServerTest() : svr_(host_, port_) {
ServerTest() : svr_(HOST, PORT), cli_(HOST, PORT) {
persons_["john"] = "programmer";
}
@ -117,106 +117,63 @@ protected:
f_.get();
}
const char* host_ = "localhost";
int port_ = 1914;
const char* HOST = "localhost";
const int PORT = 1914;
std::map<std::string, std::string> persons_;
Server svr_;
Client cli_;
std::future<void> f_;
};
TEST_F(ServerTest, GetMethod200)
{
Response res;
bool ret = Client(host_, port_).get("/hi", res);
ASSERT_EQ(true, ret);
EXPECT_EQ(200, res.status);
EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
EXPECT_EQ("Hello World!", res.body);
}
TEST_F(ServerTest, GetMethod302)
{
Response res;
bool ret = Client(host_, port_).get("/", res);
ASSERT_EQ(true, ret);
EXPECT_EQ(302, res.status);
EXPECT_EQ("/hi", res.get_header_value("Location"));
}
TEST_F(ServerTest, GetMethod404)
{
Response res;
bool ret = Client(host_, port_).get("/invalid", res);
ASSERT_EQ(true, ret);
EXPECT_EQ(404, res.status);
}
TEST_F(ServerTest, GetMethodPersonJohn)
{
Response res;
bool ret = Client(host_, port_).get("/person/john", res);
ASSERT_EQ(true, ret);
EXPECT_EQ(200, res.status);
EXPECT_EQ("text/plain", res.get_header_value("Content-Type"));
EXPECT_EQ("programmer", res.body);
}
TEST_F(ServerTest, PostMethod)
{
{
Response res;
bool ret = Client(host_, port_).get("/person/john2", res);
ASSERT_EQ(true, ret);
ASSERT_EQ(404, res.status);
}
{
auto content = "name=john2&note=coder";
auto content_type = "application/x-www-form-urlencoded";
Response res;
bool ret = Client(host_, port_).post("/person", content, content_type, res);
ASSERT_EQ(true, ret);
ASSERT_EQ(200, res.status);
}
{
Response res;
bool ret = Client(host_, port_).get("/person/john2", res);
ASSERT_EQ(true, ret);
ASSERT_EQ(200, res.status);
ASSERT_EQ("text/plain", res.get_header_value("Content-Type"));
ASSERT_EQ("coder", res.body);
}
}
TEST_F(ServerTest, GetMethod200Shared)
{
auto res = Client(host_, port_).get("/hi");
auto res = cli_.get("/hi");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status);
EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
EXPECT_EQ("Hello World!", res->body);
}
TEST_F(ServerTest, PostMethodShared)
TEST_F(ServerTest, GetMethod302)
{
{
auto res = Client(host_, port_).get("/person/john3");
ASSERT_TRUE(res != nullptr);
ASSERT_EQ(404, res->status);
}
{
auto content = "name=john3&note=coder";
auto content_type = "application/x-www-form-urlencoded";
auto res = Client(host_, port_).post("/person", content, content_type);
ASSERT_TRUE(res != nullptr);
ASSERT_EQ(200, res->status);
}
{
auto res = Client(host_, port_).get("/person/john3");
ASSERT_TRUE(res != nullptr);
ASSERT_EQ(200, res->status);
ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
ASSERT_EQ("coder", res->body);
}
auto res = cli_.get("/");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(302, res->status);
EXPECT_EQ("/hi", res->get_header_value("Location"));
}
TEST_F(ServerTest, GetMethod404)
{
auto res = cli_.get("/invalid");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(404, res->status);
}
TEST_F(ServerTest, GetMethodPersonJohn)
{
auto res = cli_.get("/person/john");
ASSERT_TRUE(res != nullptr);
EXPECT_EQ(200, res->status);
EXPECT_EQ("text/plain", res->get_header_value("Content-Type"));
EXPECT_EQ("programmer", res->body);
}
TEST_F(ServerTest, PostMethod)
{
auto res = cli_.get("/person/john3");
ASSERT_TRUE(res != nullptr);
ASSERT_EQ(404, res->status);
res = cli_.post("/person", "name=john3&note=coder", "application/x-www-form-urlencoded");
ASSERT_TRUE(res != nullptr);
ASSERT_EQ(200, res->status);
res = cli_.get("/person/john3");
ASSERT_TRUE(res != nullptr);
ASSERT_EQ(200, res->status);
ASSERT_EQ("text/plain", res->get_header_value("Content-Type"));
ASSERT_EQ("coder", res->body);
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix