session_tests.cpp
Go to the documentation of this file.
00001 #include <gtest/gtest.h>
00002 
00003 #include <chrono>
00004 #include <string>
00005 
00006 #include <cpr/cpr.h>
00007 
00008 #include "server.h"
00009 
00010 using namespace cpr;
00011 
00012 static Server* server = new Server();
00013 auto base = server->GetBaseUrl();
00014 
00015 TEST(RedirectTests, TemporaryDefaultRedirectTest) {
00016     auto url = Url{base + "/temporary_redirect.html"};
00017     Session session;
00018     session.SetUrl(url);
00019     auto response = session.Get();
00020     auto expected_text = std::string{"Hello world!"};
00021     EXPECT_EQ(expected_text, response.text);
00022     EXPECT_EQ(Url{base + "/hello.html"}, response.url);
00023     EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00024     EXPECT_EQ(200, response.status_code);
00025     EXPECT_EQ(ErrorCode::OK, response.error.code);
00026 }
00027 
00028 TEST(RedirectTests, NoTemporaryRedirectTest) {
00029     auto url = Url{base + "/temporary_redirect.html"};
00030     Session session;
00031     session.SetUrl(url);
00032     session.SetRedirect(false);
00033     auto response = session.Get();
00034     auto expected_text = std::string{"Found"};
00035     EXPECT_EQ(expected_text, response.text);
00036     EXPECT_EQ(url, response.url);
00037     EXPECT_EQ(std::string{}, response.header["content-type"]);
00038     EXPECT_EQ(302, response.status_code);
00039     EXPECT_EQ(ErrorCode::OK, response.error.code);
00040 }
00041 
00042 TEST(RedirectTests, PermanentDefaultRedirectTest) {
00043     auto url = Url{base + "/permanent_redirect.html"};
00044     Session session;
00045     session.SetUrl(url);
00046     auto response = session.Get();
00047     auto expected_text = std::string{"Hello world!"};
00048     EXPECT_EQ(expected_text, response.text);
00049     EXPECT_EQ(Url{base + "/hello.html"}, response.url);
00050     EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00051     EXPECT_EQ(200, response.status_code);
00052     EXPECT_EQ(ErrorCode::OK, response.error.code);
00053 }
00054 
00055 TEST(RedirectTests, NoPermanentRedirectTest) {
00056     auto url = Url{base + "/permanent_redirect.html"};
00057     Session session;
00058     session.SetUrl(url);
00059     session.SetRedirect(false);
00060     auto response = session.Get();
00061     auto expected_text = std::string{"Moved Permanently"};
00062     EXPECT_EQ(expected_text, response.text);
00063     EXPECT_EQ(url, response.url);
00064     EXPECT_EQ(std::string{}, response.header["content-type"]);
00065     EXPECT_EQ(301, response.status_code);
00066     EXPECT_EQ(ErrorCode::OK, response.error.code);
00067 }
00068 
00069 TEST(MaxRedirectsTests, ZeroMaxRedirectsSuccessTest) {
00070     auto url = Url{base + "/hello.html"};
00071     Session session;
00072     session.SetUrl(url);
00073     session.SetMaxRedirects(MaxRedirects(0));
00074     auto response = session.Get();
00075     auto expected_text = std::string{"Hello world!"};
00076     EXPECT_EQ(expected_text, response.text);
00077     EXPECT_EQ(url, response.url);
00078     EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00079     EXPECT_EQ(200, response.status_code);
00080     EXPECT_EQ(ErrorCode::OK, response.error.code);
00081 }
00082 
00083 TEST(MaxRedirectsTests, ZeroMaxRedirectsFailureTest) {
00084     auto url = Url{base + "/permanent_redirect.html"};
00085     Session session;
00086     session.SetUrl(url);
00087     session.SetMaxRedirects(MaxRedirects(0));
00088     auto response = session.Get();
00089     EXPECT_EQ(std::string{}, response.text);
00090     EXPECT_EQ(url, response.url);
00091     EXPECT_EQ(std::string{}, response.header["content-type"]);
00092     EXPECT_EQ(301, response.status_code);
00093     EXPECT_EQ(ErrorCode::OK, response.error.code);
00094 }
00095 
00096 TEST(MaxRedirectsTests, OneMaxRedirectsSuccessTest) {
00097     auto url = Url{base + "/permanent_redirect.html"};
00098     Session session;
00099     session.SetUrl(url);
00100     session.SetMaxRedirects(MaxRedirects(1));
00101     auto response = session.Get();
00102     auto expected_text = std::string{"Hello world!"};
00103     EXPECT_EQ(expected_text, response.text);
00104     EXPECT_EQ(Url{base + "/hello.html"}, response.url);
00105     EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00106     EXPECT_EQ(200, response.status_code);
00107     EXPECT_EQ(ErrorCode::OK, response.error.code);
00108 }
00109 
00110 TEST(MaxRedirectsTests, OneMaxRedirectsFailureTest) {
00111     auto url = Url{base + "/two_redirects.html"};
00112     Session session;
00113     session.SetUrl(url);
00114     session.SetMaxRedirects(MaxRedirects(1));
00115     auto response = session.Get();
00116     EXPECT_EQ(std::string{}, response.text);
00117     EXPECT_EQ(Url{base + "/permanent_redirect.html"}, response.url);
00118     EXPECT_EQ(std::string{}, response.header["content-type"]);
00119     EXPECT_EQ(301, response.status_code);
00120     EXPECT_EQ(ErrorCode::OK, response.error.code);
00121 }
00122 
00123 TEST(MaxRedirectsTests, TwoMaxRedirectsSuccessTest) {
00124     auto url = Url{base + "/two_redirects.html"};
00125     Session session;
00126     session.SetUrl(url);
00127     session.SetMaxRedirects(MaxRedirects(2));
00128     auto response = session.Get();
00129     auto expected_text = std::string{"Hello world!"};
00130     EXPECT_EQ(expected_text, response.text);
00131     EXPECT_EQ(Url{base + "/hello.html"}, response.url);
00132     EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00133     EXPECT_EQ(200, response.status_code);
00134     EXPECT_EQ(ErrorCode::OK, response.error.code);
00135 }
00136 
00137 TEST(MultipleGetTests, BasicMultipleGetTest) {
00138     auto url = Url{base + "/hello.html"};
00139     Session session;
00140     session.SetUrl(url);
00141     for (int i = 0; i < 100; ++i) {
00142         auto response = session.Get();
00143         auto expected_text = std::string{"Hello world!"};
00144         EXPECT_EQ(expected_text, response.text);
00145         EXPECT_EQ(url, response.url);
00146         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00147         EXPECT_EQ(200, response.status_code);
00148         EXPECT_EQ(ErrorCode::OK, response.error.code);
00149     }
00150 }
00151 
00152 TEST(MultipleGetTests, UrlChangeMultipleGetTest) {
00153     Session session;
00154     {
00155         auto url = Url{base + "/hello.html"};
00156         session.SetUrl(url);
00157         auto response = session.Get();
00158         auto expected_text = std::string{"Hello world!"};
00159         EXPECT_EQ(expected_text, response.text);
00160         EXPECT_EQ(url, response.url);
00161         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00162         EXPECT_EQ(200, response.status_code);
00163         EXPECT_EQ(ErrorCode::OK, response.error.code);
00164     }
00165     {
00166         auto url = Url{base + "/basic.json"};
00167         session.SetUrl(url);
00168         auto response = session.Get();
00169         auto expected_text = std::string{"[\n"
00170                                          "  {\n"
00171                                          "    \"first_key\": \"first_value\",\n"
00172                                          "    \"second_key\": \"second_value\"\n"
00173                                          "  }\n"
00174                                          "]"};
00175         EXPECT_EQ(expected_text, response.text);
00176         EXPECT_EQ(url, response.url);
00177         EXPECT_EQ(std::string{"application/octet-stream"}, response.header["content-type"]);
00178         EXPECT_EQ(200, response.status_code);
00179         EXPECT_EQ(ErrorCode::OK, response.error.code);
00180     }
00181 }
00182 
00183 TEST(MultipleGetTests, HeaderMultipleGetTest) {
00184     auto url = Url{base + "/header_reflect.html"};
00185     Session session;
00186     session.SetUrl(url);
00187     session.SetHeader(Header{{"hello", "world"}});
00188     for (int i = 0; i < 100; ++i) {
00189         auto response = session.Get();
00190         auto expected_text = std::string{"Header reflect GET"};
00191         EXPECT_EQ(expected_text, response.text);
00192         EXPECT_EQ(url, response.url);
00193         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00194         EXPECT_EQ(std::string{"world"}, response.header["hello"]);
00195         EXPECT_EQ(200, response.status_code);
00196         EXPECT_EQ(ErrorCode::OK, response.error.code);
00197     }
00198 }
00199 
00200 TEST(MultipleGetTests, HeaderChangeMultipleGetTest) {
00201     auto url = Url{base + "/header_reflect.html"};
00202     Session session;
00203     session.SetUrl(url);
00204     session.SetHeader(Header{{"hello", "world"}});
00205     {
00206         auto response = session.Get();
00207         auto expected_text = std::string{"Header reflect GET"};
00208         EXPECT_EQ(expected_text, response.text);
00209         EXPECT_EQ(url, response.url);
00210         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00211         EXPECT_EQ(std::string{"world"}, response.header["hello"]);
00212         EXPECT_EQ(200, response.status_code);
00213         EXPECT_EQ(ErrorCode::OK, response.error.code);
00214     }
00215     session.SetHeader(Header{{"key", "value"}});
00216     {
00217         auto response = session.Get();
00218         auto expected_text = std::string{"Header reflect GET"};
00219         EXPECT_EQ(expected_text, response.text);
00220         EXPECT_EQ(url, response.url);
00221         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00222         EXPECT_EQ(std::string{"value"}, response.header["key"]);
00223         EXPECT_EQ(200, response.status_code);
00224         EXPECT_EQ(ErrorCode::OK, response.error.code);
00225     }
00226 }
00227 
00228 TEST(MultipleGetTests, ParameterMultipleGetTest) {
00229     auto url = Url{base + "/hello.html"};
00230     Session session;
00231     session.SetUrl(url);
00232     session.SetParameters({{"hello", "world"}});
00233     for (int i = 0; i < 100; ++i) {
00234         auto response = session.Get();
00235         auto expected_text = std::string{"Hello world!"};
00236         EXPECT_EQ(expected_text, response.text);
00237         EXPECT_EQ(Url{url + "?hello=world"}, response.url);
00238         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00239         EXPECT_EQ(200, response.status_code);
00240         EXPECT_EQ(ErrorCode::OK, response.error.code);
00241     }
00242 }
00243 
00244 TEST(MultipleGetTests, ParameterChangeMultipleGetTest) {
00245     auto url = Url{base + "/hello.html"};
00246     Session session;
00247     session.SetUrl(url);
00248     session.SetParameters({{"hello", "world"}});
00249     {
00250         auto response = session.Get();
00251         auto expected_text = std::string{"Hello world!"};
00252         EXPECT_EQ(expected_text, response.text);
00253         EXPECT_EQ(Url{url + "?hello=world"}, response.url);
00254         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00255         EXPECT_EQ(200, response.status_code);
00256         EXPECT_EQ(ErrorCode::OK, response.error.code);
00257     }
00258     session.SetUrl(url);
00259     session.SetParameters({{"key", "value"}});
00260     {
00261         auto response = session.Get();
00262         auto expected_text = std::string{"Hello world!"};
00263         EXPECT_EQ(expected_text, response.text);
00264         EXPECT_EQ(Url{url + "?key=value"}, response.url);
00265         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00266         EXPECT_EQ(200, response.status_code);
00267         EXPECT_EQ(ErrorCode::OK, response.error.code);
00268     }
00269 }
00270 
00271 TEST(MultipleGetTests, BasicAuthenticationMultipleGetTest) {
00272     auto url = Url{base + "/basic_auth.html"};
00273     Session session;
00274     session.SetUrl(url);
00275     session.SetAuth(Authentication{"user", "password"});
00276     for (int i = 0; i < 100; ++i) {
00277         auto response = session.Get();
00278         auto expected_text = std::string{"Header reflect GET"};
00279         EXPECT_EQ(expected_text, response.text);
00280         EXPECT_EQ(url, response.url);
00281         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00282         EXPECT_EQ(200, response.status_code);
00283         EXPECT_EQ(ErrorCode::OK, response.error.code);
00284     }
00285 }
00286 
00287 TEST(MultipleGetTests, BasicAuthenticationChangeMultipleGetTest) {
00288     auto url = Url{base + "/basic_auth.html"};
00289     Session session;
00290     session.SetUrl(url);
00291     session.SetAuth(Authentication{"user", "password"});
00292     {
00293         auto response = session.Get();
00294         auto expected_text = std::string{"Header reflect GET"};
00295         EXPECT_EQ(expected_text, response.text);
00296         EXPECT_EQ(url, response.url);
00297         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00298         EXPECT_EQ(200, response.status_code);
00299         EXPECT_EQ(ErrorCode::OK, response.error.code);
00300     }
00301     session.SetAuth(Authentication{"user", "bad_password"});
00302     {
00303         auto response = session.Get();
00304         EXPECT_EQ(std::string{}, response.text);
00305         EXPECT_EQ(url, response.url);
00306         EXPECT_EQ(std::string{}, response.header["content-type"]);
00307         EXPECT_EQ(401, response.status_code);
00308         EXPECT_EQ(ErrorCode::OK, response.error.code);
00309     }
00310     session.SetAuth(Authentication{"bad_user", "password"});
00311     {
00312         auto response = session.Get();
00313         EXPECT_EQ(std::string{}, response.text);
00314         EXPECT_EQ(url, response.url);
00315         EXPECT_EQ(std::string{}, response.header["content-type"]);
00316         EXPECT_EQ(401, response.status_code);
00317         EXPECT_EQ(ErrorCode::OK, response.error.code);
00318     }
00319 }
00320 
00321 TEST(ParameterTests, ParameterSingleTest) {
00322     auto url = Url{base + "/hello.html"};
00323     Session session;
00324     session.SetUrl(url);
00325     Parameters parameters{{"hello", "world"}};
00326     session.SetParameters(parameters);
00327     auto response = session.Get();
00328     auto expected_text = std::string{"Hello world!"};
00329     EXPECT_EQ(expected_text, response.text);
00330     EXPECT_EQ(Url{url + "?hello=world"}, response.url);
00331     EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00332     EXPECT_EQ(200, response.status_code);
00333     EXPECT_EQ(ErrorCode::OK, response.error.code);
00334 }
00335 
00336 TEST(ParameterTests, ParameterMultipleTest) {
00337     auto url = Url{base + "/hello.html"};
00338     Session session;
00339     session.SetUrl(url);
00340     Parameters parameters{{"hello", "world"}, {"key", "value"}};
00341     session.SetParameters(parameters);
00342     auto response = session.Get();
00343     auto expected_text = std::string{"Hello world!"};
00344     EXPECT_EQ(expected_text, response.text);
00345     EXPECT_EQ(Url{url + "?hello=world&key=value"}, response.url);
00346     EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00347     EXPECT_EQ(200, response.status_code);
00348     EXPECT_EQ(ErrorCode::OK, response.error.code);
00349 }
00350 
00351 TEST(TimeoutTests, SetTimeoutTest) {
00352     auto url = Url{base + "/hello.html"};
00353     Session session;
00354     session.SetUrl(url);
00355     session.SetTimeout(0L);
00356     auto response = session.Get();
00357     auto expected_text = std::string{"Hello world!"};
00358     EXPECT_EQ(expected_text, response.text);
00359     EXPECT_EQ(url, response.url);
00360     EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00361     EXPECT_EQ(200, response.status_code);
00362     EXPECT_EQ(ErrorCode::OK, response.error.code);
00363 }
00364 
00365 TEST(TimeoutTests, SetTimeoutLongTest) {
00366     auto url = Url{base + "/hello.html"};
00367     Session session;
00368     session.SetUrl(url);
00369     session.SetTimeout(10000L);
00370     auto response = session.Get();
00371     auto expected_text = std::string{"Hello world!"};
00372     EXPECT_EQ(expected_text, response.text);
00373     EXPECT_EQ(url, response.url);
00374     EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00375     EXPECT_EQ(200, response.status_code);
00376     EXPECT_EQ(ErrorCode::OK, response.error.code);
00377 }
00378 
00379 TEST(TimeoutTests, SetChronoTimeoutTest) {
00380     auto url = Url{base + "/hello.html"};
00381     Session session;
00382     session.SetUrl(url);
00383     session.SetTimeout(std::chrono::milliseconds{0});
00384     auto response = session.Get();
00385     auto expected_text = std::string{"Hello world!"};
00386     EXPECT_EQ(expected_text, response.text);
00387     EXPECT_EQ(url, response.url);
00388     EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00389     EXPECT_EQ(200, response.status_code);
00390     EXPECT_EQ(ErrorCode::OK, response.error.code);
00391 }
00392 
00393 TEST(TimeoutTests, SetChronoTimeoutLongTest) {
00394     auto url = Url{base + "/hello.html"};
00395     Session session;
00396     session.SetUrl(url);
00397     session.SetTimeout(std::chrono::milliseconds{10000});
00398     auto response = session.Get();
00399     auto expected_text = std::string{"Hello world!"};
00400     EXPECT_EQ(expected_text, response.text);
00401     EXPECT_EQ(url, response.url);
00402     EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00403     EXPECT_EQ(200, response.status_code);
00404     EXPECT_EQ(ErrorCode::OK, response.error.code);
00405 }
00406 
00407 TEST(LowSpeedTests, SetLowSpeedTest) {
00408     auto url = Url{base + "/hello.html"};
00409     Session session;
00410     session.SetUrl(url);
00411     session.SetLowSpeed({1, 1});
00412     auto response = session.Get();
00413     auto expected_text = std::string{"Hello world!"};
00414     EXPECT_EQ(expected_text, response.text);
00415     EXPECT_EQ(url, response.url);
00416     EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00417     EXPECT_EQ(200, response.status_code);
00418     EXPECT_EQ(ErrorCode::OK, response.error.code);
00419 }
00420 
00421 TEST(PayloadTests, SetPayloadTest) {
00422     auto url = Url{base + "/url_post.html"};
00423     Session session;
00424     session.SetUrl(url);
00425     session.SetPayload({{"x", "5"}});
00426     auto response = session.Post();
00427     auto expected_text = std::string{"{\n"
00428                                      "  \"x\": 5\n"
00429                                      "}"};
00430     EXPECT_EQ(expected_text, response.text);
00431     EXPECT_EQ(url, response.url);
00432     EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00433     EXPECT_EQ(201, response.status_code);
00434     EXPECT_EQ(ErrorCode::OK, response.error.code);
00435 }
00436 
00437 TEST(PayloadTests, SetPayloadLValueTest) {
00438     auto url = Url{base + "/url_post.html"};
00439     Session session;
00440     session.SetUrl(url);
00441     auto payload = Payload{{"x", "5"}};
00442     session.SetPayload(payload);
00443     auto response = session.Post();
00444     auto expected_text = std::string{"{\n"
00445                                      "  \"x\": 5\n"
00446                                      "}"};
00447     EXPECT_EQ(expected_text, response.text);
00448     EXPECT_EQ(url, response.url);
00449     EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00450     EXPECT_EQ(201, response.status_code);
00451     EXPECT_EQ(ErrorCode::OK, response.error.code);
00452 }
00453 
00454 TEST(MultipartTests, SetMultipartTest) {
00455     auto url = Url{base + "/form_post.html"};
00456     Session session;
00457     session.SetUrl(url);
00458     session.SetMultipart({{"x", "5"}});
00459     auto response = session.Post();
00460     auto expected_text = std::string{"{\n"
00461                                      "  \"x\": 5\n"
00462                                      "}"};
00463     EXPECT_EQ(expected_text, response.text);
00464     EXPECT_EQ(url, response.url);
00465     EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00466     EXPECT_EQ(201, response.status_code);
00467     EXPECT_EQ(ErrorCode::OK, response.error.code);
00468 }
00469 
00470 TEST(MultipartTests, SetMultipartValueTest) {
00471     auto url = Url{base + "/form_post.html"};
00472     Session session;
00473     session.SetUrl(url);
00474     auto multipart = Multipart{{"x", "5"}};
00475     session.SetMultipart(multipart);
00476     auto response = session.Post();
00477     auto expected_text = std::string{"{\n"
00478                                      "  \"x\": 5\n"
00479                                      "}"};
00480     EXPECT_EQ(expected_text, response.text);
00481     EXPECT_EQ(url, response.url);
00482     EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00483     EXPECT_EQ(201, response.status_code);
00484     EXPECT_EQ(ErrorCode::OK, response.error.code);
00485 }
00486 
00487 TEST(BodyTests, SetBodyTest) {
00488     auto url = Url{base + "/url_post.html"};
00489     Session session;
00490     session.SetUrl(url);
00491     session.SetBody(Body{"x=5"});
00492     auto response = session.Post();
00493     auto expected_text = std::string{"{\n"
00494                                      "  \"x\": 5\n"
00495                                      "}"};
00496     EXPECT_EQ(expected_text, response.text);
00497     EXPECT_EQ(url, response.url);
00498     EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00499     EXPECT_EQ(201, response.status_code);
00500     EXPECT_EQ(ErrorCode::OK, response.error.code);
00501 }
00502 
00503 TEST(BodyTests, SetBodyValueTest) {
00504     auto url = Url{base + "/url_post.html"};
00505     Session session;
00506     session.SetUrl(url);
00507     auto body = Body{"x=5"};
00508     session.SetBody(body);
00509     auto response = session.Post();
00510     auto expected_text = std::string{"{\n"
00511                                      "  \"x\": 5\n"
00512                                      "}"};
00513     EXPECT_EQ(expected_text, response.text);
00514     EXPECT_EQ(url, response.url);
00515     EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00516     EXPECT_EQ(201, response.status_code);
00517     EXPECT_EQ(ErrorCode::OK, response.error.code);
00518 }
00519 
00520 TEST(DigestTests, SetDigestTest) {
00521     auto url = Url{base + "/digest_auth.html"};
00522     Session session;
00523     session.SetUrl(url);
00524     session.SetDigest({"user", "password"});
00525     auto response = session.Get();
00526     auto expected_text = std::string{"Header reflect GET"};
00527     EXPECT_EQ(expected_text, response.text);
00528     EXPECT_EQ(url, response.url);
00529     EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00530     EXPECT_EQ(200, response.status_code);
00531     EXPECT_EQ(ErrorCode::OK, response.error.code);
00532 }
00533 
00534 TEST(CookiesTests, BasicCookiesTest) {
00535     auto url = Url{base + "/basic_cookies.html"};
00536     Session session{};
00537     session.SetUrl(url);
00538     Cookies cookies;
00539 
00540     {
00541         auto response = session.Get();
00542         auto expected_text = std::string{"Hello world!"};
00543         EXPECT_EQ(expected_text, response.text);
00544         EXPECT_EQ(url, response.url);
00545         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00546         EXPECT_EQ(200, response.status_code);
00547         EXPECT_EQ(ErrorCode::OK, response.error.code);
00548         cookies = response.cookies;
00549     }
00550     {
00551         cookies["hello"] = "world";
00552         cookies["my"] = "another; fake=cookie;"; // This is url encoded
00553         session.SetCookies(cookies);
00554         auto response = session.Get();
00555         auto expected_text = std::string{"Hello world!"};
00556         EXPECT_EQ(expected_text, response.text);
00557         EXPECT_EQ(url, response.url);
00558         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00559         EXPECT_EQ(200, response.status_code);
00560         EXPECT_EQ(ErrorCode::OK, response.error.code);
00561         EXPECT_EQ(cookies["cookie"], response.cookies["cookie"]);
00562         EXPECT_EQ(cookies["icecream"], response.cookies["icecream"]);
00563         EXPECT_EQ(cookies["expires"], response.cookies["expires"]);
00564     }
00565 }
00566 
00567 TEST(CookiesTests, CookiesConstructorTest) {
00568     auto url = Url{base + "/basic_cookies.html"};
00569     Session session{};
00570     session.SetUrl(url);
00571     Cookies cookies;
00572 
00573     {
00574         auto response = session.Get();
00575         auto expected_text = std::string{"Hello world!"};
00576         EXPECT_EQ(expected_text, response.text);
00577         EXPECT_EQ(url, response.url);
00578         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00579         EXPECT_EQ(200, response.status_code);
00580         EXPECT_EQ(ErrorCode::OK, response.error.code);
00581         cookies = response.cookies;
00582     }
00583     {
00584         cookies = Cookies{{"hello", "world"}, {"my", "another; fake=cookie;"}};
00585         session.SetCookies(cookies);
00586         auto response = session.Get();
00587         auto expected_text = std::string{"Hello world!"};
00588         EXPECT_EQ(expected_text, response.text);
00589         EXPECT_EQ(url, response.url);
00590         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00591         EXPECT_EQ(200, response.status_code);
00592         EXPECT_EQ(ErrorCode::OK, response.error.code);
00593         cookies = response.cookies;
00594         EXPECT_EQ(cookies["cookie"], response.cookies["cookie"]);
00595         EXPECT_EQ(cookies["icecream"], response.cookies["icecream"]);
00596         EXPECT_EQ(cookies["expires"], response.cookies["expires"]);
00597     }
00598 }
00599 
00600 TEST(DifferentMethodTests, GetPostTest) {
00601     Session session;
00602     {
00603         auto url = Url{base + "/hello.html"};
00604         session.SetUrl(url);
00605         auto response = session.Get();
00606         auto expected_text = std::string{"Hello world!"};
00607         EXPECT_EQ(expected_text, response.text);
00608         EXPECT_EQ(url, response.url);
00609         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00610         EXPECT_EQ(200, response.status_code);
00611         EXPECT_EQ(ErrorCode::OK, response.error.code);
00612     }
00613     {
00614         auto url = Url{base + "/url_post.html"};
00615         session.SetUrl(url);
00616         session.SetPayload({{"x", "5"}});
00617         auto response = session.Post();
00618         auto expected_text = std::string{"{\n"
00619                                          "  \"x\": 5\n"
00620                                          "}"};
00621         EXPECT_EQ(expected_text, response.text);
00622         EXPECT_EQ(url, response.url);
00623         EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00624         EXPECT_EQ(201, response.status_code);
00625         EXPECT_EQ(ErrorCode::OK, response.error.code);
00626     }
00627 }
00628 
00629 TEST(DifferentMethodTests, PostGetTest) {
00630     Session session;
00631     {
00632         auto url = Url{base + "/url_post.html"};
00633         session.SetUrl(url);
00634         session.SetPayload({{"x", "5"}});
00635         auto response = session.Post();
00636         auto expected_text = std::string{"{\n"
00637                                          "  \"x\": 5\n"
00638                                          "}"};
00639         EXPECT_EQ(expected_text, response.text);
00640         EXPECT_EQ(url, response.url);
00641         EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00642         EXPECT_EQ(201, response.status_code);
00643         EXPECT_EQ(ErrorCode::OK, response.error.code);
00644     }
00645     {
00646         auto url = Url{base + "/hello.html"};
00647         session.SetUrl(url);
00648         auto response = session.Get();
00649         auto expected_text = std::string{"Hello world!"};
00650         EXPECT_EQ(expected_text, response.text);
00651         EXPECT_EQ(url, response.url);
00652         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00653         EXPECT_EQ(200, response.status_code);
00654         EXPECT_EQ(ErrorCode::OK, response.error.code);
00655     }
00656 }
00657 
00658 TEST(DifferentMethodTests, GetPostGetTest) {
00659     Session session;
00660     {
00661         auto url = Url{base + "/hello.html"};
00662         session.SetUrl(url);
00663         auto response = session.Get();
00664         auto expected_text = std::string{"Hello world!"};
00665         EXPECT_EQ(expected_text, response.text);
00666         EXPECT_EQ(url, response.url);
00667         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00668         EXPECT_EQ(200, response.status_code);
00669         EXPECT_EQ(ErrorCode::OK, response.error.code);
00670     }
00671     {
00672         auto url = Url{base + "/url_post.html"};
00673         session.SetUrl(url);
00674         session.SetPayload({{"x", "5"}});
00675         auto response = session.Post();
00676         auto expected_text = std::string{"{\n"
00677                                          "  \"x\": 5\n"
00678                                          "}"};
00679         EXPECT_EQ(expected_text, response.text);
00680         EXPECT_EQ(url, response.url);
00681         EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00682         EXPECT_EQ(201, response.status_code);
00683         EXPECT_EQ(ErrorCode::OK, response.error.code);
00684     }
00685     {
00686         auto url = Url{base + "/hello.html"};
00687         session.SetUrl(url);
00688         auto response = session.Get();
00689         auto expected_text = std::string{"Hello world!"};
00690         EXPECT_EQ(expected_text, response.text);
00691         EXPECT_EQ(url, response.url);
00692         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00693         EXPECT_EQ(200, response.status_code);
00694         EXPECT_EQ(ErrorCode::OK, response.error.code);
00695     }
00696 }
00697 
00698 TEST(DifferentMethodTests, PostGetPostTest) {
00699     Session session;
00700     {
00701         auto url = Url{base + "/url_post.html"};
00702         session.SetUrl(url);
00703         session.SetPayload({{"x", "5"}});
00704         auto response = session.Post();
00705         auto expected_text = std::string{"{\n"
00706                                          "  \"x\": 5\n"
00707                                          "}"};
00708         EXPECT_EQ(expected_text, response.text);
00709         EXPECT_EQ(url, response.url);
00710         EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00711         EXPECT_EQ(201, response.status_code);
00712         EXPECT_EQ(ErrorCode::OK, response.error.code);
00713     }
00714     {
00715         auto url = Url{base + "/hello.html"};
00716         session.SetUrl(url);
00717         auto response = session.Get();
00718         auto expected_text = std::string{"Hello world!"};
00719         EXPECT_EQ(expected_text, response.text);
00720         EXPECT_EQ(url, response.url);
00721         EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00722         EXPECT_EQ(200, response.status_code);
00723         EXPECT_EQ(ErrorCode::OK, response.error.code);
00724     }
00725     {
00726         auto url = Url{base + "/url_post.html"};
00727         session.SetUrl(url);
00728         session.SetPayload({{"x", "5"}});
00729         auto response = session.Post();
00730         auto expected_text = std::string{"{\n"
00731                                          "  \"x\": 5\n"
00732                                          "}"};
00733         EXPECT_EQ(expected_text, response.text);
00734         EXPECT_EQ(url, response.url);
00735         EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00736         EXPECT_EQ(201, response.status_code);
00737         EXPECT_EQ(ErrorCode::OK, response.error.code);
00738     }
00739 }
00740 
00741 TEST(DifferentMethodTests, MultipleGetPostTest) {
00742     Session session;
00743     for (int i = 0; i < 100; ++i) {
00744         {
00745             auto url = Url{base + "/hello.html"};
00746             session.SetUrl(url);
00747             auto response = session.Get();
00748             auto expected_text = std::string{"Hello world!"};
00749             EXPECT_EQ(expected_text, response.text);
00750             EXPECT_EQ(url, response.url);
00751             EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00752             EXPECT_EQ(200, response.status_code);
00753             EXPECT_EQ(ErrorCode::OK, response.error.code);
00754         }
00755         {
00756             auto url = Url{base + "/url_post.html"};
00757             session.SetUrl(url);
00758             session.SetPayload({{"x", "5"}});
00759             auto response = session.Post();
00760             auto expected_text = std::string{"{\n"
00761                                              "  \"x\": 5\n"
00762                                              "}"};
00763             EXPECT_EQ(expected_text, response.text);
00764             EXPECT_EQ(url, response.url);
00765             EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00766             EXPECT_EQ(201, response.status_code);
00767             EXPECT_EQ(ErrorCode::OK, response.error.code);
00768         }
00769     }
00770 }
00771 
00772 int main(int argc, char** argv) {
00773     ::testing::InitGoogleTest(&argc, argv);
00774     ::testing::AddGlobalTestEnvironment(server);
00775     return RUN_ALL_TESTS();
00776 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:06