test: use TestRequestHeaderMapImpl for building custom headers#2455
test: use TestRequestHeaderMapImpl for building custom headers#2455alyssawilk merged 11 commits intoenvoyproxy:mainfrom
Conversation
| namespace Http { | ||
| namespace Utility { | ||
|
|
||
| void toEnvoyHeaders(HeaderMap& transformed_headers, envoy_headers headers) { |
There was a problem hiding this comment.
The edits to this file aren't strictly part of this diff. I just noticed that the toEnvoyHeaders function has better parameter names in its header_utility.h file, figured I'd fix the mismatch.
Signed-off-by: caschoener <schoener@google.com>
Signed-off-by: caschoener <schoener@google.com>
Signed-off-by: caschoener <schoener@google.com>
Signed-off-by: caschoener <schoener@google.com>
alyssawilk
left a comment
There was a problem hiding this comment.
Sweet! Very excited to see that hacky custom header map go away :-)
| for (auto i = 0; i < raw_headers.length; i++) { | ||
| auto key = Data::Utility::copyToString(raw_headers.entries[i].key); | ||
| auto value = Data::Utility::copyToString(raw_headers.entries[i].value); | ||
|
|
There was a problem hiding this comment.
looks like changes to this file can be reverted?
| envoy_headers envoyHeaders = Http::Utility::toBridgeHeaders(request_headers); | ||
| Platform::RawHeaderMap rawHeaderMap = Platform::envoyHeadersAsRawHeaderMap(envoyHeaders); | ||
|
|
||
| Platform::RequestHeadersBuilder builder(Platform::RequestMethod::GET, scheme_, host, "/"); |
There was a problem hiding this comment.
I think we actually want to get the scheme and host and path from the request headers, which may mean setting the host and scheme in default_request_headers in the test set up (like they were before)
it's rare but a useful corner case to be able to test http schemed requests over a TLS conection
| Platform::RawHeaderMap rawHeaderMap = Platform::envoyHeadersAsRawHeaderMap(envoyHeaders); | ||
|
|
||
| Platform::RequestHeadersBuilder builder(Platform::RequestMethod::GET, scheme_, host, "/"); | ||
| for (const auto& pair : rawHeaderMap) { |
There was a problem hiding this comment.
can we do something like
header_map.iterate(
[&request_headers, builder](const HeaderEntry& header) -> HeaderMap::Iterate {
}
to get the key/value pairs from request_headers and stick them in builder without doing the above conversions?
| void threadRoutine(absl::Notification& engine_running); | ||
| // Must be called manually by subclasses in their TearDown(); | ||
| void TearDown(); | ||
| std::shared_ptr<Platform::RequestHeaders> |
|
|
||
| Buffer::OwnedImpl request_data = Buffer::OwnedImpl("request body"); | ||
| Http::TestRequestHeaderMapImpl custom_headers; | ||
| HttpTestUtility::addDefaultHeaders(custom_headers); |
There was a problem hiding this comment.
I think we can use default_request_headers_ and make sure we addDefaultHeaders in initialize() to avoid having to create headers in each test case
| std::string upstream_request; | ||
| EXPECT_TRUE(upstream_connection->waitForData(FakeRawConnection::waitForInexactMatch("GET /"), | ||
| &upstream_request)); | ||
|
|
There was a problem hiding this comment.
nit: always good to revert non-necessary changes just to reduce churn :-)
| if (upstreamProtocol() == Http::CodecType::HTTP2) { | ||
| builder.addUpstreamHttpProtocol(Platform::UpstreamHttpProtocol::HTTP2); | ||
| } | ||
| default_request_headers_ = std::make_shared<Platform::RequestHeaders>(builder.build()); |
There was a problem hiding this comment.
I think we want to move default_request_headers_ to be a TestRequestHeaderMap impl, and shift all the tests over to using the new utility.
Signed-off-by: caschoener <schoener@google.com>
Signed-off-by: caschoener <schoener@google.com>
| Platform::RequestHeadersBuilder builder( | ||
| Platform::RequestMethod::GET, | ||
| std::string(default_request_headers_.Scheme()->value().getStringView()), | ||
| std::string(default_request_headers_.Host()->value().getStringView()), "/"); |
There was a problem hiding this comment.
let's get path from the headers as well?
| request_headers.formatter().value(); | ||
| key_val = formatter.format(key_val); | ||
| } | ||
| auto key = std::string(key_val); |
There was a problem hiding this comment.
isn't key_val already a string? Why do we need the extra one?
| builder.set(pair.first, pair.second); | ||
| } | ||
| auto mobile_headers = std::make_shared<Platform::RequestHeaders>(builder.build()); | ||
| return mobile_headers; |
There was a problem hiding this comment.
nit: consider skipping the temporary variable and just return the make_shared
| "envoy.extensions.http.header_formatters.preserve_case.v3.PreserveCaseFormatterConfig"); | ||
| ConfigHelper::setProtocolOptions(*bootstrap.mutable_static_resources()->mutable_clusters(0), | ||
| protocol_options); | ||
| }); |
There was a problem hiding this comment.
why do we need this? Envoy defaults should include stateful formatter
Signed-off-by: caschoener <schoener@google.com>
abeyad
left a comment
There was a problem hiding this comment.
Looks good Chris, thanks for working on this! Left a couple comments.
| builder.set(entry.first, values); | ||
| } | ||
| HttpTestUtility::addDefaultHeaders(default_request_headers_); | ||
| default_request_headers_.setHost(fake_upstreams_[0]->localAddress()->asStringView()); |
There was a problem hiding this comment.
why is setHost on default_request_headers_ called after adding the default request headers?
There was a problem hiding this comment.
addDefaultHeaders is really just an initialization function, which sets host to "/" I believe. And then we overwrite the values afterwards.
There was a problem hiding this comment.
Got it, thanks for clarifying. The addDefaultHeaders name was a bit confusing, wonder if something like setDefaultHeaderValues would make more sense, but in any case, that's not something for this PR.
|
|
||
| for (const auto& pair : rawHeaderMap) { | ||
| builder.set(pair.first, pair.second); | ||
| } |
There was a problem hiding this comment.
I'm not sure I follow L117-133. In L117, we iterate over request_headers, and add them to the RequestHeadersBuilder. In L131, we iterate over the rawHeaderMap and add them to the RequestHeadersBuilder. But the rawHeaderMap is built from envoy_headers which is built from request_headers, so aren't we adding the same headers to the RequestHeadersBuilder twice?
There was a problem hiding this comment.
Great catch yeah looks like I forgot to delete some code :O
Signed-off-by: caschoener <schoener@google.com>
In the commit #2362 we added a
custom_headers_field on theBaseClientIntegrationTestclass to use for custom headers.This commit replaces that class field by instead instantiating a
custom_headersobject inside tests when necessary. Then adds a helper function to convert it from theTestRequestHeaderMapImpl(an easy to edit type) toRequestHeaders(envoy header type).Signed-off-by: caschoener schoener@google.com