1 #include <gtest/gtest.h> 7 #include <boost/process.hpp> 11 #include "http/httpclient.h" 12 #include "test_utils.h" 14 #include "utilities/utils.h" 16 static std::string server =
"http://127.0.0.1:";
18 TEST(CopyConstructorTest, copied) {
21 std::string path =
"/path/1/2/3";
22 Json::Value resp = http_copy.get(server + path, HttpInterface::kNoLimit).getJson();
23 EXPECT_EQ(resp[
"path"].asString(), path);
26 TEST(GetTest, get_performed) {
28 std::string path =
"/path/1/2/3";
29 Json::Value response = http.get(server + path, HttpInterface::kNoLimit).getJson();
30 EXPECT_EQ(response[
"path"].asString(), path);
33 TEST(GetTestWithHeaders, get_performed) {
34 std::vector<std::string> headers = {
"Authorization: Bearer token"};
36 std::string path =
"/auth_call";
37 Json::Value response = http.get(server + path, HttpInterface::kNoLimit).getJson();
38 EXPECT_EQ(response[
"status"].asString(),
"good");
42 TEST(GetTest, download_size_limit) {
44 std::string path =
"/large_file";
46 std::cout <<
"RESP SIZE " << resp.body.length() << std::endl;
47 EXPECT_EQ(resp.curl_code, CURLE_FILESIZE_EXCEEDED);
51 TEST(GetTest, download_speed_limit) {
53 std::string path =
"/slow_file";
55 http.overrideSpeedLimitParams(3, 5000);
56 HttpResponse resp = http.get(server + path, HttpInterface::kNoLimit);
57 EXPECT_EQ(resp.curl_code, CURLE_OPERATION_TIMEDOUT);
60 TEST(PostTest, post_performed) {
62 std::string path =
"/path/1/2/3";
66 Json::Value response = http.post(server + path, data).getJson();
67 EXPECT_EQ(response[
"path"].asString(), path);
68 EXPECT_EQ(response[
"data"][
"key"].asString(),
"val");
71 TEST(PostTest, put_performed) {
73 std::string path =
"/path/1/2/3";
77 Json::Value json = http.put(server + path, data).getJson();
79 EXPECT_EQ(json[
"path"].asString(), path);
80 EXPECT_EQ(json[
"data"][
"key"].asString(),
"val");
88 const auto resp = http.get(server +
"/user_agent", HttpInterface::kNoLimit);
89 const auto app = resp.body.substr(0, resp.body.find(
'/'));
90 EXPECT_EQ(app,
"Aktualizr");
93 Utils::setUserAgent(
"blah");
98 auto resp = http.get(server +
"/user_agent", HttpInterface::kNoLimit);
99 EXPECT_EQ(resp.body,
"blah");
103 TEST(Headers, update_header) {
104 std::vector<std::string> headers = {
"Authorization: Bearer bad"};
107 ASSERT_FALSE(http.updateHeader(
"NOSUCHHEADER",
"foo"));
109 std::string path =
"/auth_call";
110 std::string body = http.get(server + path, HttpInterface::kNoLimit).body;
111 EXPECT_EQ(body,
"{}");
113 ASSERT_TRUE(http.updateHeader(
"Authorization",
"Bearer token"));
114 Json::Value response = http.get(server + path, HttpInterface::kNoLimit).getJson();
115 EXPECT_EQ(response[
"status"].asString(),
"good");
121 int main(
int argc,
char** argv) {
122 ::testing::InitGoogleTest(&argc, argv);
124 std::string port = TestUtils::getFreePort();
126 boost::process::child server_process(
"tests/fake_http_server/fake_test_server.py", port,
"-f");
127 TestUtils::waitForServer(server +
"/");
129 return RUN_ALL_TESTS();