head_tests.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include <string>
4 
5 #include <cpr/cpr.h>
6 
7 #include "server.h"
8 
9 using namespace cpr;
10 
11 static Server* server = new Server();
12 auto base = server->GetBaseUrl();
13 
14 TEST(HeadTests, BasicHeadTest) {
15  auto url = Url{base + "/hello.html"};
16  auto response = cpr::Head(url);
17  EXPECT_EQ(std::string{}, response.text);
18  EXPECT_EQ(url, response.url);
19  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
20  EXPECT_EQ(200, response.status_code);
21  EXPECT_EQ(ErrorCode::OK, response.error.code);
22 }
23 
24 TEST(HeadTests, ComplexHeadTest) {
25  auto url = Url{base + "/basic.json"};
26  auto response = cpr::Head(url);
27  EXPECT_EQ(std::string{}, response.text);
28  EXPECT_EQ(url, response.url);
29  EXPECT_EQ(std::string{"application/octet-stream"}, response.header["content-type"]);
30  EXPECT_EQ(200, response.status_code);
31  EXPECT_EQ(ErrorCode::OK, response.error.code);
32 }
33 
34 TEST(HeadTests, ResourceNotFoundHeadTest) {
35  auto url = Url{base + "/error.html"};
36  auto response = cpr::Head(url);
37  EXPECT_EQ(std::string{}, response.text);
38  EXPECT_EQ(url, response.url);
39  EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
40  EXPECT_EQ(404, response.status_code);
41  EXPECT_EQ(ErrorCode::OK, response.error.code);
42 }
43 
44 TEST(HeadTests, BadHostHeadTest) {
45  auto url = Url{"http://bad_host/"};
46  auto response = cpr::Head(url);
47  EXPECT_EQ(std::string{}, response.text);
48  EXPECT_EQ(url, response.url);
49  EXPECT_EQ(0, response.status_code);
50  EXPECT_EQ(ErrorCode::HOST_RESOLUTION_FAILURE, response.error.code);
51 }
52 
53 TEST(HeadTests, CookieHeadTest) {
54  auto url = Url{base + "/basic_cookies.html"};
55  auto cookies = Cookies{{"hello", "world"}, {"my", "another; fake=cookie;"}};
56  auto response = cpr::Head(url, cookies);
57  EXPECT_EQ(std::string{}, response.text);
58  EXPECT_EQ(url, response.url);
59  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
60  EXPECT_EQ(200, response.status_code);
61  EXPECT_EQ(ErrorCode::OK, response.error.code);
62  cookies = response.cookies;
63  EXPECT_EQ(cookies["cookie"], response.cookies["cookie"]);
64  EXPECT_EQ(cookies["icecream"], response.cookies["icecream"]);
65  EXPECT_EQ(cookies["expires"], response.cookies["expires"]);
66 }
67 
68 TEST(HeadTests, ParameterHeadTest) {
69  auto url = Url{base + "/hello.html"};
70  auto parameters = Parameters{{"key", "value"}};
71  auto response = cpr::Head(url, parameters);
72  EXPECT_EQ(std::string{}, response.text);
73  EXPECT_EQ(Url{url + "?key=value"}, response.url);
74  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
75  EXPECT_EQ(200, response.status_code);
76  EXPECT_EQ(ErrorCode::OK, response.error.code);
77 }
78 
79 TEST(HeadTests, AuthenticationSuccessHeadTest) {
80  auto url = Url{base + "/basic_auth.html"};
81  auto response = cpr::Head(url, Authentication{"user", "password"});
82  EXPECT_EQ(std::string{}, response.text);
83  EXPECT_EQ(url, response.url);
84  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
85  EXPECT_EQ(200, response.status_code);
86  EXPECT_EQ(ErrorCode::OK, response.error.code);
87 }
88 
89 TEST(HeadTests, AuthenticationNullFailureHeadTest) {
90  auto url = Url{base + "/basic_auth.html"};
91  auto response = cpr::Head(url);
92  EXPECT_EQ(std::string{}, response.text);
93  EXPECT_EQ(url, response.url);
94  EXPECT_EQ(std::string{}, response.header["content-type"]);
95  EXPECT_EQ(401, response.status_code);
96  EXPECT_EQ(ErrorCode::OK, response.error.code);
97 }
98 
99 TEST(HeadTests, AuthenticationFailureHeadTest) {
100  auto url = Url{base + "/basic_auth.html"};
101  auto response = cpr::Head(url, Authentication{"user", "bad_password"});
102  EXPECT_EQ(std::string{}, response.text);
103  EXPECT_EQ(url, response.url);
104  EXPECT_EQ(std::string{}, response.header["content-type"]);
105  EXPECT_EQ(401, response.status_code);
106  EXPECT_EQ(ErrorCode::OK, response.error.code);
107 }
108 
109 TEST(HeadTests, DISABLED_DigestSuccessHeadTest) { // Is nondeterministic using embedded mongoose
110  auto url = Url{base + "/digest_auth.html"};
111  auto response = cpr::Head(url, Digest{"user", "password"});
112  EXPECT_EQ(std::string{}, response.text);
113  EXPECT_EQ(url, response.url);
114  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
115  EXPECT_EQ(200, response.status_code);
116  EXPECT_EQ(ErrorCode::OK, response.error.code);
117 }
118 
119 TEST(HeadTests, HeaderReflectNoneHeadTest) {
120  auto url = Url{base + "/header_reflect.html"};
121  auto response = cpr::Head(url);
122  EXPECT_EQ(std::string{}, response.text);
123  EXPECT_EQ(url, response.url);
124  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
125  EXPECT_EQ(std::string{}, response.header["hello"]);
126  EXPECT_EQ(200, response.status_code);
127  EXPECT_EQ(ErrorCode::OK, response.error.code);
128 }
129 
130 TEST(HeadTests, HeaderReflectEmptyHeadTest) {
131  auto url = Url{base + "/header_reflect.html"};
132  auto response = cpr::Head(url, Header{});
133  EXPECT_EQ(std::string{}, response.text);
134  EXPECT_EQ(url, response.url);
135  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
136  EXPECT_EQ(std::string{}, response.header["hello"]);
137  EXPECT_EQ(200, response.status_code);
138  EXPECT_EQ(ErrorCode::OK, response.error.code);
139 }
140 
141 TEST(HeadTests, HeaderReflectHeadTest) {
142  auto url = Url{base + "/header_reflect.html"};
143  auto response = cpr::Head(url, Header{{"hello", "world"}});
144  EXPECT_EQ(std::string{}, response.text);
145  EXPECT_EQ(url, response.url);
146  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
147  EXPECT_EQ(std::string{"world"}, response.header["hello"]);
148  EXPECT_EQ(200, response.status_code);
149  EXPECT_EQ(ErrorCode::OK, response.error.code);
150 }
151 
152 TEST(HeadTests, SetEmptyHeaderHeadTest) {
153  auto url = Url{base + "/header_reflect.html"};
154  auto response = cpr::Head(url, Header{{"hello", ""}});
155  EXPECT_EQ(std::string{}, response.text);
156  EXPECT_EQ(url, response.url);
157  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
158  EXPECT_EQ(std::string{}, response.header["hello"]);
159  EXPECT_EQ(200, response.status_code);
160  EXPECT_EQ(ErrorCode::OK, response.error.code);
161 }
162 
163 TEST(HeadTests, RedirectHeadTest) {
164  auto url = Url{base + "/temporary_redirect.html"};
165  auto response = cpr::Head(url, false);
166  EXPECT_EQ(std::string{}, response.text);
167  EXPECT_EQ(url, response.url);
168  EXPECT_EQ(std::string{}, response.header["content-type"]);
169  EXPECT_EQ(302, response.status_code);
170  EXPECT_EQ(ErrorCode::OK, response.error.code);
171 }
172 
173 TEST(HeadTests, ZeroMaxRedirectsHeadTest) {
174  auto url = Url{base + "/hello.html"};
175  auto response = cpr::Head(url, 0L);
176  EXPECT_EQ(std::string{}, response.text);
177  EXPECT_EQ(url, response.url);
178  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
179  EXPECT_EQ(200, response.status_code);
180  EXPECT_EQ(ErrorCode::OK, response.error.code);
181 }
182 
183 TEST(HeadTests, BasicHeadAsyncTest) {
184  auto url = Url{base + "/hello.html"};
185  std::vector<AsyncResponse> responses;
186  for (int i = 0; i < 10; ++i) {
187  responses.emplace_back(cpr::HeadAsync(url));
188  }
189  for (auto& future_response : responses) {
190  auto response = future_response.get();
191  EXPECT_EQ(std::string{}, response.text);
192  EXPECT_EQ(url, response.url);
193  EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
194  EXPECT_EQ(200, response.status_code);
195  EXPECT_EQ(ErrorCode::OK, response.error.code);
196  }
197 }
198 
199 int main(int argc, char** argv) {
200  ::testing::InitGoogleTest(&argc, argv);
202  return RUN_ALL_TESTS();
203 }
Environment * AddGlobalTestEnvironment(Environment *env)
Definition: gtest.h:1342
Definition: server.h:12
std::string Url
Definition: cprtypes.h:14
static Server * server
Definition: head_tests.cpp:11
::std::string string
Definition: gtest-port.h:1129
auto base
Definition: head_tests.cpp:12
AsyncResponse HeadAsync(Ts...ts)
Definition: api.h:115
unsigned int i
Definition: unit1303.c:79
int main(int argc, char **argv)
Definition: head_tests.cpp:199
Url GetBaseUrl()
Definition: server.cpp:625
#define EXPECT_EQ(expected, actual)
Definition: gtest.h:2015
Definition: auth.cpp:3
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2325
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5292
Response Head(Ts &&...ts)
Definition: api.h:107
TEST(HeadTests, BasicHeadTest)
Definition: head_tests.cpp:14


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:15