00001 #include <gtest/gtest.h>
00002
00003 #include <cstdio>
00004 #include <fstream>
00005 #include <string>
00006
00007 #include <cpr/cpr.h>
00008
00009 #include "cpr/multipart.h"
00010 #include "server.h"
00011
00012 using namespace cpr;
00013
00014 static Server* server = new Server();
00015 auto base = server->GetBaseUrl();
00016
00017
00018 TEST(BodyPostTests, DefaultUrlEncodedPostTest) {
00019 auto url = Url{base + "/url_post.html"};
00020 auto response = cpr::Post(url, Body{"x=5"});
00021 auto expected_text = std::string{"{\n"
00022 " \"x\": 5\n"
00023 "}"};
00024 EXPECT_EQ(expected_text, response.text);
00025 EXPECT_EQ(url, response.url);
00026 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00027 EXPECT_EQ(201, response.status_code);
00028 EXPECT_EQ(ErrorCode::OK, response.error.code);
00029 }
00030
00031 TEST(BodyPostTests, TextUrlEncodedPostTest) {
00032 auto url = Url{base + "/url_post.html"};
00033 auto response = cpr::Post(url, Body{"x=hello world!!~"});
00034 auto expected_text = std::string{"{\n"
00035 " \"x\": hello world!!~\n"
00036 "}"};
00037 EXPECT_EQ(expected_text, response.text);
00038 EXPECT_EQ(url, response.url);
00039 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00040 EXPECT_EQ(201, response.status_code);
00041 EXPECT_EQ(ErrorCode::OK, response.error.code);
00042 }
00043
00044 TEST(BodyPostTests, TextUrlEncodedNoCopyPostTest) {
00045 auto url = Url{base + "/url_post.html"};
00046 auto body = Body{"x=hello world!!~"};
00047
00048 auto response = cpr::Post(url, body);
00049 auto expected_text = std::string{"{\n"
00050 " \"x\": hello world!!~\n"
00051 "}"};
00052 EXPECT_EQ(expected_text, response.text);
00053 EXPECT_EQ(url, response.url);
00054 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00055 EXPECT_EQ(201, response.status_code);
00056 EXPECT_EQ(ErrorCode::OK, response.error.code);
00057 }
00058
00059 TEST(BodyPostTests, UrlEncodedManyPostTest) {
00060 auto url = Url{base + "/url_post.html"};
00061 auto response = cpr::Post(url, Body{"x=5&y=13"});
00062 auto expected_text = std::string{"{\n"
00063 " \"x\": 5,\n"
00064 " \"y\": 13,\n"
00065 " \"sum\": 18\n"
00066 "}"};
00067 EXPECT_EQ(expected_text, response.text);
00068 EXPECT_EQ(url, response.url);
00069 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00070 EXPECT_EQ(201, response.status_code);
00071 EXPECT_EQ(ErrorCode::OK, response.error.code);
00072 }
00073
00074 TEST(BodyPostTests, CustomHeaderNumberPostTest) {
00075 auto url = Url{base + "/json_post.html"};
00076 auto response = cpr::Post(url, Body{"{\"x\":5}"}, Header{{"Content-Type", "application/json"}});
00077 auto expected_text = std::string{"{\"x\":5}"};
00078 EXPECT_EQ(expected_text, response.text);
00079 EXPECT_EQ(url, response.url);
00080 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00081 EXPECT_EQ(201, response.status_code);
00082 EXPECT_EQ(ErrorCode::OK, response.error.code);
00083 }
00084
00085 TEST(BodyPostTests, CustomHeaderTextPostTest) {
00086 auto url = Url{base + "/json_post.html"};
00087 auto response = cpr::Post(url, Body{"{\"x\":\"hello world!!~\"}"},
00088 Header{{"Content-Type", "application/json"}});
00089 auto expected_text = std::string{"{\"x\":\"hello world!!~\"}"};
00090 EXPECT_EQ(expected_text, response.text);
00091 EXPECT_EQ(url, response.url);
00092 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]);
00093 EXPECT_EQ(201, response.status_code);
00094 EXPECT_EQ(ErrorCode::OK, response.error.code);
00095 }
00096
00097 TEST(BodyPostTests, CustomWrongHeaderPostTest) {
00098 auto url = Url{base + "/json_post.html"};
00099 auto response = cpr::Post(url, Body{"{\"x\":5}"}, Header{{"Content-Type", "text/plain"}});
00100 auto expected_text = std::string{"Unsupported Media Type"};
00101 EXPECT_EQ(expected_text, response.text);
00102 EXPECT_EQ(url, response.url);
00103 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00104 EXPECT_EQ(415, response.status_code);
00105 EXPECT_EQ(ErrorCode::OK, response.error.code);
00106 }
00107
00108 TEST(BodyPostTests, UrlPostBadHostTest) {
00109 auto url = Url{"http://bad_host/"};
00110 auto response = cpr::Post(url, Body{"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(BodyPostTests, StringMoveBodyTest) {
00119 auto url = Url{base + "/url_post.html"};
00120 auto response = cpr::Post(url, Body{std::string{"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 int main(int argc, char** argv) {
00132 ::testing::InitGoogleTest(&argc, argv);
00133 ::testing::AddGlobalTestEnvironment(server);
00134 return RUN_ALL_TESTS();
00135 }