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(DeleteTests, DeleteTest) { 00015 auto url = Url{base + "/delete.html"}; 00016 auto response = cpr::Delete(url); 00017 auto expected_text = std::string{"Delete success"}; 00018 EXPECT_EQ(expected_text, response.text); 00019 EXPECT_EQ(url, response.url); 00020 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00021 EXPECT_EQ(200, response.status_code); 00022 EXPECT_EQ(ErrorCode::OK, response.error.code); 00023 } 00024 00025 TEST(DeleteTests, DeleteUnallowedTest) { 00026 auto url = Url{base + "/delete_unallowed.html"}; 00027 auto response = cpr::Delete(url); 00028 auto expected_text = std::string{"Method unallowed"}; 00029 EXPECT_EQ(expected_text, response.text); 00030 EXPECT_EQ(url, response.url); 00031 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00032 EXPECT_EQ(405, response.status_code); 00033 EXPECT_EQ(ErrorCode::OK, response.error.code); 00034 } 00035 00036 TEST(DeleteTests, DeleteJsonBodyTest) { 00037 auto url = Url{base + "/delete.html"}; 00038 auto response = cpr::Delete(url, cpr::Body{"'foo': 'bar'"}, cpr::Header{{"Content-Type", "application/json"}}); 00039 auto expected_text = std::string{"'foo': 'bar'"}; 00040 EXPECT_EQ(expected_text, response.text); 00041 EXPECT_EQ(url, response.url); 00042 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]); 00043 EXPECT_EQ(200, response.status_code); 00044 EXPECT_EQ(ErrorCode::OK, response.error.code); 00045 } 00046 00047 TEST(DeleteTests, SessionDeleteTest) { 00048 auto url = Url{base + "/delete.html"}; 00049 Session session; 00050 session.SetUrl(url); 00051 auto response = session.Delete(); 00052 auto expected_text = std::string{"Delete success"}; 00053 EXPECT_EQ(expected_text, response.text); 00054 EXPECT_EQ(url, response.url); 00055 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00056 EXPECT_EQ(200, response.status_code); 00057 EXPECT_EQ(ErrorCode::OK, response.error.code); 00058 } 00059 00060 TEST(DeleteTests, SessionDeleteUnallowedTest) { 00061 auto url = Url{base + "/delete_unallowed.html"}; 00062 Session session; 00063 session.SetUrl(url); 00064 auto response = session.Delete(); 00065 auto expected_text = std::string{"Method unallowed"}; 00066 EXPECT_EQ(expected_text, response.text); 00067 EXPECT_EQ(url, response.url); 00068 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00069 EXPECT_EQ(405, response.status_code); 00070 EXPECT_EQ(ErrorCode::OK, response.error.code); 00071 } 00072 00073 TEST(DeleteTests, SessionDeleteJsonBodyTest) { 00074 auto url = Url{base + "/delete.html"}; 00075 Session session; 00076 session.SetUrl(url); 00077 session.SetHeader(cpr::Header{{"Content-Type", "application/json"}}); 00078 session.SetBody(cpr::Body{"{'foo': 'bar'}"}); 00079 auto response = session.Delete(); 00080 auto expected_text = std::string{"{'foo': 'bar'}"}; 00081 EXPECT_EQ(expected_text, response.text); 00082 EXPECT_EQ(url, response.url); 00083 EXPECT_EQ(std::string{"application/json"}, response.header["content-type"]); 00084 EXPECT_EQ(200, response.status_code); 00085 EXPECT_EQ(ErrorCode::OK, response.error.code); 00086 } 00087 00088 TEST(DeleteTests, SessionDeleteAfterGetTest) { 00089 Session session; 00090 { 00091 auto url = Url{base + "/get.html"}; 00092 session.SetUrl(url); 00093 auto response = session.Get(); 00094 } 00095 auto url = Url{base + "/delete.html"}; 00096 session.SetUrl(url); 00097 auto response = session.Delete(); 00098 auto expected_text = std::string{"Delete success"}; 00099 EXPECT_EQ(expected_text, response.text); 00100 EXPECT_EQ(url, response.url); 00101 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00102 EXPECT_EQ(200, response.status_code); 00103 EXPECT_EQ(ErrorCode::OK, response.error.code); 00104 } 00105 00106 TEST(DeleteTests, SessionDeleteUnallowedAfterGetTest) { 00107 Session session; 00108 { 00109 auto url = Url{base + "/get.html"}; 00110 session.SetUrl(url); 00111 auto response = session.Get(); 00112 } 00113 auto url = Url{base + "/delete_unallowed.html"}; 00114 session.SetUrl(url); 00115 auto response = session.Delete(); 00116 auto expected_text = std::string{"Method unallowed"}; 00117 EXPECT_EQ(expected_text, response.text); 00118 EXPECT_EQ(url, response.url); 00119 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00120 EXPECT_EQ(405, response.status_code); 00121 EXPECT_EQ(ErrorCode::OK, response.error.code); 00122 } 00123 00124 TEST(DeleteTests, SessionDeleteAfterHeadTest) { 00125 Session session; 00126 { 00127 auto url = Url{base + "/get.html"}; 00128 session.SetUrl(url); 00129 auto response = session.Head(); 00130 } 00131 auto url = Url{base + "/delete.html"}; 00132 session.SetUrl(url); 00133 auto response = session.Delete(); 00134 auto expected_text = std::string{"Delete success"}; 00135 EXPECT_EQ(expected_text, response.text); 00136 EXPECT_EQ(url, response.url); 00137 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00138 EXPECT_EQ(200, response.status_code); 00139 EXPECT_EQ(ErrorCode::OK, response.error.code); 00140 } 00141 00142 TEST(DeleteTests, SessionDeleteUnallowedAfterHeadTest) { 00143 Session session; 00144 { 00145 auto url = Url{base + "/get.html"}; 00146 session.SetUrl(url); 00147 auto response = session.Head(); 00148 } 00149 auto url = Url{base + "/delete_unallowed.html"}; 00150 session.SetUrl(url); 00151 auto response = session.Delete(); 00152 auto expected_text = std::string{"Method unallowed"}; 00153 EXPECT_EQ(expected_text, response.text); 00154 EXPECT_EQ(url, response.url); 00155 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00156 EXPECT_EQ(405, response.status_code); 00157 EXPECT_EQ(ErrorCode::OK, response.error.code); 00158 } 00159 00160 TEST(DeleteTests, SessionDeleteAfterPostTest) { 00161 Session session; 00162 { 00163 auto url = Url{base + "/url_post.html"}; 00164 auto payload = Payload{{"x", "5"}}; 00165 session.SetUrl(url); 00166 auto response = session.Post(); 00167 } 00168 auto url = Url{base + "/delete.html"}; 00169 session.SetUrl(url); 00170 auto response = session.Delete(); 00171 auto expected_text = std::string{"Delete success"}; 00172 EXPECT_EQ(expected_text, response.text); 00173 EXPECT_EQ(url, response.url); 00174 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00175 EXPECT_EQ(200, response.status_code); 00176 EXPECT_EQ(ErrorCode::OK, response.error.code); 00177 } 00178 00179 TEST(DeleteTests, SessionDeleteUnallowedAfterPostTest) { 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 + "/delete_unallowed.html"}; 00188 session.SetUrl(url); 00189 auto response = session.Delete(); 00190 auto expected_text = std::string{"Method unallowed"}; 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(405, response.status_code); 00195 EXPECT_EQ(ErrorCode::OK, response.error.code); 00196 } 00197 00198 TEST(DeleteTests, AsyncDeleteTest) { 00199 auto url = Url{base + "/delete.html"}; 00200 auto future_response = cpr::DeleteAsync(url); 00201 auto response = future_response.get(); 00202 auto expected_text = std::string{"Delete success"}; 00203 EXPECT_EQ(expected_text, response.text); 00204 EXPECT_EQ(url, response.url); 00205 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00206 EXPECT_EQ(200, response.status_code); 00207 EXPECT_EQ(ErrorCode::OK, response.error.code); 00208 } 00209 00210 TEST(DeleteTests, AsyncDeleteUnallowedTest) { 00211 auto url = Url{base + "/delete_unallowed.html"}; 00212 auto future_response = cpr::DeleteAsync(url); 00213 auto response = future_response.get(); 00214 auto expected_text = std::string{"Method unallowed"}; 00215 EXPECT_EQ(expected_text, response.text); 00216 EXPECT_EQ(url, response.url); 00217 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00218 EXPECT_EQ(405, response.status_code); 00219 EXPECT_EQ(ErrorCode::OK, response.error.code); 00220 } 00221 00222 TEST(DeleteTests, AsyncMultipleDeleteTest) { 00223 auto url = Url{base + "/delete.html"}; 00224 std::vector<AsyncResponse> responses; 00225 for (int i = 0; i < 10; ++i) { 00226 responses.emplace_back(cpr::DeleteAsync(url)); 00227 } 00228 for (auto& future_response : responses) { 00229 auto response = future_response.get(); 00230 auto expected_text = std::string{"Delete success"}; 00231 EXPECT_EQ(expected_text, response.text); 00232 EXPECT_EQ(url, response.url); 00233 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00234 EXPECT_EQ(200, response.status_code); 00235 EXPECT_EQ(ErrorCode::OK, response.error.code); 00236 } 00237 } 00238 00239 TEST(DeleteTests, AsyncMultipleDeleteUnallowedTest) { 00240 auto url = Url{base + "/delete_unallowed.html"}; 00241 std::vector<AsyncResponse> responses; 00242 for (int i = 0; i < 10; ++i) { 00243 responses.emplace_back(cpr::DeleteAsync(url)); 00244 } 00245 for (auto& future_response : responses) { 00246 auto response = future_response.get(); 00247 auto expected_text = std::string{"Method unallowed"}; 00248 EXPECT_EQ(expected_text, response.text); 00249 EXPECT_EQ(url, response.url); 00250 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]); 00251 EXPECT_EQ(405, response.status_code); 00252 EXPECT_EQ(ErrorCode::OK, response.error.code); 00253 } 00254 } 00255 00256 int main(int argc, char** argv) { 00257 ::testing::InitGoogleTest(&argc, argv); 00258 ::testing::AddGlobalTestEnvironment(server); 00259 return RUN_ALL_TESTS(); 00260 }