Skip to content

Commit 7e86ae0

Browse files
HttpResponse to copy network statistics
Copy constructor and copy assignment should take network statistics into account for precise calculation. Relates-To: OCMAM-308 Signed-off-by: Rustam Gamidov <[email protected]>
1 parent aac3dd4 commit 7e86ae0

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

olp-cpp-sdk-core/include/olp/core/client/HttpResponse.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ class CORE_API HttpResponse {
132132
* @param other The instance of `HttpStatus` to copy from.
133133
*/
134134
HttpResponse(const HttpResponse& other)
135-
: status_(other.status_), headers_(other.headers_) {
135+
: status_(other.status_),
136+
headers_(other.headers_),
137+
network_statistics_(other.network_statistics_) {
136138
response_ << other.response_.rdbuf();
137139
if (!response_.good()) {
138140
// Depending on the users handling of the stringstream it might be that
@@ -157,6 +159,7 @@ class CORE_API HttpResponse {
157159
response_ = std::stringstream{};
158160
response_ << other.response_.rdbuf();
159161
headers_ = other.headers_;
162+
network_statistics_ = other.network_statistics_;
160163
}
161164

162165
return *this;

olp-cpp-sdk-core/tests/client/PendingUrlRequestsTest.cpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020 HERE Europe B.V.
2+
* Copyright (C) 2020-2024 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,6 +48,8 @@ constexpr int kCancelledStatus =
4848
constexpr http::RequestId kRequestId = 1234u;
4949
constexpr auto kSleepFor = std::chrono::seconds(1);
5050
constexpr auto kWaitFor = std::chrono::seconds(5);
51+
constexpr uint64_t kBytesDownloaded = 568234u;
52+
constexpr uint64_t kBytesUploaded = 42342u;
5153

5254
client::HttpResponse GetHttpResponse(ErrorCode error, std::string status) {
5355
return client::HttpResponse(static_cast<int>(error), status);
@@ -70,6 +72,12 @@ TEST(HttpResponseTest, Copy) {
7072
SCOPED_TRACE("Error response");
7173

7274
auto response = GetHttpResponse(ErrorCode::CANCELLED_ERROR, kBadResponse);
75+
response.SetNetworkStatistics({kBytesUploaded, kBytesDownloaded});
76+
EXPECT_EQ(response.GetNetworkStatistics().GetBytesUploaded(),
77+
kBytesUploaded);
78+
EXPECT_EQ(response.GetNetworkStatistics().GetBytesDownloaded(),
79+
kBytesDownloaded);
80+
7381
auto copy_response = response;
7482

7583
std::string status;
@@ -86,6 +94,10 @@ TEST(HttpResponseTest, Copy) {
8694
static_cast<int>(ErrorCode::CANCELLED_ERROR));
8795
EXPECT_EQ(copy_response.GetStatus(), response.GetStatus());
8896
EXPECT_EQ(status, copy_status);
97+
EXPECT_EQ(copy_response.GetNetworkStatistics().GetBytesUploaded(),
98+
kBytesUploaded);
99+
EXPECT_EQ(copy_response.GetNetworkStatistics().GetBytesDownloaded(),
100+
kBytesDownloaded);
89101
}
90102

91103
{
@@ -94,6 +106,12 @@ TEST(HttpResponseTest, Copy) {
94106
http::Headers headers = {{"header1", "value1"}, {"header2", "value2"}};
95107
auto response =
96108
GetHttpResponse(http::HttpStatusCode::OK, kGoodResponse, headers);
109+
response.SetNetworkStatistics({kBytesUploaded, kBytesDownloaded});
110+
EXPECT_EQ(response.GetNetworkStatistics().GetBytesUploaded(),
111+
kBytesUploaded);
112+
EXPECT_EQ(response.GetNetworkStatistics().GetBytesDownloaded(),
113+
kBytesDownloaded);
114+
97115
auto copy_response = response;
98116

99117
std::string status;
@@ -109,6 +127,43 @@ TEST(HttpResponseTest, Copy) {
109127
EXPECT_EQ(response.GetStatus(), http::HttpStatusCode::OK);
110128
EXPECT_EQ(copy_response.GetStatus(), response.GetStatus());
111129
EXPECT_EQ(status, copy_status);
130+
EXPECT_EQ(copy_response.GetNetworkStatistics().GetBytesUploaded(),
131+
kBytesUploaded);
132+
EXPECT_EQ(copy_response.GetNetworkStatistics().GetBytesDownloaded(),
133+
kBytesDownloaded);
134+
}
135+
136+
{
137+
SCOPED_TRACE("Copy assign");
138+
139+
auto response = GetHttpResponse(ErrorCode::CANCELLED_ERROR, kBadResponse);
140+
response.SetNetworkStatistics({kBytesUploaded, kBytesDownloaded});
141+
EXPECT_EQ(response.GetNetworkStatistics().GetBytesUploaded(),
142+
kBytesUploaded);
143+
EXPECT_EQ(response.GetNetworkStatistics().GetBytesDownloaded(),
144+
kBytesDownloaded);
145+
146+
HttpResponse copy_response;
147+
copy_response = response;
148+
149+
std::string status;
150+
std::string copy_status;
151+
response.GetResponse(status);
152+
copy_response.GetResponse(copy_status);
153+
154+
EXPECT_FALSE(status.empty());
155+
EXPECT_FALSE(copy_status.empty());
156+
EXPECT_TRUE(response.GetHeaders().empty());
157+
EXPECT_TRUE(copy_response.GetHeaders().empty());
158+
EXPECT_EQ(kBadResponse, status);
159+
EXPECT_EQ(response.GetStatus(),
160+
static_cast<int>(ErrorCode::CANCELLED_ERROR));
161+
EXPECT_EQ(copy_response.GetStatus(), response.GetStatus());
162+
EXPECT_EQ(status, copy_status);
163+
EXPECT_EQ(copy_response.GetNetworkStatistics().GetBytesUploaded(),
164+
kBytesUploaded);
165+
EXPECT_EQ(copy_response.GetNetworkStatistics().GetBytesDownloaded(),
166+
kBytesDownloaded);
112167
}
113168
}
114169

0 commit comments

Comments
 (0)