Skip to content

Commit

Permalink
cleanup: move test utility methods in ScopedRdsIntegrationTest to bas…
Browse files Browse the repository at this point in the history
…e class HttpIntegrationTest (#8108)

Fixes #8050
Risk Level: LOW [refactor only]

Signed-off-by: Xin Zhuang <stevenzzz@google.com>
  • Loading branch information
stevenzzzz authored and htuch committed Sep 3, 2019
1 parent 0b026cf commit 911f3b0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 35 deletions.
34 changes: 34 additions & 0 deletions test/integration/http_integration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,40 @@ void HttpIntegrationTest::cleanupUpstreamAndDownstream() {
}
}

void HttpIntegrationTest::sendRequestAndVerifyResponse(
const Http::TestHeaderMapImpl& request_headers, const int request_size,
const Http::TestHeaderMapImpl& response_headers, const int response_size,
const int backend_idx) {
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = sendRequestAndWaitForResponse(request_headers, request_size, response_headers,
response_size, backend_idx);
verifyResponse(std::move(response), "200", response_headers, std::string(response_size, 'a'));

EXPECT_TRUE(upstream_request_->complete());
EXPECT_EQ(request_size, upstream_request_->bodyLength());
cleanupUpstreamAndDownstream();
}

void HttpIntegrationTest::verifyResponse(IntegrationStreamDecoderPtr response,
const std::string& response_code,
const Http::TestHeaderMapImpl& expected_headers,
const std::string& expected_body) {
EXPECT_TRUE(response->complete());
EXPECT_EQ(response_code, response->headers().Status()->value().getStringView());
expected_headers.iterate(
[](const Http::HeaderEntry& header, void* context) -> Http::HeaderMap::Iterate {
auto response_headers = static_cast<Http::HeaderMap*>(context);
const Http::HeaderEntry* entry =
response_headers->get(Http::LowerCaseString{std::string(header.key().getStringView())});
EXPECT_NE(entry, nullptr);
EXPECT_EQ(header.value().getStringView(), entry->value().getStringView());
return Http::HeaderMap::Iterate::Continue;
},
const_cast<void*>(static_cast<const void*>(&response->headers())));

EXPECT_EQ(response->body(), expected_body);
}

uint64_t
HttpIntegrationTest::waitForNextUpstreamRequest(const std::vector<uint64_t>& upstream_indices) {
uint64_t upstream_with_request;
Expand Down
14 changes: 14 additions & 0 deletions test/integration/http_integration.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ class HttpIntegrationTest : public BaseIntegrationTest {
// Close |codec_client_| and |fake_upstream_connection_| cleanly.
void cleanupUpstreamAndDownstream();

// Verifies the response_headers contains the expected_headers, and response body matches given
// body string.
void verifyResponse(IntegrationStreamDecoderPtr response, const std::string& response_code,
const Http::TestHeaderMapImpl& expected_headers,
const std::string& expected_body);

// Helper that sends a request to Envoy, and verifies if Envoy response headers and body size is
// the same as the expected headers map.
// Requires the "http" port has been registered.
void sendRequestAndVerifyResponse(const Http::TestHeaderMapImpl& request_headers,
const int request_size,
const Http::TestHeaderMapImpl& response_headers,
const int response_size, const int backend_idx);

// Check for completion of upstream_request_, and a simple "200" response.
void checkSimpleRequestSuccess(uint64_t expected_request_size, uint64_t expected_response_size,
IntegrationStreamDecoder* response);
Expand Down
35 changes: 0 additions & 35 deletions test/integration/scoped_rds_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,41 +94,6 @@ class ScopedRdsIntegrationTest : public HttpIntegrationTest,
HttpIntegrationTest::initialize();
}

// TODO(stevenzzzz): move these utility methods to base classes to share with other tests.
// Helper that verifies if given headers are in the response header map.
void verifyResponse(IntegrationStreamDecoderPtr response, const std::string& response_code,
const Http::TestHeaderMapImpl& expected_headers,
const std::string& expected_body) {
EXPECT_TRUE(response->complete());
EXPECT_EQ(response_code, response->headers().Status()->value().getStringView());
expected_headers.iterate(
[](const Http::HeaderEntry& header, void* context) -> Http::HeaderMap::Iterate {
auto response_headers = static_cast<Http::HeaderMap*>(context);
const Http::HeaderEntry* entry = response_headers->get(
Http::LowerCaseString{std::string(header.key().getStringView())});
EXPECT_NE(entry, nullptr);
EXPECT_EQ(header.value().getStringView(), entry->value().getStringView());
return Http::HeaderMap::Iterate::Continue;
},
const_cast<void*>(static_cast<const void*>(&response->headers())));
EXPECT_EQ(response->body(), expected_body);
}

// Helper that sends a request to Envoy, and verifies if Envoy response headers and body size is
// the same as the expected headers map.
void sendRequestAndVerifyResponse(const Http::TestHeaderMapImpl& request_headers,
const int request_size,
const Http::TestHeaderMapImpl& response_headers,
const int response_size, const int backend_idx) {
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = sendRequestAndWaitForResponse(request_headers, request_size, response_headers,
response_size, backend_idx);
verifyResponse(std::move(response), "200", response_headers, std::string(response_size, 'a'));
EXPECT_TRUE(upstream_request_->complete());
EXPECT_EQ(request_size, upstream_request_->bodyLength());
cleanupUpstreamAndDownstream();
}

void createUpstreams() override {
HttpIntegrationTest::createUpstreams();
// Create the SRDS upstream.
Expand Down

0 comments on commit 911f3b0

Please sign in to comment.