Changed return type of client.

This commit is contained in:
yhirose 2012-10-02 23:24:23 -04:00
parent 42473b722f
commit f91cc98b89
2 changed files with 53 additions and 27 deletions

View file

@ -109,7 +109,7 @@ public:
Client(const char* host, int port);
~Client();
int get(const char* url, Response& res);
bool get(const char* url, Response& res);
private:
bool read_response_line(FILE* fp, Response& res);
@ -545,11 +545,11 @@ inline bool Client::read_response_line(FILE* fp, Response& res)
return true;
}
inline int Client::get(const char* url, Response& res)
inline bool Client::get(const char* url, Response& res)
{
socket_t sock = create_client_socket(host_.c_str(), port_);
if (sock == -1) {
return -1;
return false;
}
FILE* fp_read;
@ -561,7 +561,7 @@ inline int Client::get(const char* url, Response& res)
fflush(fp_write);
if (!read_response_line(fp_read, res)) {
return -1;
return false;
}
read_headers(fp_read, res.headers);
@ -571,13 +571,13 @@ inline int Client::get(const char* url, Response& res)
if (len) {
res.body.assign(len, 0);
if (!fgets(&res.body[0], res.body.size() + 1, fp_read)) {
return -1;
return false;
}
}
close_client_socket(sock);
return 0;
return res.status == 200;
}
} // namespace httplib

View file

@ -45,6 +45,13 @@ TEST(GetHeaderValueTest, DefaultValue)
ASSERT_STREQ("text/plain", val);
}
TEST(GetHeaderValueTest, DefaultValueInt)
{
MultiMap map = {{"Dummy","Dummy"}};
auto val = get_header_value_int(map, "Content-Length", 100);
ASSERT_EQ(100, val);
}
TEST(GetHeaderValueTest, RegularValue)
{
MultiMap map = {{"Content-Type","text/html"}, {"Dummy", "Dummy"}};
@ -52,35 +59,54 @@ TEST(GetHeaderValueTest, RegularValue)
ASSERT_STREQ("text/html", val);
}
TEST(ServerTest, GetMethod)
TEST(GetHeaderValueTest, RegularValueInt)
{
MultiMap map = {{"Content-Length","100"}, {"Dummy", "Dummy"}};
auto val = get_header_value_int(map, "Content-Length", 0);
ASSERT_EQ(100, val);
}
class ServerTest : public ::testing::Test {
protected:
ServerTest() : svr(host, port) {
}
virtual void SetUp() {
svr.get(url, [&](httplib::Connection& c) {
c.response.set_content(content);
});
f = async([&](){ svr.run(); });
}
virtual void TearDown() {
svr.stop();
f.get();
}
const char* host = "localhost";
int port = 1914;
const char* url = "/hi";
const char* content = "Hello World!";
Server svr(host, port);
Server svr;
std::future<void> f;
};
svr.get(url, [&](httplib::Connection& c) {
c.response.set_content(content);
});
TEST_F(ServerTest, GetMethod200)
{
Response res;
bool ret = Client(host, port).get(url, res);
ASSERT_EQ(true, ret);
ASSERT_EQ(200, res.status);
ASSERT_EQ(content, res.body);
}
auto f = async([&](){ svr.run(); });
{
Response res;
Client(host, port).get(url, res);
EXPECT_EQ(200, res.status);
EXPECT_EQ(content, res.body);
}
{
Response res;
Client(host, port).get("/invalid", res);
EXPECT_EQ(404, res.status);
}
svr.stop();
TEST_F(ServerTest, GetMethod404)
{
Response res;
bool ret = Client(host, port).get("/invalid", res);
ASSERT_EQ(false, ret);
ASSERT_EQ(404, res.status);
}
// vim: et ts=4 sw=4 cin cino={1s ff=unix