Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tidy up HttpClient tests #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/libaktualizr/http/httpclient.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class HttpClient : public HttpInterface {
void timeout(int64_t ms);

private:
FRIEND_TEST(GetTest, download_speed_limit);
FRIEND_TEST(HttpClient, DownloadSpeedLimit);

static const CurlGlobalInitWrapper manageCurlGlobalInit_;
CURL *curl;
Expand Down
54 changes: 31 additions & 23 deletions src/libaktualizr/http/httpclient_test.cc
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
#include <gtest/gtest.h>

#include <errno.h>
#include <stdio.h>
#include <cstdlib>
#include "http/httpclient.h"

#include <atomic>
#include <boost/process.hpp>

#include "json/json.h"

#include <chrono>
#include <string>
#include <thread>
#include "http/httpclient.h"
#include "libaktualizr/types.h"

#include "json/json.h"
#include "logging/logging.h"
#include "test_utils.h"
#include "utilities/flow_control.h"
#include "utilities/utils.h"

// NOLINTNEXTLINE(*non-const*)
static std::string server = "http://127.0.0.1:";

TEST(CopyConstructorTest, copied) {
// NOLINTNEXTLINE(*non-const*)
TEST(HttpClient, CopyConstructorTest) {
HttpClient http;
HttpClient http_copy(http);
std::string path = "/path/1/2/3";
Json::Value resp = http_copy.get(server + path, HttpInterface::kNoLimit, nullptr).getJson();
EXPECT_EQ(resp["path"].asString(), path);
}

TEST(GetTest, get_performed) {
// NOLINTNEXTLINE(*non-const*)
TEST(HttpClient, Get) {
HttpClient http;
std::string path = "/path/1/2/3";
Json::Value response = http.get(server + path, HttpInterface::kNoLimit, nullptr).getJson();
EXPECT_EQ(response["path"].asString(), path);
}

TEST(GetTestWithHeaders, get_performed) {
// NOLINTNEXTLINE(*non-const*)
TEST(HttpClient, GetWithHeaders) {
std::vector<std::string> headers = {"Authorization: Bearer token"};
HttpClient http(&headers);
std::string path = "/auth_call";
Expand All @@ -41,7 +44,8 @@ TEST(GetTestWithHeaders, get_performed) {
}

/* Reject http GET responses that exceed size limit. */
TEST(GetTest, download_size_limit) {
// NOLINTNEXTLINE(*non-const*)
TEST(HttpClient, DownloadSizeLimit) {
HttpClient http;
std::string path = "/large_file";
HttpResponse resp = http.get(server + path, 1024, nullptr);
Expand All @@ -50,7 +54,8 @@ TEST(GetTest, download_size_limit) {
}

/* Reject http GET responses that do not meet speed limit. */
TEST(GetTest, download_speed_limit) {
// NOLINTNEXTLINE(*non-const*)
TEST(HttpClient, DownloadSpeedLimit) {
HttpClient http;
std::string path = "/slow_file";

Expand All @@ -59,13 +64,14 @@ TEST(GetTest, download_speed_limit) {
EXPECT_EQ(resp.curl_code, CURLE_OPERATION_TIMEDOUT);
}

TEST(GetTest, cancellation) {
// NOLINTNEXTLINE(*non-const*)
TEST(HttpClient, Cancellation) {
HttpClient http;
std::string path = "/slow_file";
api::FlowControlToken token;
std::atomic<bool> did_abort;
std::atomic<bool> did_abort{false};
auto end = std::chrono::steady_clock::now() + std::chrono::seconds(2);
std::thread t1([&token, end, &did_abort] {
std::thread abort_thread([&token, end, &did_abort] {
std::this_thread::sleep_until(end);
token.setAbort();
did_abort = true;
Expand All @@ -80,10 +86,11 @@ TEST(GetTest, cancellation) {
EXPECT_LE(0, diff);
EXPECT_LE(diff, 3000);
EXPECT_EQ(resp.curl_code, CURLE_ABORTED_BY_CALLBACK);
t1.join();
abort_thread.join();
}

TEST(PostTest, post_performed) {
// NOLINTNEXTLINE(*non-const*)
TEST(HttpClient, Post) {
HttpClient http;
std::string path = "/path/1/2/3";
Json::Value data;
Expand All @@ -94,7 +101,8 @@ TEST(PostTest, post_performed) {
EXPECT_EQ(response["data"]["key"].asString(), "val");
}

TEST(PostTest, put_performed) {
// NOLINTNEXTLINE(*non-const*)
TEST(HttpClient, Put) {
HttpClient http;
std::string path = "/path/1/2/3";
Json::Value data;
Expand All @@ -106,7 +114,8 @@ TEST(PostTest, put_performed) {
EXPECT_EQ(json["data"]["key"].asString(), "val");
}

TEST(HttpClient, user_agent) {
// NOLINTNEXTLINE(*non-const*)
TEST(HttpClient, UserAgent) {
{
// test the default, when setUserAgent hasn't been called yet
HttpClient http;
Expand All @@ -126,7 +135,8 @@ TEST(HttpClient, user_agent) {
}
}

TEST(Headers, update_header) {
// NOLINTNEXTLINE(*non-const*)
TEST(HttpClient, UpdateHeader) {
std::vector<std::string> headers = {"Authorization: Bearer bad"};
HttpClient http(&headers);

Expand All @@ -141,8 +151,6 @@ TEST(Headers, update_header) {
EXPECT_EQ(response["status"].asString(), "good");
}

// TODO(OTA-4546): add tests for HttpClient::download

#ifndef __NO_MAIN__
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
Expand Down
Loading