00001 #include <gtest/gtest.h> 00002 00003 #include <string> 00004 00005 #include <cpr/cpr.h> 00006 00007 #include "server.h" 00008 00009 using namespace cpr; 00010 00011 static Server* server = new Server(); 00012 auto base = server->GetBaseUrl(); 00013 00014 TEST(PutTests, PutTest) { 00015 auto url = Url{base + "/put.html"}; 00016 auto payload = Payload{{"x", "5"}}; 00017 auto response = cpr::Put(url, payload); 00018 auto expected_text = std::string{"{\n" 00019 " \"x\": 5\n" 00020 "}"}; 00021 EXPECT_EQ(expected_text, response.text); 00022 EXPECT_EQ(url, response.url); 00023 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]); 00024 EXPECT_EQ(200, response.status_code); 00025 EXPECT_EQ(ErrorCode::OK, response.error.code); 00026 } 00027 00028 TEST(PutTests, PutUnallowedTest) { 00029 auto url = Url{base + "/put_unallowed.html"}; 00030 auto payload = Payload{{"x", "5"}}; 00031 auto response = cpr::Put(url, payload); 00032 auto expected_text = std::string{"Method unallowed"}; 00033 EXPECT_EQ(expected_text, response.text); 00034 EXPECT_EQ(url, response.url); 00035 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00036 EXPECT_EQ(405, response.status_code); 00037 EXPECT_EQ(ErrorCode::OK, response.error.code); 00038 } 00039 00040 TEST(PutTests, SessionPutTest) { 00041 auto url = Url{base + "/put.html"}; 00042 auto payload = Payload{{"x", "5"}}; 00043 Session session; 00044 session.SetUrl(url); 00045 session.SetPayload(payload); 00046 auto response = session.Put(); 00047 auto expected_text = std::string{"{\n" 00048 " \"x\": 5\n" 00049 "}"}; 00050 EXPECT_EQ(expected_text, response.text); 00051 EXPECT_EQ(url, response.url); 00052 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]); 00053 EXPECT_EQ(200, response.status_code); 00054 EXPECT_EQ(ErrorCode::OK, response.error.code); 00055 } 00056 00057 TEST(PutTests, SessionPutUnallowedTest) { 00058 auto url = Url{base + "/put_unallowed.html"}; 00059 auto payload = Payload{{"x", "5"}}; 00060 Session session; 00061 session.SetUrl(url); 00062 session.SetPayload(payload); 00063 auto response = session.Put(); 00064 auto expected_text = std::string{"Method unallowed"}; 00065 EXPECT_EQ(expected_text, response.text); 00066 EXPECT_EQ(url, response.url); 00067 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00068 EXPECT_EQ(405, response.status_code); 00069 EXPECT_EQ(ErrorCode::OK, response.error.code); 00070 } 00071 00072 TEST(PutTests, SessionPutAfterGetTest) { 00073 Session session; 00074 { 00075 auto url = Url{base + "/get.html"}; 00076 session.SetUrl(url); 00077 auto response = session.Get(); 00078 } 00079 auto url = Url{base + "/put.html"}; 00080 auto payload = Payload{{"x", "5"}}; 00081 session.SetUrl(url); 00082 session.SetPayload(payload); 00083 auto response = session.Put(); 00084 auto expected_text = std::string{"{\n" 00085 " \"x\": 5\n" 00086 "}"}; 00087 EXPECT_EQ(expected_text, response.text); 00088 EXPECT_EQ(url, response.url); 00089 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]); 00090 EXPECT_EQ(200, response.status_code); 00091 EXPECT_EQ(ErrorCode::OK, response.error.code); 00092 } 00093 00094 TEST(PutTests, SessionPutUnallowedAfterGetTest) { 00095 Session session; 00096 { 00097 auto url = Url{base + "/get.html"}; 00098 session.SetUrl(url); 00099 auto response = session.Get(); 00100 } 00101 auto url = Url{base + "/put_unallowed.html"}; 00102 auto payload = Payload{{"x", "5"}}; 00103 session.SetUrl(url); 00104 session.SetPayload(payload); 00105 auto response = session.Put(); 00106 auto expected_text = std::string{"Method unallowed"}; 00107 EXPECT_EQ(expected_text, response.text); 00108 EXPECT_EQ(url, response.url); 00109 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00110 EXPECT_EQ(405, response.status_code); 00111 EXPECT_EQ(ErrorCode::OK, response.error.code); 00112 } 00113 00114 TEST(PutTests, SessionPutAfterHeadTest) { 00115 Session session; 00116 { 00117 auto url = Url{base + "/get.html"}; 00118 session.SetUrl(url); 00119 auto response = session.Head(); 00120 } 00121 auto url = Url{base + "/put.html"}; 00122 auto payload = Payload{{"x", "5"}}; 00123 session.SetUrl(url); 00124 session.SetPayload(payload); 00125 auto response = session.Put(); 00126 auto expected_text = std::string{"{\n" 00127 " \"x\": 5\n" 00128 "}"}; 00129 EXPECT_EQ(expected_text, response.text); 00130 EXPECT_EQ(url, response.url); 00131 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]); 00132 EXPECT_EQ(200, response.status_code); 00133 EXPECT_EQ(ErrorCode::OK, response.error.code); 00134 } 00135 00136 TEST(PutTests, SessionPutUnallowedAfterHeadTest) { 00137 Session session; 00138 { 00139 auto url = Url{base + "/get.html"}; 00140 session.SetUrl(url); 00141 auto response = session.Head(); 00142 } 00143 auto url = Url{base + "/put_unallowed.html"}; 00144 auto payload = Payload{{"x", "5"}}; 00145 session.SetUrl(url); 00146 session.SetPayload(payload); 00147 auto response = session.Put(); 00148 auto expected_text = std::string{"Method unallowed"}; 00149 EXPECT_EQ(expected_text, response.text); 00150 EXPECT_EQ(url, response.url); 00151 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00152 EXPECT_EQ(405, response.status_code); 00153 EXPECT_EQ(ErrorCode::OK, response.error.code); 00154 } 00155 00156 TEST(PutTests, SessionPutAfterPostTest) { 00157 Session session; 00158 { 00159 auto url = Url{base + "/url_post.html"}; 00160 auto payload = Payload{{"x", "5"}}; 00161 session.SetUrl(url); 00162 auto response = session.Post(); 00163 } 00164 auto url = Url{base + "/put.html"}; 00165 auto payload = Payload{{"x", "5"}}; 00166 session.SetUrl(url); 00167 session.SetPayload(payload); 00168 auto response = session.Put(); 00169 auto expected_text = std::string{"{\n" 00170 " \"x\": 5\n" 00171 "}"}; 00172 EXPECT_EQ(expected_text, response.text); 00173 EXPECT_EQ(url, response.url); 00174 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]); 00175 EXPECT_EQ(200, response.status_code); 00176 EXPECT_EQ(ErrorCode::OK, response.error.code); 00177 } 00178 00179 TEST(PutTests, SessionPutUnallowedAfterPostTest) { 00180 Session session; 00181 { 00182 auto url = Url{base + "/url_post.html"}; 00183 auto payload = Payload{{"x", "5"}}; 00184 session.SetUrl(url); 00185 auto response = session.Post(); 00186 } 00187 auto url = Url{base + "/put_unallowed.html"}; 00188 auto payload = Payload{{"x", "5"}}; 00189 session.SetUrl(url); 00190 session.SetPayload(payload); 00191 auto response = session.Put(); 00192 auto expected_text = std::string{"Method unallowed"}; 00193 EXPECT_EQ(expected_text, response.text); 00194 EXPECT_EQ(url, response.url); 00195 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00196 EXPECT_EQ(405, response.status_code); 00197 EXPECT_EQ(ErrorCode::OK, response.error.code); 00198 } 00199 00200 TEST(PutTests, AsyncPutTest) { 00201 auto url = Url{base + "/put.html"}; 00202 auto payload = Payload{{"x", "5"}}; 00203 auto future_response = cpr::PutAsync(url, payload); 00204 auto response = future_response.get(); 00205 auto expected_text = std::string{"{\n" 00206 " \"x\": 5\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(200, response.status_code); 00212 EXPECT_EQ(ErrorCode::OK, response.error.code); 00213 } 00214 00215 TEST(PutTests, AsyncPutUnallowedTest) { 00216 auto url = Url{base + "/put_unallowed.html"}; 00217 auto payload = Payload{{"x", "5"}}; 00218 auto future_response = cpr::PutAsync(url, payload); 00219 auto response = future_response.get(); 00220 auto expected_text = std::string{"Method unallowed"}; 00221 EXPECT_EQ(expected_text, response.text); 00222 EXPECT_EQ(url, response.url); 00223 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00224 EXPECT_EQ(405, response.status_code); 00225 EXPECT_EQ(ErrorCode::OK, response.error.code); 00226 } 00227 00228 TEST(PutTests, AsyncMultiplePutTest) { 00229 auto url = Url{base + "/put.html"}; 00230 auto payload = Payload{{"x", "5"}}; 00231 std::vector<AsyncResponse> responses; 00232 for (int i = 0; i < 10; ++i) { 00233 responses.emplace_back(cpr::PutAsync(url, payload)); 00234 } 00235 for (auto& future_response : responses) { 00236 auto response = future_response.get(); 00237 auto expected_text = std::string{"{\n" 00238 " \"x\": 5\n" 00239 "}"}; 00240 EXPECT_EQ(expected_text, response.text); 00241 EXPECT_EQ(url, response.url); 00242 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]); 00243 EXPECT_EQ(200, response.status_code); 00244 EXPECT_EQ(ErrorCode::OK, response.error.code); 00245 } 00246 } 00247 00248 TEST(PutTests, AsyncMultiplePutUnallowedTest) { 00249 auto url = Url{base + "/put_unallowed.html"}; 00250 auto payload = Payload{{"x", "5"}}; 00251 std::vector<AsyncResponse> responses; 00252 for (int i = 0; i < 10; ++i) { 00253 responses.emplace_back(cpr::PutAsync(url, payload)); 00254 } 00255 for (auto& future_response : responses) { 00256 auto response = future_response.get(); 00257 auto expected_text = std::string{"Method unallowed"}; 00258 EXPECT_EQ(expected_text, response.text); 00259 EXPECT_EQ(url, response.url); 00260 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00261 EXPECT_EQ(405, response.status_code); 00262 EXPECT_EQ(ErrorCode::OK, response.error.code); 00263 } 00264 } 00265 00266 int main(int argc, char** argv) { 00267 ::testing::InitGoogleTest(&argc, argv); 00268 ::testing::AddGlobalTestEnvironment(server); 00269 return RUN_ALL_TESTS(); 00270 }