forked from mrtazz/restclient-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_restclient_get.cpp
More file actions
113 lines (93 loc) · 2.78 KB
/
test_restclient_get.cpp
File metadata and controls
113 lines (93 loc) · 2.78 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "restclient-cpp/restclient.h"
#include "restclient-cpp/meta.h"
#include <gtest/gtest.h>
#include <json/json.h>
#include <string>
class RestClientGetTest : public ::testing::Test
{
protected:
std::string url;
std::string ctype;
std::string data;
RestClientGetTest()
{
}
virtual ~RestClientGetTest()
{
}
virtual void SetUp()
{
url = "http://httpbin.org/get";
ctype = "";
data = "";
}
virtual void TearDown()
{
}
};
// Tests
// check return code
TEST_F(RestClientGetTest, TestRestClientGETCode)
{
RestClient::response res = RestClient::get(url);
EXPECT_EQ(200, res.code);
}
TEST_F(RestClientGetTest, TestRestClientGETBodyCode)
{
RestClient::response res = RestClient::get(url);
Json::Value root;
std::istringstream str(res.body);
str >> root;
EXPECT_EQ("http://httpbin.org/get", root.get("url", "no url set").asString());
EXPECT_EQ("restclient-cpp/" VERSION, root["headers"].get("User-Agent", "no url set").asString());
}
//check if additional http headers were sent
TEST_F(RestClientGetTest, TestRestClientGETAdditionalHeaders)
{
RestClient::headermap headers;
headers["Accept"] = "text/json";
headers["Accept-Charset"] = "iso8859-2";
headers["Accept-Language"] = "en-US";
headers["User-Agent"] = "restclient-cpp";
RestClient::response res = RestClient::get("http://httpbin.org/headers", headers);
EXPECT_EQ(200, res.code);
Json::Value root;
std::istringstream str(res.body);
str >> root;
const Json::Value r_headers = root["headers"];
for ( RestClient::headermap::const_iterator it = headers.begin(); it != headers.end(); ++it) {
EXPECT_EQ(it->second, r_headers.get(it->first, "Header " + it->first + " not found").asString());
}
}
// check for failure
TEST_F(RestClientGetTest, TestRestClientFailureCode)
{
std::string u = "http://nonexistent";
RestClient::response res = RestClient::get(u);
EXPECT_EQ(-1, res.code);
}
TEST_F(RestClientGetTest, TestRestClientGETHeaders)
{
RestClient::response res = RestClient::get(url);
EXPECT_EQ("keep-alive", res.headers["Connection"]);
}
TEST_F(RestClientGetTest, TestRestClientGETTimeout)
{
std::string u = "http://httpbin.org/delay/10";
RestClient::response res = RestClient::get(u, 5);
EXPECT_EQ(28, res.code);
}
TEST_F(RestClientGetTest, TestRestClientGETAuth)
{
RestClient::setAuth("foo", "bar");
RestClient::response res = RestClient::get("http://httpbin.org/basic-auth/foo/bar");
EXPECT_EQ(200, res.code);
Json::Value root;
std::istringstream str(res.body);
str >> root;
EXPECT_EQ("foo", root.get("user", "no user").asString());
EXPECT_EQ(true, root.get("authenticated", false).asBool());
RestClient::clearAuth();
res = RestClient::get("http://httpbin.org/basic-auth/foo/bar");
EXPECT_EQ(401, res.code);
}