Skip to content

Commit f47acbb

Browse files
limhyungseokmrtazz
authored andcommitted
add HEAD method
this closes mrtazz#63
1 parent 01d5679 commit f47acbb

File tree

7 files changed

+57
-0
lines changed

7 files changed

+57
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ RestClient::Response r = RestClient::get("http://url.com")
2727
RestClient::Response r = RestClient::post("http://url.com/post", "text/json", "{\"foo\": \"bla\"}")
2828
RestClient::Response r = RestClient::put("http://url.com/put", "text/json", "{\"foo\": \"bla\"}")
2929
RestClient::Response r = RestClient::del("http://url.com/delete")
30+
RestClient::Response r = RestClient::head("http://url.com")
3031
```
3132

3233
The response is of type [RestClient::Response][restclient_response] and has
@@ -77,6 +78,7 @@ conn->AppendHeader("X-MY-HEADER", "foo")
7778
conn->SetCAInfoFilePath("/etc/custom-ca.crt")
7879

7980
RestClient::Response r = conn->get("/get")
81+
RestClient::Response r = conn->head("/get")
8082
RestClient::Response r = conn->del("/delete")
8183

8284
// set different content header for POST and PUT

include/restclient-cpp/connection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ class Connection {
148148
RestClient::Response put(const std::string& uri,
149149
const std::string& data);
150150
RestClient::Response del(const std::string& uri);
151+
RestClient::Response head(const std::string& uri);
151152

152153
private:
153154
CURL* curlHandle;

include/restclient-cpp/restclient.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Response put(const std::string& url,
5858
const std::string& content_type,
5959
const std::string& data);
6060
Response del(const std::string& url);
61+
Response head(const std::string& url);
6162

6263
} // namespace RestClient
6364

source/connection.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,21 @@ RestClient::Connection::del(const std::string& url) {
358358
return this->performCurlRequest(url);
359359
}
360360

361+
/**
362+
* @brief HTTP HEAD method
363+
*
364+
* @param url to query
365+
*
366+
* @return response struct
367+
*/
368+
RestClient::Response
369+
RestClient::Connection::head(const std::string& url) {
370+
/** we want HTTP HEAD */
371+
const char* http_head = "HEAD";
372+
373+
/** set HTTP HEAD METHOD */
374+
curl_easy_setopt(this->curlHandle, CURLOPT_CUSTOMREQUEST, http_head);
375+
curl_easy_setopt(this->curlHandle, CURLOPT_NOBODY, 1L);
376+
377+
return this->performCurlRequest(url);
378+
}

source/restclient.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,17 @@ RestClient::Response RestClient::del(const std::string& url) {
108108
return ret;
109109
}
110110

111+
/**
112+
* @brief HTTP HEAD method
113+
*
114+
* @param url to query
115+
*
116+
* @return response struct
117+
*/
118+
RestClient::Response RestClient::head(const std::string& url) {
119+
RestClient::Response ret;
120+
RestClient::Connection *conn = new RestClient::Connection("");
121+
ret = conn->head(url);
122+
delete conn;
123+
return ret;
124+
}

test/test_connection.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,18 @@ TEST_F(ConnectionTest, TestGetInfoFromRedirect)
182182
EXPECT_NE(0, info.lastRequest.redirectTime);
183183
EXPECT_NE(0, info.lastRequest.redirectCount);
184184
}
185+
186+
TEST_F(ConnectionTest, TestHeadHeaders)
187+
{
188+
RestClient::HeaderFields headers;
189+
headers["Foo"] = "bar";
190+
headers["Bla"] = "lol";
191+
conn->SetHeaders(headers);
192+
RestClient::Response res = conn->head("/headers");
193+
EXPECT_EQ(200, res.code);
194+
EXPECT_EQ("", res.body);
195+
196+
RestClient::HeaderFields headers_returned = conn->GetHeaders();
197+
EXPECT_EQ("bar", headers_returned["Foo"]);
198+
EXPECT_EQ("lol", headers_returned["Bla"]);
199+
}

test/test_restclient.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,9 @@ TEST_F(RestClientTest, TestRestClientAuth)
164164
res = RestClient::get("http://httpbin.org/basic-auth/foo/bar");
165165
EXPECT_EQ(401, res.code);
166166
}
167+
TEST_F(RestClientTest, TestRestClientHeadCode)
168+
{
169+
RestClient::Response res = RestClient::head("http://httpbin.org/get");
170+
EXPECT_EQ(200, res.code);
171+
EXPECT_EQ("", res.body);
172+
}

0 commit comments

Comments
 (0)