Skip to content

Commit 1547977

Browse files
committed
More example comments
Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent 4c489d9 commit 1547977

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

rust/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ declare_init_functions!(init, new_http_filter_config_fn);
77

88
/// This implements the [`envoy_proxy_dynamic_modules_rust_sdk::ProgramInitFunction`].
99
///
10-
/// This is called exactly once when the module is loaded.
10+
/// This is called exactly once when the module is loaded. It can be used to
11+
/// initialize global state as well as check the runtime environment to ensure that
12+
/// the module is running in a supported environment.
13+
///
14+
/// Returning `false` will cause Envoy to reject the config hence the
15+
/// filter will not be loaded.
1116
fn init() -> bool {
1217
true
1318
}
@@ -21,10 +26,10 @@ fn init() -> bool {
2126
fn new_http_filter_config_fn<EC: EnvoyHttpFilterConfig, EHF: EnvoyHttpFilter>(
2227
_envoy_filter_config: &mut EC,
2328
filter_name: &str,
24-
_filter_config: &str,
29+
filter_config: &str,
2530
) -> Option<Box<dyn HttpFilterConfig<EC, EHF>>> {
2631
match filter_name {
27-
"passthrough" => Some(Box::new(PassthroughHttpFilterConfig {})),
32+
"passthrough" => Some(Box::new(PassthroughHttpFilterConfig::new(filter_config))),
2833
_ => panic!("Unknown filter name: {}", filter_name),
2934
}
3035
}

rust/src/passthrough.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
11
use envoy_proxy_dynamic_modules_rust_sdk::*;
22

33
/// This implements the [`envoy_proxy_dynamic_modules_rust_sdk::HttpFilterConfig`] trait.
4-
pub struct PassthroughHttpFilterConfig {}
4+
///
5+
/// The trait corresponds to a Envoy filter chain configuration.
6+
pub struct PassthroughHttpFilterConfig {
7+
_filter_config: String,
8+
}
9+
10+
impl PassthroughHttpFilterConfig {
11+
/// This is the constructor for the [`PassthroughHttpFilterConfig`].
12+
///
13+
/// filter_config is the filter config from the Envoy config here:
14+
/// https://www.envoyproxy.io/docs/envoy/latest/api-v3/extensions/dynamic_modules/v3/dynamic_modules.proto#envoy-v3-api-msg-extensions-dynamic-modules-v3-dynamicmoduleconfig
15+
pub fn new(filter_config: &str) -> Self {
16+
Self {
17+
_filter_config: filter_config.to_string(),
18+
}
19+
}
20+
}
521

622
impl<EC: EnvoyHttpFilterConfig, EHF: EnvoyHttpFilter> HttpFilterConfig<EC, EHF>
723
for PassthroughHttpFilterConfig
824
{
25+
/// This is called for each new HTTP filter.
926
fn new_http_filter(&self, _envoy: &mut EC) -> Box<dyn HttpFilter<EHF>> {
1027
Box::new(PassthroughHttpFilter {})
1128
}
@@ -16,4 +33,43 @@ impl<EC: EnvoyHttpFilterConfig, EHF: EnvoyHttpFilter> HttpFilterConfig<EC, EHF>
1633
/// This is a passthrough filter that does nothing.
1734
pub struct PassthroughHttpFilter {}
1835

36+
/// This implements the [`envoy_proxy_dynamic_modules_rust_sdk::HttpFilter`] trait.
37+
///
38+
/// Default implementation of all methods is to return `Continue`.
1939
impl<EHF: EnvoyHttpFilter> HttpFilter<EHF> for PassthroughHttpFilter {}
40+
41+
#[cfg(test)]
42+
mod tests {
43+
use super::*;
44+
45+
#[test]
46+
/// This demonstrates how to write a test without Envoy using a mock provided by the SDK.
47+
fn test_passthrough_http_filter() {
48+
let mut envoy_filter = envoy_proxy_dynamic_modules_rust_sdk::MockEnvoyHttpFilter::new();
49+
let mut passthrough_filter = PassthroughHttpFilter {};
50+
assert_eq!(
51+
passthrough_filter.on_request_headers(&mut envoy_filter, false),
52+
abi::envoy_dynamic_module_type_on_http_filter_request_headers_status::Continue
53+
);
54+
assert_eq!(
55+
passthrough_filter.on_request_body(&mut envoy_filter, false),
56+
abi::envoy_dynamic_module_type_on_http_filter_request_body_status::Continue
57+
);
58+
assert_eq!(
59+
passthrough_filter.on_request_trailers(&mut envoy_filter),
60+
abi::envoy_dynamic_module_type_on_http_filter_request_trailers_status::Continue
61+
);
62+
assert_eq!(
63+
passthrough_filter.on_response_headers(&mut envoy_filter, false),
64+
abi::envoy_dynamic_module_type_on_http_filter_response_headers_status::Continue
65+
);
66+
assert_eq!(
67+
passthrough_filter.on_response_body(&mut envoy_filter, false),
68+
abi::envoy_dynamic_module_type_on_http_filter_response_body_status::Continue
69+
);
70+
assert_eq!(
71+
passthrough_filter.on_response_trailers(&mut envoy_filter),
72+
abi::envoy_dynamic_module_type_on_http_filter_response_trailers_status::Continue
73+
);
74+
}
75+
}

0 commit comments

Comments
 (0)