mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-11 21:53:57 +00:00
Changed return type of client.
This commit is contained in:
parent
42473b722f
commit
f91cc98b89
12
httplib.h
12
httplib.h
@ -109,7 +109,7 @@ public:
|
|||||||
Client(const char* host, int port);
|
Client(const char* host, int port);
|
||||||
~Client();
|
~Client();
|
||||||
|
|
||||||
int get(const char* url, Response& res);
|
bool get(const char* url, Response& res);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool read_response_line(FILE* fp, Response& res);
|
bool read_response_line(FILE* fp, Response& res);
|
||||||
@ -545,11 +545,11 @@ inline bool Client::read_response_line(FILE* fp, Response& res)
|
|||||||
return true;
|
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_);
|
socket_t sock = create_client_socket(host_.c_str(), port_);
|
||||||
if (sock == -1) {
|
if (sock == -1) {
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE* fp_read;
|
FILE* fp_read;
|
||||||
@ -561,7 +561,7 @@ inline int Client::get(const char* url, Response& res)
|
|||||||
fflush(fp_write);
|
fflush(fp_write);
|
||||||
|
|
||||||
if (!read_response_line(fp_read, res)) {
|
if (!read_response_line(fp_read, res)) {
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
read_headers(fp_read, res.headers);
|
read_headers(fp_read, res.headers);
|
||||||
@ -571,13 +571,13 @@ inline int Client::get(const char* url, Response& res)
|
|||||||
if (len) {
|
if (len) {
|
||||||
res.body.assign(len, 0);
|
res.body.assign(len, 0);
|
||||||
if (!fgets(&res.body[0], res.body.size() + 1, fp_read)) {
|
if (!fgets(&res.body[0], res.body.size() + 1, fp_read)) {
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
close_client_socket(sock);
|
close_client_socket(sock);
|
||||||
|
|
||||||
return 0;
|
return res.status == 200;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace httplib
|
} // namespace httplib
|
||||||
|
68
test/test.cc
68
test/test.cc
@ -45,6 +45,13 @@ TEST(GetHeaderValueTest, DefaultValue)
|
|||||||
ASSERT_STREQ("text/plain", val);
|
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)
|
TEST(GetHeaderValueTest, RegularValue)
|
||||||
{
|
{
|
||||||
MultiMap map = {{"Content-Type","text/html"}, {"Dummy", "Dummy"}};
|
MultiMap map = {{"Content-Type","text/html"}, {"Dummy", "Dummy"}};
|
||||||
@ -52,35 +59,54 @@ TEST(GetHeaderValueTest, RegularValue)
|
|||||||
ASSERT_STREQ("text/html", val);
|
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";
|
const char* host = "localhost";
|
||||||
int port = 1914;
|
int port = 1914;
|
||||||
const char* url = "/hi";
|
const char* url = "/hi";
|
||||||
const char* content = "Hello World!";
|
const char* content = "Hello World!";
|
||||||
|
|
||||||
Server svr(host, port);
|
Server svr;
|
||||||
|
std::future<void> f;
|
||||||
|
};
|
||||||
|
|
||||||
svr.get(url, [&](httplib::Connection& c) {
|
TEST_F(ServerTest, GetMethod200)
|
||||||
c.response.set_content(content);
|
{
|
||||||
});
|
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(); });
|
TEST_F(ServerTest, GetMethod404)
|
||||||
|
{
|
||||||
{
|
Response res;
|
||||||
Response res;
|
bool ret = Client(host, port).get("/invalid", res);
|
||||||
Client(host, port).get(url, res);
|
ASSERT_EQ(false, ret);
|
||||||
EXPECT_EQ(200, res.status);
|
ASSERT_EQ(404, res.status);
|
||||||
EXPECT_EQ(content, res.body);
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
Response res;
|
|
||||||
Client(host, port).get("/invalid", res);
|
|
||||||
EXPECT_EQ(404, res.status);
|
|
||||||
}
|
|
||||||
|
|
||||||
svr.stop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// vim: et ts=4 sw=4 cin cino={1s ff=unix
|
// vim: et ts=4 sw=4 cin cino={1s ff=unix
|
||||||
|
Loading…
x
Reference in New Issue
Block a user