Aktualizr
C++ SOTA Client
httpclient_test.cc
1 #include <gtest/gtest.h>
2 
3 #include <errno.h>
4 #include <stdio.h>
5 #include <cstdlib>
6 
7 #include <boost/process.hpp>
8 
9 #include "json/json.h"
10 
11 #include "http/httpclient.h"
12 #include "libaktualizr/types.h"
13 #include "test_utils.h"
14 #include "utilities/utils.h"
15 
16 static std::string server = "http://127.0.0.1:";
17 
18 TEST(CopyConstructorTest, copied) {
19  HttpClient http;
20  HttpClient http_copy(http);
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);
24 }
25 
26 TEST(GetTest, get_performed) {
27  HttpClient http;
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);
31 }
32 
33 TEST(GetTestWithHeaders, get_performed) {
34  std::vector<std::string> headers = {"Authorization: Bearer token"};
35  HttpClient http(&headers);
36  std::string path = "/auth_call";
37  Json::Value response = http.get(server + path, HttpInterface::kNoLimit).getJson();
38  EXPECT_EQ(response["status"].asString(), "good");
39 }
40 
41 /* Reject http GET responses that exceed size limit. */
42 TEST(GetTest, download_size_limit) {
43  HttpClient http;
44  std::string path = "/large_file";
45  HttpResponse resp = http.get(server + path, 1024);
46  std::cout << "RESP SIZE " << resp.body.length() << std::endl;
47  EXPECT_EQ(resp.curl_code, CURLE_FILESIZE_EXCEEDED);
48 }
49 
50 /* Reject http GET responses that do not meet speed limit. */
51 TEST(GetTest, download_speed_limit) {
52  HttpClient http;
53  std::string path = "/slow_file";
54 
55  http.overrideSpeedLimitParams(3, 5000);
56  HttpResponse resp = http.get(server + path, HttpInterface::kNoLimit);
57  EXPECT_EQ(resp.curl_code, CURLE_OPERATION_TIMEDOUT);
58 }
59 
60 TEST(PostTest, post_performed) {
61  HttpClient http;
62  std::string path = "/path/1/2/3";
63  Json::Value data;
64  data["key"] = "val";
65 
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");
69 }
70 
71 TEST(PostTest, put_performed) {
72  HttpClient http;
73  std::string path = "/path/1/2/3";
74  Json::Value data;
75  data["key"] = "val";
76 
77  Json::Value json = http.put(server + path, data).getJson();
78 
79  EXPECT_EQ(json["path"].asString(), path);
80  EXPECT_EQ(json["data"]["key"].asString(), "val");
81 }
82 
83 TEST(HttpClient, user_agent) {
84  {
85  // test the default, when setUserAgent hasn't been called yet
86  HttpClient http;
87 
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");
91  }
92 
93  Utils::setUserAgent("blah");
94 
95  {
96  HttpClient http;
97 
98  auto resp = http.get(server + "/user_agent", HttpInterface::kNoLimit);
99  EXPECT_EQ(resp.body, "blah");
100  }
101 }
102 
103 TEST(Headers, update_header) {
104  std::vector<std::string> headers = {"Authorization: Bearer bad"};
105  HttpClient http(&headers);
106 
107  ASSERT_FALSE(http.updateHeader("NOSUCHHEADER", "foo"));
108 
109  std::string path = "/auth_call";
110  std::string body = http.get(server + path, HttpInterface::kNoLimit).body;
111  EXPECT_EQ(body, "{}");
112 
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");
116 }
117 
118 // TODO(OTA-4546): add tests for HttpClient::download
119 
120 #ifndef __NO_MAIN__
121 int main(int argc, char** argv) {
122  ::testing::InitGoogleTest(&argc, argv);
123 
124  std::string port = TestUtils::getFreePort();
125  server += port;
126  boost::process::child server_process("tests/fake_http_server/fake_test_server.py", port, "-f");
127  TestUtils::waitForServer(server + "/");
128 
129  return RUN_ALL_TESTS();
130 }
131 #endif
types.h
data
General data structures.
Definition: types.h:217
HttpResponse
Definition: httpinterface.h:17
HttpClient
Definition: httpclient.h:28