forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirect_integration_test.cc
107 lines (85 loc) · 4.62 KB
/
redirect_integration_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "test/integration/http_protocol_integration.h"
namespace Envoy {
class RedirectIntegrationTest : public HttpProtocolIntegrationTest {
public:
void initialize() override {
envoy::api::v2::route::RetryPolicy retry_policy;
config_helper_.addRoute("pass.through.internal.redirect", "/", "cluster_0", false,
envoy::api::v2::route::RouteAction::NOT_FOUND,
envoy::api::v2::route::VirtualHost::NONE, retry_policy, false, "",
envoy::api::v2::route::RouteAction::PASS_THROUGH_INTERNAL_REDIRECT);
config_helper_.addRoute("handle.internal.redirect", "/", "cluster_0", false,
envoy::api::v2::route::RouteAction::NOT_FOUND,
envoy::api::v2::route::VirtualHost::NONE, retry_policy, false, "",
envoy::api::v2::route::RouteAction::HANDLE_INTERNAL_REDIRECT);
HttpProtocolIntegrationTest::initialize();
}
protected:
Http::TestHeaderMapImpl redirect_response_{
{":status", "302"}, {"content-length", "0"}, {"location", "http://authority2/new/url"}};
};
// By default if internal redirects are not configured, redirects are proxied.
TEST_P(RedirectIntegrationTest, RedirectNotConfigured) {
// Use base class initialize.
HttpProtocolIntegrationTest::initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
auto response = sendRequestAndWaitForResponse(default_request_headers_, 0, redirect_response_, 0);
EXPECT_TRUE(response->complete());
EXPECT_STREQ("302", response->headers().Status()->value().c_str());
}
// Now test a route with redirects configured on in pass-through mode.
TEST_P(RedirectIntegrationTest, InternalRedirectPassedThrough) {
initialize();
codec_client_ = makeHttpConnection(lookupPort("http"));
default_request_headers_.insertHost().value("pass.through.internal.redirect", 30);
auto response = sendRequestAndWaitForResponse(default_request_headers_, 0, redirect_response_, 0);
EXPECT_STREQ("302", response->headers().Status()->value().c_str());
EXPECT_EQ(
0,
test_server_->counter("cluster.cluster_0.upstream_internal_redirect_failed_total")->value());
}
TEST_P(RedirectIntegrationTest, BasicInternalRedirect) {
// Validate that header sanitization is only called once.
config_helper_.addConfigModifier(
[](envoy::config::filter::network::http_connection_manager::v2::HttpConnectionManager& hcm) {
hcm.set_via("via_value");
});
initialize();
fake_upstreams_[0]->set_allow_unexpected_disconnects(true);
codec_client_ = makeHttpConnection(lookupPort("http"));
default_request_headers_.insertHost().value("handle.internal.redirect", 24);
IntegrationStreamDecoderPtr response =
codec_client_->makeHeaderOnlyRequest(default_request_headers_);
waitForNextUpstreamRequest();
upstream_request_->encodeHeaders(redirect_response_, true);
waitForNextUpstreamRequest();
ASSERT(upstream_request_->headers().EnvoyOriginalUrl() != nullptr);
EXPECT_STREQ("http://handle.internal.redirect/test/long/url",
upstream_request_->headers().EnvoyOriginalUrl()->value().c_str());
EXPECT_STREQ("/new/url", upstream_request_->headers().Path()->value().c_str());
EXPECT_STREQ("authority2", upstream_request_->headers().Host()->value().c_str());
EXPECT_STREQ("via_value", upstream_request_->headers().Via()->value().c_str());
upstream_request_->encodeHeaders(default_response_headers_, true);
response->waitForEndStream();
ASSERT_TRUE(response->complete());
EXPECT_STREQ("200", response->headers().Status()->value().c_str());
EXPECT_EQ(1, test_server_->counter("cluster.cluster_0.upstream_internal_redirect_succeeded_total")
->value());
}
TEST_P(RedirectIntegrationTest, InvalidRedirect) {
initialize();
redirect_response_.insertLocation().value("invalid_url", 11);
// Send the same request as above, only send an invalid URL as the response.
// The request should not be redirected.
codec_client_ = makeHttpConnection(lookupPort("http"));
default_request_headers_.insertHost().value("handle.internal.redirect", 24);
auto response = sendRequestAndWaitForResponse(default_request_headers_, 0, redirect_response_, 0);
EXPECT_STREQ("302", response->headers().Status()->value().c_str());
EXPECT_EQ(
1,
test_server_->counter("cluster.cluster_0.upstream_internal_redirect_failed_total")->value());
}
INSTANTIATE_TEST_CASE_P(Protocols, RedirectIntegrationTest,
testing::ValuesIn(HttpProtocolIntegrationTest::getProtocolTestParams()),
HttpProtocolIntegrationTest::protocolTestParamsToString);
} // namespace Envoy