Skip to content

Commit

Permalink
fix: usable memory loss in HttpClientBasic::Path (philips-software#304)
Browse files Browse the repository at this point in the history
* fix: usable memory loss in HttpClientBasic::Path

* chore: add micro test for HttpClientBasic::Path functionality
  • Loading branch information
BarisTanyeri committed May 26, 2023
1 parent 3267831 commit d5d4abf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion services/network/HttpClientBasic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace services
{
auto path = services::PathFromUrl(url);
auto offset = path.empty() ? url.size() : path.begin() - url.begin();
return infra::BoundedString(infra::MakeRange(url.begin() + offset, url.begin() + url.max_size() - offset), path.size());
return infra::BoundedString(infra::MakeRange(url.begin() + offset, url.begin() + url.max_size()), path.size());
}

services::HttpHeaders HttpClientBasic::Headers() const
Expand Down
22 changes: 22 additions & 0 deletions services/network/test/TestHttpClientBasic.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "infra/stream/test/StreamMock.hpp"
#include "infra/timer/test_helper/ClockFixture.hpp"
#include "infra/util/BoundedString.hpp"
#include "infra/util/test_helper/BoundedStringMatcher.hpp"
#include "infra/util/test_helper/MockCallback.hpp"
#include "infra/util/test_helper/MockHelpers.hpp"
Expand All @@ -18,6 +19,11 @@ class HttpClientBasicMock
ContentError();
}

infra::BoundedString GetPath()
{
return Path();
}

MOCK_METHOD0(Established, void());
MOCK_METHOD0(Done, void());
MOCK_METHOD1(Error, void(bool intermittentFailure));
Expand Down Expand Up @@ -50,6 +56,22 @@ class HttpClientBasicTest
testing::StrictMock<services::HttpClientMock> httpClient;
};

TEST_F(HttpClientBasicTest, returned_path_split_correctly)
{
auto path = controller->GetPath();
infra::BoundedConstString::WithStorage<5> result = "/path";
EXPECT_EQ(path, result);
}

TEST_F(HttpClientBasicTest, returned_path_memory_trimmed_correctly)
{
std::string_view hostnamePart = "https://hostname";
auto path = controller->GetPath();
EXPECT_EQ(path.end(), url.end());
EXPECT_EQ(path.begin(), url.begin() + hostnamePart.size());
EXPECT_EQ(path.max_size(), url.max_size() - hostnamePart.size());
}

TEST_F(HttpClientBasicTest, intermittent_error_is_reported_on_ConnectionFailed)
{
EXPECT_CALL(*controller, Error(true));
Expand Down

0 comments on commit d5d4abf

Please sign in to comment.