Skip to content

Commit b3317e3

Browse files
author
Kirill Zhuchkov
authored
Add method that removes an epxectation by a path-method matcher. (#720)
The method is essential for cleaning expectations on the fly so that the mock server doesn't eat up all available RAM. Relates-To: OAM-188 Signed-off-by: Kirill Zhuchkov <[email protected]>
1 parent 4d52a0e commit b3317e3

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

tests/utils/mock-server-client/Client.h

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@
3333

3434
namespace mockserver {
3535

36+
namespace {
3637
const auto kBaseUrl = "https://localhost:1080";
3738
const auto kExpectationPath = "/mockserver/expectation";
3839
const auto kStatusPath = "/mockserver/status";
3940
const auto kResetPath = "/mockserver/reset";
41+
const auto kClearPath = "/mockserver/clear";
4042
const auto kTimeout = std::chrono::seconds{10};
43+
} // namespace
4144

4245
class Client {
4346
public:
@@ -55,6 +58,9 @@ class Client {
5558

5659
void Reset();
5760

61+
void RemoveMockResponse(const std::string& method_matcher,
62+
const std::string& path_matcher);
63+
5864
private:
5965
void CreateExpectation(const Expectation& expectation);
6066

@@ -127,8 +133,28 @@ inline Status::Ports Client::Ports() const {
127133
inline void Client::Reset() {
128134
auto response = http_client_->CallApi(kResetPath, "PUT", {}, {}, {}, nullptr,
129135
"", olp::client::CancellationContext{});
136+
}
137+
138+
inline void Client::RemoveMockResponse(const std::string& method_matcher,
139+
const std::string& path_matcher) {
140+
rapidjson::StringBuffer buffer;
141+
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
142+
143+
writer.StartObject();
144+
writer.Key("method");
145+
writer.String(method_matcher.c_str());
146+
writer.Key("path");
147+
writer.String(path_matcher.c_str());
148+
writer.EndObject();
149+
150+
const auto data = std::string{buffer.GetString()};
130151

131-
return;
152+
const auto request_body =
153+
std::make_shared<std::vector<unsigned char>>(data.begin(), data.end());
154+
155+
auto response =
156+
http_client_->CallApi(kClearPath, "PUT", {}, {}, {}, request_body, "",
157+
olp::client::CancellationContext{});
132158
}
133159

134160
inline void Client::CreateExpectation(const Expectation& expectation) {
@@ -139,8 +165,6 @@ inline void Client::CreateExpectation(const Expectation& expectation) {
139165
auto response =
140166
http_client_->CallApi(kExpectationPath, "PUT", {}, {}, {}, request_body,
141167
"", olp::client::CancellationContext{});
142-
143-
return;
144168
}
145169

146170
} // namespace mockserver

0 commit comments

Comments
 (0)