mirror of
https://github.com/alibaba/higress.git
synced 2026-05-27 22:27:29 +08:00
Rust Plugin add Rule matcher test (#3230)
This commit is contained in:
@@ -35,7 +35,6 @@ struct SayHelloRoot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct SayHello {
|
struct SayHello {
|
||||||
rule_matcher: SharedRuleMatcher<SayHelloConfig>,
|
|
||||||
log: Log,
|
log: Log,
|
||||||
config: Option<Rc<SayHelloConfig>>,
|
config: Option<Rc<SayHelloConfig>>,
|
||||||
weak: Weak<RefCell<Box<dyn HttpContextWrapper<SayHelloConfig>>>>,
|
weak: Weak<RefCell<Box<dyn HttpContextWrapper<SayHelloConfig>>>>,
|
||||||
@@ -43,6 +42,7 @@ struct SayHello {
|
|||||||
|
|
||||||
#[derive(Default, Debug, Deserialize, Clone)]
|
#[derive(Default, Debug, Deserialize, Clone)]
|
||||||
struct SayHelloConfig {
|
struct SayHelloConfig {
|
||||||
|
#[serde(default)]
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,7 +92,6 @@ impl RootContextWrapper<SayHelloConfig> for SayHelloRoot {
|
|||||||
Some(Box::new(SayHello {
|
Some(Box::new(SayHello {
|
||||||
log: Log::new(PLUGIN_NAME.to_string()),
|
log: Log::new(PLUGIN_NAME.to_string()),
|
||||||
config: None,
|
config: None,
|
||||||
rule_matcher: Rc::new(RefCell::new(RuleMatcher::default())),
|
|
||||||
weak: Default::default(),
|
weak: Default::default(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
@@ -122,20 +121,15 @@ impl HttpContextWrapper<SayHelloConfig> for SayHello {
|
|||||||
&mut self,
|
&mut self,
|
||||||
_headers: &multimap::MultiMap<String, String>,
|
_headers: &multimap::MultiMap<String, String>,
|
||||||
) -> HeaderAction {
|
) -> HeaderAction {
|
||||||
let binding = self.rule_matcher.borrow();
|
if let Some(config) = &self.config {
|
||||||
let config = match binding.get_match_config() {
|
|
||||||
None => {
|
|
||||||
self.send_http_response(200, vec![], Some("Hello, World!".as_bytes()));
|
|
||||||
return HeaderAction::Continue;
|
|
||||||
}
|
|
||||||
Some(config) => config.1,
|
|
||||||
};
|
|
||||||
|
|
||||||
self.send_http_response(
|
self.send_http_response(
|
||||||
200,
|
200,
|
||||||
vec![],
|
vec![],
|
||||||
Some(format!("Hello, {}!", config.name).as_bytes()),
|
Some(format!("Hello, {}!", config.name).as_bytes()),
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
|
self.send_http_response(200, vec![], Some("Hello, World!".as_bytes()));
|
||||||
|
}
|
||||||
HeaderAction::Continue
|
HeaderAction::Continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -176,16 +176,23 @@ where
|
|||||||
let service_name =
|
let service_name =
|
||||||
String::from_utf8(get_property(vec!["cluster_name"]).unwrap_or_default())
|
String::from_utf8(get_property(vec!["cluster_name"]).unwrap_or_default())
|
||||||
.unwrap_or_else(|_| "".to_string());
|
.unwrap_or_else(|_| "".to_string());
|
||||||
|
self.get_match_config_by_args(&host, &route_name, &service_name)
|
||||||
|
}
|
||||||
|
fn get_match_config_by_args(
|
||||||
|
&self,
|
||||||
|
host: &str,
|
||||||
|
route_name: &str,
|
||||||
|
service_name: &str,
|
||||||
|
) -> Option<(i64, Rc<PluginConfig>)> {
|
||||||
for (i, rule) in self.rule_config.iter().enumerate() {
|
for (i, rule) in self.rule_config.iter().enumerate() {
|
||||||
match rule.category {
|
match rule.category {
|
||||||
Category::Host => {
|
Category::Host => {
|
||||||
if self.host_match(rule, host.as_str()) {
|
if self.host_match(rule, host) {
|
||||||
return Some((i as i64, rule.config.clone()));
|
return Some((i as i64, rule.config.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Category::Route => {
|
Category::Route => {
|
||||||
if rule.routes.contains(route_name.as_str()) {
|
if rule.routes.contains(route_name) {
|
||||||
return Some((i as i64, rule.config.clone()));
|
return Some((i as i64, rule.config.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -197,7 +204,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Category::Service => {
|
Category::Service => {
|
||||||
if self.service_match(rule, &service_name) {
|
if self.service_match(rule, service_name) {
|
||||||
return Some((i as i64, rule.config.clone()));
|
return Some((i as i64, rule.config.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -639,6 +646,48 @@ mod tests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_match_route_config() {
|
||||||
|
let mut rule: RuleMatcher<CustomConfig> = RuleMatcher::default();
|
||||||
|
|
||||||
|
let res = rule.parse_rule_config(
|
||||||
|
&serde_json::from_str(
|
||||||
|
r#"{"_rules_":[{"_match_route_":["test1","test2"],"name":"ann", "age":16}]}"#,
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
);
|
||||||
|
assert!(res.is_ok());
|
||||||
|
let config = rule.get_match_config_by_args("test", "test", "test");
|
||||||
|
assert!(config.is_none());
|
||||||
|
let config = rule.get_match_config_by_args("test", "test1", "test");
|
||||||
|
assert!(config.is_some());
|
||||||
|
let c = config.unwrap();
|
||||||
|
assert_eq!(c.1.name, "ann");
|
||||||
|
assert_eq!(c.1.age, 16);
|
||||||
|
|
||||||
|
let config = rule.get_match_config_by_args("test", "test2", "test");
|
||||||
|
assert!(config.is_some());
|
||||||
|
let c = config.unwrap();
|
||||||
|
assert_eq!(c.1.name, "ann");
|
||||||
|
assert_eq!(c.1.age, 16);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_match_route1_config() {
|
||||||
|
let mut rule: RuleMatcher<CustomConfig> = RuleMatcher::default();
|
||||||
|
|
||||||
|
let res = rule.parse_rule_config(
|
||||||
|
&serde_json::from_str(r#"{"_rules_":[{"_match_route_":["test1"]}]}"#).unwrap(),
|
||||||
|
);
|
||||||
|
assert!(res.is_ok());
|
||||||
|
let config = rule.get_match_config_by_args("test", "test", "test");
|
||||||
|
assert!(config.is_none());
|
||||||
|
let config = rule.get_match_config_by_args("test", "test1", "test");
|
||||||
|
assert!(config.is_some());
|
||||||
|
let c = config.unwrap();
|
||||||
|
assert_eq!(c.1.name, "");
|
||||||
|
assert_eq!(c.1.age, 0);
|
||||||
|
}
|
||||||
#[derive(Default, Clone, Deserialize, PartialEq, Eq)]
|
#[derive(Default, Clone, Deserialize, PartialEq, Eq)]
|
||||||
struct CompleteConfig {
|
struct CompleteConfig {
|
||||||
// global config
|
// global config
|
||||||
|
|||||||
Reference in New Issue
Block a user