Skip to content

Commit

Permalink
TEST: default behavior within http rules
Browse files Browse the repository at this point in the history
  • Loading branch information
slivingston committed Nov 2, 2024
1 parent 587ed7a commit a663553
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/bin/rrhttp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,17 @@ impl Request {
Some(q) => q,
None => return true,
};
let mut matched = vec![];
for value_rule in schema {
let query_value = match query.get(&value_rule.name) {
Some(v_option) => match v_option {
Some(v) => v,
Some(v) => {
if matched.contains(&v) {
// Reject if there are duplicates
return false;
}
v
}
None => {
// TODO: is empty parameter equivalent to `true`?
return false;
Expand All @@ -176,6 +183,7 @@ impl Request {
continue;
}
};
matched.push(&value_rule.name);
match value_rule.value_type {
ValueType::Bool => {
if query_value != "true" && query_value != "false" {
Expand Down Expand Up @@ -212,12 +220,20 @@ impl Request {
}
}
}
if rule.default == ConfigMode::Block && matched.len() != query.len() {
return false;
}
} else {
// POST
let body = match &self.body {
Some(b) => b,
None => return true,
};
if !body.is_object() {
// Body must be JSON object {...}
return false;
}
let mut matched = vec![];
for value_rule in schema {
let body_value = match body.get(&value_rule.name) {
Some(v) => v,
Expand All @@ -228,6 +244,7 @@ impl Request {
continue;
}
};
matched.push(&value_rule.name);
match value_rule.value_type {
ValueType::Bool => {
if body_value.is_boolean() {
Expand Down Expand Up @@ -258,6 +275,13 @@ impl Request {
}
}
}
if rule.default == ConfigMode::Block {
if let Some(b) = body.as_object() {
if matched.len() != b.len() {
return false;
}
}
}
}
true
}
Expand Down Expand Up @@ -702,6 +726,13 @@ rules:
q.insert("FileName".to_string(), Some("image1".into()));
}
assert!(config.is_valid(&req));

// Change to block (also known as reject) if unknown query part
let mut config = config.clone();
if let Some(rule) = &mut config.rules.first_mut() {
rule.default = ConfigMode::Block;
}
assert!(!config.is_valid(&req));
}

#[test]
Expand All @@ -712,6 +743,7 @@ rules:
- verb: POST
uri: /api/head
has_body: true
default: block
schema:
- name: Pitch
optional: false
Expand Down Expand Up @@ -769,6 +801,15 @@ rules:
"Velocity": 75,
}));
assert!(!config.is_valid(&req));

req.body = Some(json!({
"Pitch": 0,
"Roll": 0,
"Yaw": 0,
"Velocity": 75,
"Other": 0,
}));
assert!(!config.is_valid(&req));
}

#[test]
Expand Down

0 comments on commit a663553

Please sign in to comment.