post_tests.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 
3 #include <array>
4 #include <cstdio>
5 #include <fstream>
6 #include <string>
7 
8 #include <cpr/cpr.h>
9 #include <cpr/multipart.h>
10 
11 #include "server.h"
12 
13 using namespace cpr;
14 
15 static Server* server = new Server();
16 auto base = server->GetBaseUrl();
17 
18 TEST(UrlEncodedPostTests, UrlPostSingleTest) {
19  auto url = Url{base + "/url_post.html"};
20  auto response = cpr::Post(url, Payload{{"x", "5"}});
21  auto expected_text = std::string{"{\n"
22  " \"x\": 5\n"
23  "}"};
24  EXPECT_EQ(expected_text, response.text);
25  EXPECT_EQ(url, response.url);
26  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
27  EXPECT_EQ(201, response.status_code);
28  EXPECT_EQ(ErrorCode::OK, response.error.code);
29 }
30 
31 TEST(UrlEncodedPostTests, UrlPostAddPayloadPair) {
32  auto url = Url{base + "/url_post.html"};
33  auto payload = Payload{{"x", "1"}};
34  payload.AddPair({"y", "2"});
35  auto response = cpr::Post(url, payload);
36  auto expected_text = std::string{"{\n"
37  " \"x\": 1,\n"
38  " \"y\": 2,\n"
39  " \"sum\": 3\n"
40  "}"};
41  EXPECT_EQ(expected_text, response.text);
42  EXPECT_EQ(url, response.url);
43  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
44  EXPECT_EQ(201, response.status_code);
45 }
46 
47 TEST(UrlEncodedPostTests, UrlPostPayloadIteratorTest) {
48  auto url = Url{base + "/url_post.html"};
49  std::vector<Pair> payloadData;
50  payloadData.push_back({"x", "1"});
51  payloadData.push_back({"y", "2"});
52  auto response = cpr::Post(url, Payload(payloadData.begin(), payloadData.end()));
53  auto expected_text = std::string{"{\n"
54  " \"x\": 1,\n"
55  " \"y\": 2,\n"
56  " \"sum\": 3\n"
57  "}"};
58  EXPECT_EQ(expected_text, response.text);
59  EXPECT_EQ(url, response.url);
60  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
61  EXPECT_EQ(201, response.status_code);
62 }
63 
64 TEST(UrlEncodedPostTests, UrlPostEncodeTest) {
65  auto url = Url{base + "/url_post.html"};
66  auto response = cpr::Post(url, Payload{{"x", "hello world!!~"}});
67  auto expected_text = std::string{"{\n"
68  " \"x\": hello world!!~\n"
69  "}"};
70  EXPECT_EQ(expected_text, response.text);
71  EXPECT_EQ(url, response.url);
72  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
73  EXPECT_EQ(201, response.status_code);
74  EXPECT_EQ(ErrorCode::OK, response.error.code);
75 }
76 
77 TEST(UrlEncodedPostTests, UrlPostEncodeNoCopyTest) {
78  auto url = Url{base + "/url_post.html"};
79  auto payload = Payload{{"x", "hello world!!~"}};
80  // payload lives through the lifetime of Post, so it doesn't need to be copied
81  auto response = cpr::Post(url, payload);
82  auto expected_text = std::string{"{\n"
83  " \"x\": hello world!!~\n"
84  "}"};
85  EXPECT_EQ(expected_text, response.text);
86  EXPECT_EQ(url, response.url);
87  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
88  EXPECT_EQ(201, response.status_code);
89  EXPECT_EQ(ErrorCode::OK, response.error.code);
90 }
91 
92 TEST(UrlEncodedPostTests, UrlPostManyTest) {
93  auto url = Url{base + "/url_post.html"};
94  auto response = cpr::Post(url, Payload{{"x", 5}, {"y", 13}});
95  auto expected_text = std::string{"{\n"
96  " \"x\": 5,\n"
97  " \"y\": 13,\n"
98  " \"sum\": 18\n"
99  "}"};
100  EXPECT_EQ(expected_text, response.text);
101  EXPECT_EQ(url, response.url);
102  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
103  EXPECT_EQ(201, response.status_code);
104  EXPECT_EQ(ErrorCode::OK, response.error.code);
105 }
106 
107 TEST(UrlEncodedPostTests, UrlPostBadHostTest) {
108  auto url = Url{"http://bad_host/"};
109  auto response = cpr::Post(url, Payload{{"hello", "world"}});
110  EXPECT_EQ(std::string{}, response.text);
111  EXPECT_EQ(url, response.url);
112  EXPECT_EQ(std::string{}, response.header["content-type"]);
113  EXPECT_EQ(0, response.status_code);
114  EXPECT_EQ(ErrorCode::HOST_RESOLUTION_FAILURE, response.error.code);
115 }
116 
117 TEST(UrlEncodedPostTests, FormPostSingleTest) {
118  auto url = Url{base + "/form_post.html"};
119  auto response = cpr::Post(url, Multipart{{"x", 5}});
120  auto expected_text = std::string{"{\n"
121  " \"x\": 5\n"
122  "}"};
123  EXPECT_EQ(expected_text, response.text);
124  EXPECT_EQ(url, response.url);
125  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
126  EXPECT_EQ(201, response.status_code);
127  EXPECT_EQ(ErrorCode::OK, response.error.code);
128 }
129 
130 TEST(UrlEncodedPostTests, FormPostFileTest) {
131  auto filename = std::string{"test_file"};
132  auto content = std::string{"hello world"};
133  std::ofstream test_file;
134  test_file.open(filename);
135  test_file << content;
136  test_file.close();
137  auto url = Url{base + "/form_post.html"};
138  auto response = cpr::Post(url, Multipart{{"x", File{filename}}});
139  auto expected_text = std::string{"{\n"
140  " \"x\": " + content + "\n"
141  "}"};
142  std::remove(filename.data());
143  EXPECT_EQ(expected_text, response.text);
144  EXPECT_EQ(url, response.url);
145  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
146  EXPECT_EQ(201, response.status_code);
147  EXPECT_EQ(ErrorCode::OK, response.error.code);
148 }
149 
150 TEST(UrlEncodedPostTests, FormPostFileNoCopyTest) {
151  auto filename = std::string{"test_file"};
152  auto content = std::string{"hello world"};
153  std::ofstream test_file;
154  test_file.open(filename);
155  test_file << content;
156  test_file.close();
157  auto url = Url{base + "/form_post.html"};
158  auto multipart = Multipart{{"x", File{filename}}};
159  auto response = cpr::Post(url, multipart);
160  auto expected_text = std::string{"{\n"
161  " \"x\": " + content + "\n"
162  "}"};
163  std::remove(filename.data());
164  EXPECT_EQ(expected_text, response.text);
165  EXPECT_EQ(url, response.url);
166  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
167  EXPECT_EQ(201, response.status_code);
168  EXPECT_EQ(ErrorCode::OK, response.error.code);
169 }
170 
171 TEST(UrlEncodedPostTests, FormPostFileBufferTest) {
172  auto content = std::string{"hello world"};
173  auto url = Url{base + "/form_post.html"};
174  auto response = cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});
175  auto expected_text = std::string{"{\n"
176  " \"x\": " + content + "\n"
177  "}"};
178  EXPECT_EQ(expected_text, response.text);
179  EXPECT_EQ(url, response.url);
180  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
181  EXPECT_EQ(201, response.status_code);
182  EXPECT_EQ(ErrorCode::OK, response.error.code);
183 }
184 
185 TEST(UrlEncodedPostTests, FormPostFileBufferNoCopyTest) {
186  auto content = std::string{"hello world"};
187  auto url = Url{base + "/form_post.html"};
188  auto multipart = Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}};
189  auto response = cpr::Post(url, multipart);
190  auto expected_text = std::string{"{\n"
191  " \"x\": " + content + "\n"
192  "}"};
193  EXPECT_EQ(expected_text, response.text);
194  EXPECT_EQ(url, response.url);
195  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
196  EXPECT_EQ(201, response.status_code);
197  EXPECT_EQ(ErrorCode::OK, response.error.code);
198 }
199 
200 TEST(UrlEncodedPostTests, FormPostFileBufferPointerTest) {
201  const char *content = "hello world";
202  auto url = Url{base + "/form_post.html"};
203  auto response = cpr::Post(url, Multipart{{"x", Buffer{content, 11 + content, "test_file"}}});
204  auto expected_text = std::string{"{\n"
205  " \"x\": " + std::string(content) + "\n"
206  "}"};
207  EXPECT_EQ(expected_text, response.text);
208  EXPECT_EQ(url, response.url);
209  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
210  EXPECT_EQ(201, response.status_code);
211  EXPECT_EQ(ErrorCode::OK, response.error.code);
212 }
213 
214 TEST(UrlEncodedPostTests, FormPostFileBufferArrayTest) {
215  const char content[] = "hello world";
216  auto url = Url{base + "/form_post.html"};
217  // We subtract 1 from std::end() because we don't want to include the terminating null
218  auto response = cpr::Post(
219  url, Multipart{{"x", Buffer{std::begin(content), std::end(content) - 1, "test_file"}}});
220  auto expected_text = std::string{"{\n"
221  " \"x\": " + std::string(content) + "\n"
222  "}"};
223  EXPECT_EQ(expected_text, response.text);
224  EXPECT_EQ(url, response.url);
225  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
226  EXPECT_EQ(201, response.status_code);
227  EXPECT_EQ(ErrorCode::OK, response.error.code);
228 }
229 
230 TEST(UrlEncodedPostTests, FormPostFileBufferVectorTest) {
231  std::vector<unsigned char> content{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
232  auto url = Url{base + "/form_post.html"};
233  auto response =
234  cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});
235  auto expected_text = std::string{"{\n"
236  " \"x\": hello world\n"
237  "}"};
238  EXPECT_EQ(expected_text, response.text);
239  EXPECT_EQ(url, response.url);
240  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
241  EXPECT_EQ(201, response.status_code);
242  EXPECT_EQ(ErrorCode::OK, response.error.code);
243 }
244 
245 TEST(UrlEncodedPostTests, FormPostFileBufferStdArrayTest) {
246  std::array<unsigned char, 11> content{{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}};
247  auto url = Url{base + "/form_post.html"};
248  auto response =
249  cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});
250  auto expected_text = std::string{"{\n"
251  " \"x\": hello world\n"
252  "}"};
253  EXPECT_EQ(expected_text, response.text);
254  EXPECT_EQ(url, response.url);
255  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
256  EXPECT_EQ(201, response.status_code);
257  EXPECT_EQ(ErrorCode::OK, response.error.code);
258 }
259 
260 TEST(UrlEncodedPostTests, FormPostManyTest) {
261  auto url = Url{base + "/form_post.html"};
262  auto response = cpr::Post(url, Multipart{{"x", 5}, {"y", 13}});
263  auto expected_text = std::string{"{\n"
264  " \"x\": 5,\n"
265  " \"y\": 13,\n"
266  " \"sum\": 18\n"
267  "}"};
268  EXPECT_EQ(expected_text, response.text);
269  EXPECT_EQ(url, response.url);
270  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
271  EXPECT_EQ(201, response.status_code);
272  EXPECT_EQ(ErrorCode::OK, response.error.code);
273 }
274 
275 TEST(UrlEncodedPostTests, FormPostManyNoCopyTest) {
276  auto url = Url{base + "/form_post.html"};
277  auto multipart = Multipart{{"x", 5}, {"y", 13}};
278  auto response = cpr::Post(url, multipart);
279  auto expected_text = std::string{"{\n"
280  " \"x\": 5,\n"
281  " \"y\": 13,\n"
282  " \"sum\": 18\n"
283  "}"};
284  EXPECT_EQ(expected_text, response.text);
285  EXPECT_EQ(url, response.url);
286  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
287  EXPECT_EQ(201, response.status_code);
288  EXPECT_EQ(ErrorCode::OK, response.error.code);
289 }
290 
291 TEST(UrlEncodedPostTests, FormPostContentTypeTest) {
292  auto url = Url{base + "/form_post.html"};
293  auto response = cpr::Post(url, Multipart{{"x", 5, "application/number"}});
294  auto expected_text = std::string{"{\n"
295  " \"x\": 5\n"
296  "}"};
297  EXPECT_EQ(expected_text, response.text);
298  EXPECT_EQ(url, response.url);
299  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
300  EXPECT_EQ(201, response.status_code);
301  EXPECT_EQ(ErrorCode::OK, response.error.code);
302 }
303 
304 TEST(UrlEncodedPostTests, FormPostContentTypeLValueTest) {
305  auto url = Url{base + "/form_post.html"};
306  auto multipart = Multipart{{"x", 5, "application/number"}};
307  auto response = cpr::Post(url, multipart);
308  auto expected_text = std::string{"{\n"
309  " \"x\": 5\n"
310  "}"};
311  EXPECT_EQ(expected_text, response.text);
312  EXPECT_EQ(url, response.url);
313  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
314  EXPECT_EQ(201, response.status_code);
315  EXPECT_EQ(ErrorCode::OK, response.error.code);
316 }
317 
318 TEST(UrlEncodedPostTests, UrlPostAsyncSingleTest) {
319  auto url = Url{base + "/url_post.html"};
320  auto payload = Payload{{"x", "5"}};
321  std::vector<AsyncResponse> responses;
322  for (int i = 0; i < 10; ++i) {
323  responses.emplace_back(cpr::PostAsync(url, payload));
324  }
325  for (auto& future_response : responses) {
326  auto response = future_response.get();
327  auto expected_text = std::string{"{\n"
328  " \"x\": 5\n"
329  "}"};
330  EXPECT_EQ(expected_text, response.text);
331  EXPECT_EQ(url, response.url);
332  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
333  EXPECT_EQ(201, response.status_code);
334  EXPECT_EQ(ErrorCode::OK, response.error.code);
335  }
336 }
337 
338 TEST(UrlEncodedPostTests, UrlReflectTest) {
339  auto url = Url{base + "/header_reflect.html"};
340  auto response = cpr::Post(url, Payload{{"x", "5"}});
341  auto expected_text = std::string{"Header reflect POST"};
342  EXPECT_EQ(expected_text, response.text);
343  EXPECT_EQ(url, response.url);
344  EXPECT_EQ(200, response.status_code);
345  EXPECT_EQ(ErrorCode::OK, response.error.code);
346 }
347 
348 TEST(UrlEncodedPostTests, PostWithNoBodyTest) {
349  auto url = Url{base + "/form_post.html"};
350  auto response = cpr::Post(url);
351  auto expected_text = std::string{"{\n"
352  " \"x\": \n"
353  "}"};
354  EXPECT_EQ(expected_text, response.text);
355  EXPECT_EQ(url, response.url);
356  EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
357  EXPECT_EQ(201, response.status_code);
358  EXPECT_EQ(ErrorCode::OK, response.error.code);
359 }
360 
361 int main(int argc, char** argv) {
362  ::testing::InitGoogleTest(&argc, argv);
364  return RUN_ALL_TESTS();
365 }
Environment * AddGlobalTestEnvironment(Environment *env)
Definition: gtest.h:1342
filename
Definition: server.h:12
std::string Url
Definition: cprtypes.h:14
::std::string string
Definition: gtest-port.h:1129
void AddPair(const Pair &pair)
Definition: payload.cpp:12
unsigned int i
Definition: unit1303.c:79
AsyncResponse PostAsync(Ts...ts)
Definition: api.h:69
Url GetBaseUrl()
Definition: server.cpp:625
static Server * server
Definition: post_tests.cpp:15
#define EXPECT_EQ(expected, actual)
Definition: gtest.h:2015
Definition: auth.cpp:3
TEST(UrlEncodedPostTests, UrlPostSingleTest)
Definition: post_tests.cpp:18
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2325
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:5292
int main(int argc, char **argv)
Definition: post_tests.cpp:361
Response Post(Ts &&...ts)
Definition: api.h:61
auto base
Definition: post_tests.cpp:16


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