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(HeadTests, BasicHeadTest) {
00015 auto url = Url{base + "/hello.html"};
00016 auto response = cpr::Head(url);
00017 EXPECT_EQ(std::string{}, response.text);
00018 EXPECT_EQ(url, response.url);
00019 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00020 EXPECT_EQ(200, response.status_code);
00021 EXPECT_EQ(ErrorCode::OK, response.error.code);
00022 }
00023
00024 TEST(HeadTests, ComplexHeadTest) {
00025 auto url = Url{base + "/basic.json"};
00026 auto response = cpr::Head(url);
00027 EXPECT_EQ(std::string{}, response.text);
00028 EXPECT_EQ(url, response.url);
00029 EXPECT_EQ(std::string{"application/octet-stream"}, response.header["content-type"]);
00030 EXPECT_EQ(200, response.status_code);
00031 EXPECT_EQ(ErrorCode::OK, response.error.code);
00032 }
00033
00034 TEST(HeadTests, ResourceNotFoundHeadTest) {
00035 auto url = Url{base + "/error.html"};
00036 auto response = cpr::Head(url);
00037 EXPECT_EQ(std::string{}, response.text);
00038 EXPECT_EQ(url, response.url);
00039 EXPECT_EQ(std::string{"text/plain"}, response.header["content-type"]);
00040 EXPECT_EQ(404, response.status_code);
00041 EXPECT_EQ(ErrorCode::OK, response.error.code);
00042 }
00043
00044 TEST(HeadTests, BadHostHeadTest) {
00045 auto url = Url{"http://bad_host/"};
00046 auto response = cpr::Head(url);
00047 EXPECT_EQ(std::string{}, response.text);
00048 EXPECT_EQ(url, response.url);
00049 EXPECT_EQ(0, response.status_code);
00050 EXPECT_EQ(ErrorCode::HOST_RESOLUTION_FAILURE, response.error.code);
00051 }
00052
00053 TEST(HeadTests, CookieHeadTest) {
00054 auto url = Url{base + "/basic_cookies.html"};
00055 auto cookies = Cookies{{"hello", "world"}, {"my", "another; fake=cookie;"}};
00056 auto response = cpr::Head(url, cookies);
00057 EXPECT_EQ(std::string{}, response.text);
00058 EXPECT_EQ(url, response.url);
00059 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00060 EXPECT_EQ(200, response.status_code);
00061 EXPECT_EQ(ErrorCode::OK, response.error.code);
00062 cookies = response.cookies;
00063 EXPECT_EQ(cookies["cookie"], response.cookies["cookie"]);
00064 EXPECT_EQ(cookies["icecream"], response.cookies["icecream"]);
00065 EXPECT_EQ(cookies["expires"], response.cookies["expires"]);
00066 }
00067
00068 TEST(HeadTests, ParameterHeadTest) {
00069 auto url = Url{base + "/hello.html"};
00070 auto parameters = Parameters{{"key", "value"}};
00071 auto response = cpr::Head(url, parameters);
00072 EXPECT_EQ(std::string{}, response.text);
00073 EXPECT_EQ(Url{url + "?key=value"}, response.url);
00074 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00075 EXPECT_EQ(200, response.status_code);
00076 EXPECT_EQ(ErrorCode::OK, response.error.code);
00077 }
00078
00079 TEST(HeadTests, AuthenticationSuccessHeadTest) {
00080 auto url = Url{base + "/basic_auth.html"};
00081 auto response = cpr::Head(url, Authentication{"user", "password"});
00082 EXPECT_EQ(std::string{}, response.text);
00083 EXPECT_EQ(url, response.url);
00084 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00085 EXPECT_EQ(200, response.status_code);
00086 EXPECT_EQ(ErrorCode::OK, response.error.code);
00087 }
00088
00089 TEST(HeadTests, AuthenticationNullFailureHeadTest) {
00090 auto url = Url{base + "/basic_auth.html"};
00091 auto response = cpr::Head(url);
00092 EXPECT_EQ(std::string{}, response.text);
00093 EXPECT_EQ(url, response.url);
00094 EXPECT_EQ(std::string{}, response.header["content-type"]);
00095 EXPECT_EQ(401, response.status_code);
00096 EXPECT_EQ(ErrorCode::OK, response.error.code);
00097 }
00098
00099 TEST(HeadTests, AuthenticationFailureHeadTest) {
00100 auto url = Url{base + "/basic_auth.html"};
00101 auto response = cpr::Head(url, Authentication{"user", "bad_password"});
00102 EXPECT_EQ(std::string{}, response.text);
00103 EXPECT_EQ(url, response.url);
00104 EXPECT_EQ(std::string{}, response.header["content-type"]);
00105 EXPECT_EQ(401, response.status_code);
00106 EXPECT_EQ(ErrorCode::OK, response.error.code);
00107 }
00108
00109 TEST(HeadTests, DISABLED_DigestSuccessHeadTest) {
00110 auto url = Url{base + "/digest_auth.html"};
00111 auto response = cpr::Head(url, Digest{"user", "password"});
00112 EXPECT_EQ(std::string{}, response.text);
00113 EXPECT_EQ(url, response.url);
00114 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00115 EXPECT_EQ(200, response.status_code);
00116 EXPECT_EQ(ErrorCode::OK, response.error.code);
00117 }
00118
00119 TEST(HeadTests, HeaderReflectNoneHeadTest) {
00120 auto url = Url{base + "/header_reflect.html"};
00121 auto response = cpr::Head(url);
00122 EXPECT_EQ(std::string{}, response.text);
00123 EXPECT_EQ(url, response.url);
00124 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00125 EXPECT_EQ(std::string{}, response.header["hello"]);
00126 EXPECT_EQ(200, response.status_code);
00127 EXPECT_EQ(ErrorCode::OK, response.error.code);
00128 }
00129
00130 TEST(HeadTests, HeaderReflectEmptyHeadTest) {
00131 auto url = Url{base + "/header_reflect.html"};
00132 auto response = cpr::Head(url, Header{});
00133 EXPECT_EQ(std::string{}, response.text);
00134 EXPECT_EQ(url, response.url);
00135 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00136 EXPECT_EQ(std::string{}, response.header["hello"]);
00137 EXPECT_EQ(200, response.status_code);
00138 EXPECT_EQ(ErrorCode::OK, response.error.code);
00139 }
00140
00141 TEST(HeadTests, HeaderReflectHeadTest) {
00142 auto url = Url{base + "/header_reflect.html"};
00143 auto response = cpr::Head(url, Header{{"hello", "world"}});
00144 EXPECT_EQ(std::string{}, response.text);
00145 EXPECT_EQ(url, response.url);
00146 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00147 EXPECT_EQ(std::string{"world"}, response.header["hello"]);
00148 EXPECT_EQ(200, response.status_code);
00149 EXPECT_EQ(ErrorCode::OK, response.error.code);
00150 }
00151
00152 TEST(HeadTests, SetEmptyHeaderHeadTest) {
00153 auto url = Url{base + "/header_reflect.html"};
00154 auto response = cpr::Head(url, Header{{"hello", ""}});
00155 EXPECT_EQ(std::string{}, response.text);
00156 EXPECT_EQ(url, response.url);
00157 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00158 EXPECT_EQ(std::string{}, response.header["hello"]);
00159 EXPECT_EQ(200, response.status_code);
00160 EXPECT_EQ(ErrorCode::OK, response.error.code);
00161 }
00162
00163 TEST(HeadTests, RedirectHeadTest) {
00164 auto url = Url{base + "/temporary_redirect.html"};
00165 auto response = cpr::Head(url, false);
00166 EXPECT_EQ(std::string{}, response.text);
00167 EXPECT_EQ(url, response.url);
00168 EXPECT_EQ(std::string{}, response.header["content-type"]);
00169 EXPECT_EQ(302, response.status_code);
00170 EXPECT_EQ(ErrorCode::OK, response.error.code);
00171 }
00172
00173 TEST(HeadTests, ZeroMaxRedirectsHeadTest) {
00174 auto url = Url{base + "/hello.html"};
00175 auto response = cpr::Head(url, 0L);
00176 EXPECT_EQ(std::string{}, response.text);
00177 EXPECT_EQ(url, response.url);
00178 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00179 EXPECT_EQ(200, response.status_code);
00180 EXPECT_EQ(ErrorCode::OK, response.error.code);
00181 }
00182
00183 TEST(HeadTests, BasicHeadAsyncTest) {
00184 auto url = Url{base + "/hello.html"};
00185 std::vector<AsyncResponse> responses;
00186 for (int i = 0; i < 10; ++i) {
00187 responses.emplace_back(cpr::HeadAsync(url));
00188 }
00189 for (auto& future_response : responses) {
00190 auto response = future_response.get();
00191 EXPECT_EQ(std::string{}, response.text);
00192 EXPECT_EQ(url, response.url);
00193 EXPECT_EQ(std::string{"text/html"}, response.header["content-type"]);
00194 EXPECT_EQ(200, response.status_code);
00195 EXPECT_EQ(ErrorCode::OK, response.error.code);
00196 }
00197 }
00198
00199 int main(int argc, char** argv) {
00200 ::testing::InitGoogleTest(&argc, argv);
00201 ::testing::AddGlobalTestEnvironment(server);
00202 return RUN_ALL_TESTS();
00203 }