00001 #include <gtest/gtest.h>
00002
00003 #include <array>
00004 #include <cstdio>
00005 #include <fstream>
00006 #include <string>
00007
00008 #include <cpr/cpr.h>
00009
00010 #include "cpr/multipart.h"
00011 #include "server.h"
00012
00013 using namespace cpr;
00014
00015 static Server* server = new Server();
00016 auto base = server->GetBaseUrl();
00017
00018
00019 TEST(UrlEncodedPostTests, UrlPostSingleTest) {
00020 auto url = Url{base + "/url_post.html"};
00021 auto response = cpr::Post(url, Payload{{"x", "5"}});
00022 auto expected_text = std::string{"{\n"
00023 " \"x\": 5\n"
00024 "}"};
00025 EXPECT_EQ(expected_text, response.text);
00026 EXPECT_EQ(url, response.url);
00027 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00028 EXPECT_EQ(201, response.status_code);
00029 EXPECT_EQ(ErrorCode::OK, response.error.code);
00030 }
00031
00032 TEST(UrlEncodedPostTests, UrlPostAddPayloadPair) {
00033 auto url = Url{base + "/url_post.html"};
00034 auto payload = Payload{{"x", "1"}};
00035 payload.AddPair({"y", "2"});
00036 auto response = cpr::Post(url, Payload(payload));
00037 auto expected_text = std::string{"{\n"
00038 " \"x\": 1,\n"
00039 " \"y\": 2,\n"
00040 " \"sum\": 3\n"
00041 "}"};
00042 EXPECT_EQ(expected_text, response.text);
00043 EXPECT_EQ(url, response.url);
00044 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00045 EXPECT_EQ(201, response.status_code);
00046 }
00047
00048 TEST(UrlEncodedPostTests, UrlPostPayloadIteratorTest) {
00049 auto url = Url{base + "/url_post.html"};
00050 std::vector<Pair> payloadData;
00051 payloadData.push_back({"x", "1"});
00052 payloadData.push_back({"y", "2"});
00053 auto response = cpr::Post(url, Payload(payloadData.begin(), payloadData.end()));
00054 auto expected_text = std::string{"{\n"
00055 " \"x\": 1,\n"
00056 " \"y\": 2,\n"
00057 " \"sum\": 3\n"
00058 "}"};
00059 EXPECT_EQ(expected_text, response.text);
00060 EXPECT_EQ(url, response.url);
00061 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00062 EXPECT_EQ(201, response.status_code);
00063 }
00064
00065 TEST(UrlEncodedPostTests, UrlPostEncodeTest) {
00066 auto url = Url{base + "/url_post.html"};
00067 auto response = cpr::Post(url, Payload{{"x", "hello world!!~"}});
00068 auto expected_text = std::string{"{\n"
00069 " \"x\": hello world!!~\n"
00070 "}"};
00071 EXPECT_EQ(expected_text, response.text);
00072 EXPECT_EQ(url, response.url);
00073 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00074 EXPECT_EQ(201, response.status_code);
00075 EXPECT_EQ(ErrorCode::OK, response.error.code);
00076 }
00077
00078 TEST(UrlEncodedPostTests, UrlPostEncodeNoCopyTest) {
00079 auto url = Url{base + "/url_post.html"};
00080 auto payload = Payload{{"x", "hello world!!~"}};
00081
00082 auto response = cpr::Post(url, payload);
00083 auto expected_text = std::string{"{\n"
00084 " \"x\": hello world!!~\n"
00085 "}"};
00086 EXPECT_EQ(expected_text, response.text);
00087 EXPECT_EQ(url, response.url);
00088 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00089 EXPECT_EQ(201, response.status_code);
00090 EXPECT_EQ(ErrorCode::OK, response.error.code);
00091 }
00092
00093 TEST(UrlEncodedPostTests, UrlPostManyTest) {
00094 auto url = Url{base + "/url_post.html"};
00095 auto response = cpr::Post(url, Payload{{"x", 5}, {"y", 13}});
00096 auto expected_text = std::string{"{\n"
00097 " \"x\": 5,\n"
00098 " \"y\": 13,\n"
00099 " \"sum\": 18\n"
00100 "}"};
00101 EXPECT_EQ(expected_text, response.text);
00102 EXPECT_EQ(url, response.url);
00103 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00104 EXPECT_EQ(201, response.status_code);
00105 EXPECT_EQ(ErrorCode::OK, response.error.code);
00106 }
00107
00108 TEST(UrlEncodedPostTests, UrlPostBadHostTest) {
00109 auto url = Url{"http://bad_host/"};
00110 auto response = cpr::Post(url, Payload{{"hello", "world"}});
00111 EXPECT_EQ(std::string{}, response.text);
00112 EXPECT_EQ(url, response.url);
00113 EXPECT_EQ(std::string{}, response.header["content-type"]);
00114 EXPECT_EQ(0, response.status_code);
00115 EXPECT_EQ(ErrorCode::HOST_RESOLUTION_FAILURE, response.error.code);
00116 }
00117
00118 TEST(UrlEncodedPostTests, FormPostSingleTest) {
00119 auto url = Url{base + "/form_post.html"};
00120 auto response = cpr::Post(url, Multipart{{"x", 5}});
00121 auto expected_text = std::string{"{\n"
00122 " \"x\": 5\n"
00123 "}"};
00124 EXPECT_EQ(expected_text, response.text);
00125 EXPECT_EQ(url, response.url);
00126 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00127 EXPECT_EQ(201, response.status_code);
00128 EXPECT_EQ(ErrorCode::OK, response.error.code);
00129 }
00130
00131 TEST(UrlEncodedPostTests, FormPostFileTest) {
00132 auto filename = std::string{"test_file"};
00133 auto content = std::string{"hello world"};
00134 std::ofstream test_file;
00135 test_file.open(filename);
00136 test_file << content;
00137 test_file.close();
00138 auto url = Url{base + "/form_post.html"};
00139 auto response = cpr::Post(url, Multipart{{"x", File{filename}}});
00140 auto expected_text = std::string{"{\n"
00141 " \"x\": " + content + "\n"
00142 "}"};
00143 std::remove(filename.data());
00144 EXPECT_EQ(expected_text, response.text);
00145 EXPECT_EQ(url, response.url);
00146 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00147 EXPECT_EQ(201, response.status_code);
00148 EXPECT_EQ(ErrorCode::OK, response.error.code);
00149 }
00150
00151 TEST(UrlEncodedPostTests, FormPostFileNoCopyTest) {
00152 auto filename = std::string{"test_file"};
00153 auto content = std::string{"hello world"};
00154 std::ofstream test_file;
00155 test_file.open(filename);
00156 test_file << content;
00157 test_file.close();
00158 auto url = Url{base + "/form_post.html"};
00159 auto multipart = Multipart{{"x", File{filename}}};
00160 auto response = cpr::Post(url, multipart);
00161 auto expected_text = std::string{"{\n"
00162 " \"x\": " + content + "\n"
00163 "}"};
00164 std::remove(filename.data());
00165 EXPECT_EQ(expected_text, response.text);
00166 EXPECT_EQ(url, response.url);
00167 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00168 EXPECT_EQ(201, response.status_code);
00169 EXPECT_EQ(ErrorCode::OK, response.error.code);
00170 }
00171
00172 TEST(UrlEncodedPostTests, FormPostFileBufferTest) {
00173 auto content = std::string{"hello world"};
00174 auto url = Url{base + "/form_post.html"};
00175 auto response = cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});
00176 auto expected_text = std::string{"{\n"
00177 " \"x\": " + content + "\n"
00178 "}"};
00179 EXPECT_EQ(expected_text, response.text);
00180 EXPECT_EQ(url, response.url);
00181 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00182 EXPECT_EQ(201, response.status_code);
00183 EXPECT_EQ(ErrorCode::OK, response.error.code);
00184 }
00185
00186 TEST(UrlEncodedPostTests, FormPostFileBufferNoCopyTest) {
00187 auto content = std::string{"hello world"};
00188 auto url = Url{base + "/form_post.html"};
00189 auto multipart = Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}};
00190 auto response = cpr::Post(url, multipart);
00191 auto expected_text = std::string{"{\n"
00192 " \"x\": " + content + "\n"
00193 "}"};
00194 EXPECT_EQ(expected_text, response.text);
00195 EXPECT_EQ(url, response.url);
00196 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00197 EXPECT_EQ(201, response.status_code);
00198 EXPECT_EQ(ErrorCode::OK, response.error.code);
00199 }
00200
00201 TEST(UrlEncodedPostTests, FormPostFileBufferPointerTest) {
00202 const char *content = "hello world";
00203 auto url = Url{base + "/form_post.html"};
00204 auto response = cpr::Post(url, Multipart{{"x", Buffer{content, 11 + content, "test_file"}}});
00205 auto expected_text = std::string{"{\n"
00206 " \"x\": " + std::string(content) + "\n"
00207 "}"};
00208 EXPECT_EQ(expected_text, response.text);
00209 EXPECT_EQ(url, response.url);
00210 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00211 EXPECT_EQ(201, response.status_code);
00212 EXPECT_EQ(ErrorCode::OK, response.error.code);
00213 }
00214
00215 TEST(UrlEncodedPostTests, FormPostFileBufferArrayTest) {
00216 const char content[] = "hello world";
00217 auto url = Url{base + "/form_post.html"};
00218
00219 auto response = cpr::Post(
00220 url, Multipart{{"x", Buffer{std::begin(content), std::end(content) - 1, "test_file"}}});
00221 auto expected_text = std::string{"{\n"
00222 " \"x\": " + std::string(content) + "\n"
00223 "}"};
00224 EXPECT_EQ(expected_text, response.text);
00225 EXPECT_EQ(url, response.url);
00226 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00227 EXPECT_EQ(201, response.status_code);
00228 EXPECT_EQ(ErrorCode::OK, response.error.code);
00229 }
00230
00231 TEST(UrlEncodedPostTests, FormPostFileBufferVectorTest) {
00232 std::vector<unsigned char> content{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
00233 auto url = Url{base + "/form_post.html"};
00234 auto response =
00235 cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});
00236 auto expected_text = std::string{"{\n"
00237 " \"x\": hello world\n"
00238 "}"};
00239 EXPECT_EQ(expected_text, response.text);
00240 EXPECT_EQ(url, response.url);
00241 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00242 EXPECT_EQ(201, response.status_code);
00243 EXPECT_EQ(ErrorCode::OK, response.error.code);
00244 }
00245
00246 TEST(UrlEncodedPostTests, FormPostFileBufferStdArrayTest) {
00247 std::array<unsigned char, 11> content{{'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'}};
00248 auto url = Url{base + "/form_post.html"};
00249 auto response =
00250 cpr::Post(url, Multipart{{"x", Buffer{content.begin(), content.end(), "test_file"}}});
00251 auto expected_text = std::string{"{\n"
00252 " \"x\": hello world\n"
00253 "}"};
00254 EXPECT_EQ(expected_text, response.text);
00255 EXPECT_EQ(url, response.url);
00256 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00257 EXPECT_EQ(201, response.status_code);
00258 EXPECT_EQ(ErrorCode::OK, response.error.code);
00259 }
00260
00261 TEST(UrlEncodedPostTests, FormPostManyTest) {
00262 auto url = Url{base + "/form_post.html"};
00263 auto response = cpr::Post(url, Multipart{{"x", 5}, {"y", 13}});
00264 auto expected_text = std::string{"{\n"
00265 " \"x\": 5,\n"
00266 " \"y\": 13,\n"
00267 " \"sum\": 18\n"
00268 "}"};
00269 EXPECT_EQ(expected_text, response.text);
00270 EXPECT_EQ(url, response.url);
00271 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00272 EXPECT_EQ(201, response.status_code);
00273 EXPECT_EQ(ErrorCode::OK, response.error.code);
00274 }
00275
00276 TEST(UrlEncodedPostTests, FormPostManyNoCopyTest) {
00277 auto url = Url{base + "/form_post.html"};
00278 auto multipart = Multipart{{"x", 5}, {"y", 13}};
00279 auto response = cpr::Post(url, multipart);
00280 auto expected_text = std::string{"{\n"
00281 " \"x\": 5,\n"
00282 " \"y\": 13,\n"
00283 " \"sum\": 18\n"
00284 "}"};
00285 EXPECT_EQ(expected_text, response.text);
00286 EXPECT_EQ(url, response.url);
00287 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00288 EXPECT_EQ(201, response.status_code);
00289 EXPECT_EQ(ErrorCode::OK, response.error.code);
00290 }
00291
00292 TEST(UrlEncodedPostTests, FormPostContentTypeTest) {
00293 auto url = Url{base + "/form_post.html"};
00294 auto response = cpr::Post(url, Multipart{{"x", 5, "application/number"}});
00295 auto expected_text = std::string{"{\n"
00296 " \"x\": 5\n"
00297 "}"};
00298 EXPECT_EQ(expected_text, response.text);
00299 EXPECT_EQ(url, response.url);
00300 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00301 EXPECT_EQ(201, response.status_code);
00302 EXPECT_EQ(ErrorCode::OK, response.error.code);
00303 }
00304
00305 TEST(UrlEncodedPostTests, FormPostContentTypeLValueTest) {
00306 auto url = Url{base + "/form_post.html"};
00307 auto multipart = Multipart{{"x", 5, "application/number"}};
00308 auto response = cpr::Post(url, multipart);
00309 auto expected_text = std::string{"{\n"
00310 " \"x\": 5\n"
00311 "}"};
00312 EXPECT_EQ(expected_text, response.text);
00313 EXPECT_EQ(url, response.url);
00314 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00315 EXPECT_EQ(201, response.status_code);
00316 EXPECT_EQ(ErrorCode::OK, response.error.code);
00317 }
00318
00319 TEST(UrlEncodedPostTests, UrlPostAsyncSingleTest) {
00320 auto url = Url{base + "/url_post.html"};
00321 auto payload = Payload{{"x", "5"}};
00322 std::vector<AsyncResponse> responses;
00323 for (int i = 0; i < 10; ++i) {
00324 responses.emplace_back(cpr::PostAsync(url, payload));
00325 }
00326 for (auto& future_response : responses) {
00327 auto response = future_response.get();
00328 auto expected_text = std::string{"{\n"
00329 " \"x\": 5\n"
00330 "}"};
00331 EXPECT_EQ(expected_text, response.text);
00332 EXPECT_EQ(url, response.url);
00333 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00334 EXPECT_EQ(201, response.status_code);
00335 EXPECT_EQ(ErrorCode::OK, response.error.code);
00336 }
00337 }
00338
00339 TEST(UrlEncodedPostTests, UrlReflectTest) {
00340 auto url = Url{base + "/header_reflect.html"};
00341 auto response = cpr::Post(url, Payload{{"x", "5"}});
00342 auto expected_text = std::string{"Header reflect POST"};
00343 EXPECT_EQ(expected_text, response.text);
00344 EXPECT_EQ(url, response.url);
00345 EXPECT_EQ(200, response.status_code);
00346 EXPECT_EQ(ErrorCode::OK, response.error.code);
00347 }
00348
00349 int main(int argc, char** argv) {
00350 ::testing::InitGoogleTest(&argc, argv);
00351 ::testing::AddGlobalTestEnvironment(server);
00352 return RUN_ALL_TESTS();
00353 }