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

Ensure request body is correctly propagated in Nighthawk Request Source. #1289

Merged
Merged
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
1 change: 1 addition & 0 deletions include/nighthawk/common/request.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Request {
* @return HeaderMapPtr shared pointer to a request header specification.
*/
virtual HeaderMapPtr header() const PURE;
virtual const std::string& body() const PURE;
// TODO(oschaaf): expectations
};

Expand Down
9 changes: 8 additions & 1 deletion source/common/request_impl.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include <string>
Pelinthecoder marked this conversation as resolved.
Show resolved Hide resolved
#include <utility>

#include "envoy/http/header_map.h"

#include "nighthawk/common/request.h"
Expand All @@ -8,11 +11,15 @@ namespace Nighthawk {

Pelinthecoder marked this conversation as resolved.
Show resolved Hide resolved
class RequestImpl : public Request {
public:
RequestImpl(HeaderMapPtr header) : header_(std::move(header)) {}
RequestImpl(HeaderMapPtr header, std::string json_body = "")
: header_(std::move(header)), json_body_(json_body) {}

HeaderMapPtr header() const override { return header_; }
const std::string& body() const override { return json_body_; }

private:
HeaderMapPtr header_;
std::string json_body_;
};

} // namespace Nighthawk
4 changes: 3 additions & 1 deletion source/request_source/request_options_list_plugin_impl.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "source/request_source/request_options_list_plugin_impl.h"

#include <memory>

#include "external/envoy/source/common/protobuf/message_validator_impl.h"
#include "external/envoy/source/common/protobuf/protobuf.h"
#include "external/envoy/source/common/protobuf/utility.h"
Expand Down Expand Up @@ -109,7 +111,7 @@ RequestGenerator OptionsListRequestSource::get() {
auto lower_case_key = Envoy::Http::LowerCaseString(std::string(option_header.header().key()));
header->setCopy(lower_case_key, std::string(option_header.header().value()));
}
return std::make_unique<RequestImpl>(std::move(header));
return std::make_unique<RequestImpl>(std::move(header), request_option.json_body());
};
return request_generator;
}
Expand Down
22 changes: 22 additions & 0 deletions test/request_source/request_source_plugin_test.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
#include <cstdint>
#include <string>
#include <utility>

#include "envoy/api/api.h"
#include "envoy/common/exception.h"
#include "envoy/http/header_map.h"

#include "nighthawk/common/exception.h"
#include "nighthawk/common/request.h"
#include "nighthawk/common/request_source.h"
#include "nighthawk/request_source/request_source_plugin_config_factory.h"

#include "external/envoy/source/common/config/utility.h"
#include "external/envoy/source/common/http/header_map_impl.h"
#include "external/envoy/source/common/protobuf/message_validator_impl.h"
#include "external/envoy/source/common/protobuf/protobuf.h"
#include "external/envoy/test/mocks/api/mocks.h"
#include "external/envoy/test/mocks/stats/mocks.h"
#include "external/envoy/test/test_common/file_system_for_test.h"
Expand Down Expand Up @@ -171,6 +185,10 @@ TEST_F(FileBasedRequestSourcePluginTest,
Nighthawk::HeaderMapPtr header2 = request2->header();
EXPECT_EQ(header1->getPathValue(), "/a");
EXPECT_EQ(header2->getPathValue(), "/b");
std::string body1 = request1->body();
std::string body2 = request2->body();
EXPECT_EQ(body1, R"({"message": "hello1"})");
EXPECT_EQ(body2, R"({"message": "hello2"})");
EXPECT_EQ(request3, nullptr);
}

Expand Down Expand Up @@ -353,6 +371,10 @@ TEST_F(InLineRequestSourcePluginTest,
Nighthawk::HeaderMapPtr header2 = request2->header();
EXPECT_EQ(header1->getPathValue(), "/a");
EXPECT_EQ(header2->getPathValue(), "/b");
std::string body1 = request1->body();
std::string body2 = request2->body();
EXPECT_EQ(body1, R"({"message": "hello1"})");
EXPECT_EQ(body2, R"({"message": "hello2"})");
EXPECT_EQ(request3, nullptr);
}

Expand Down
6 changes: 4 additions & 2 deletions test/request_source/test_data/test-config-ab.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
options:
- request_method: 1
request_body_size: 10
request_body_size: 23
fei-deng marked this conversation as resolved.
Show resolved Hide resolved
json_body: '{"message": "hello1"}'
request_headers:
- { header: { key: ":path", value: "/a" } }
- { header: { key: "x-nighthawk-test-server-config", value: "{response_body_size:13}" } }
- request_method: 1
request_body_size: 10
request_body_size: 23
json_body: '{"message": "hello2"}'
request_headers:
- { header: { key: ":path", value: "/b" } }
- { header: { key: "x-nighthawk-test-server-config", value: "{response_body_size:17}" } }