Skip to content

Commit

Permalink
Expect in tests
Browse files Browse the repository at this point in the history
Signed-off-by: Adam Cattermole <[email protected]>
  • Loading branch information
adam-cattermole committed Nov 20, 2024
1 parent 5c3ea61 commit b3d4307
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 45 deletions.
13 changes: 8 additions & 5 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ mod test {
}
assert!(res.is_ok());

let filter_config = res.unwrap();
let filter_config = res.expect("result is ok");
assert_eq!(filter_config.action_sets.len(), 1);

let services = &filter_config.services;
Expand Down Expand Up @@ -364,7 +364,7 @@ mod test {
}
assert!(res.is_ok());

let filter_config = res.unwrap();
let filter_config = res.expect("result is ok");
assert_eq!(filter_config.action_sets.len(), 0);
}

Expand Down Expand Up @@ -410,12 +410,15 @@ mod test {
}
assert!(res.is_ok());

let filter_config = res.unwrap();
let filter_config = res.expect("result is ok");
assert_eq!(filter_config.action_sets.len(), 1);

let services = &filter_config.services;
assert_eq!(
services.get("limitador").unwrap().timeout,
services
.get("limitador")
.expect("limitador service to be set")
.timeout,
Timeout(Duration::from_millis(20))
);

Expand Down Expand Up @@ -510,7 +513,7 @@ mod test {
}
assert!(res.is_ok());

let result = FilterConfig::try_from(res.unwrap());
let result = FilterConfig::try_from(res.expect("result is ok"));
let filter_config = result.expect("That didn't work");
let rlp_option = filter_config
.index
Expand Down
10 changes: 5 additions & 5 deletions src/configuration/action_set_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ mod tests {

let val = index.get_longest_match_action_sets("example.com");
assert!(val.is_some());
assert_eq!(val.unwrap()[0].name, "rlp1");
assert_eq!(val.expect("value must be some")[0].name, "rlp1");
}

#[test]
Expand All @@ -90,7 +90,7 @@ mod tests {
let val = index.get_longest_match_action_sets("test.example.com");

assert!(val.is_some());
assert_eq!(val.unwrap()[0].name, "rlp1");
assert_eq!(val.expect("value must be some")[0].name, "rlp1");
}

#[test]
Expand All @@ -103,11 +103,11 @@ mod tests {

let val = index.get_longest_match_action_sets("test.example.com");
assert!(val.is_some());
assert_eq!(val.unwrap()[0].name, "rlp2");
assert_eq!(val.expect("value must be some")[0].name, "rlp2");

let val = index.get_longest_match_action_sets("example.com");
assert!(val.is_some());
assert_eq!(val.unwrap()[0].name, "rlp1");
assert_eq!(val.expect("value must be some")[0].name, "rlp1");
}

#[test]
Expand All @@ -118,6 +118,6 @@ mod tests {

let val = index.get_longest_match_action_sets("test.example.com");
assert!(val.is_some());
assert_eq!(val.unwrap()[0].name, "rlp1");
assert_eq!(val.expect("value must be some")[0].name, "rlp1");
}
}
48 changes: 27 additions & 21 deletions src/data/cel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,14 @@ pub mod data {
fn it_works() {
let map = AttributeMap::new(
[
known_attribute_for(&"request.method".into()).unwrap(),
known_attribute_for(&"request.referer".into()).unwrap(),
known_attribute_for(&"source.address".into()).unwrap(),
known_attribute_for(&"destination.port".into()).unwrap(),
known_attribute_for(&"request.method".into())
.expect("request.method known attribute exists"),
known_attribute_for(&"request.referer".into())
.expect("request.referer known attribute exists"),
known_attribute_for(&"source.address".into())
.expect("source.address known attribute exists"),
known_attribute_for(&"destination.port".into())
.expect("destination.port known attribute exists"),
]
.into(),
);
Expand All @@ -572,38 +576,38 @@ pub mod data {
assert!(map.data.contains_key("destination"));
assert!(map.data.contains_key("request"));

match map.data.get("source").unwrap() {
match map.data.get("source").expect("source is some") {
Token::Node(map) => {
assert_eq!(map.len(), 1);
match map.get("address").unwrap() {
match map.get("address").expect("address is some") {
Token::Node(_) => panic!("Not supposed to get here!"),
Token::Value(v) => assert_eq!(v.path, "source.address".into()),
}
}
Token::Value(_) => panic!("Not supposed to get here!"),
}

match map.data.get("destination").unwrap() {
match map.data.get("destination").expect("destination is some") {
Token::Node(map) => {
assert_eq!(map.len(), 1);
match map.get("port").unwrap() {
match map.get("port").expect("port is some") {
Token::Node(_) => panic!("Not supposed to get here!"),
Token::Value(v) => assert_eq!(v.path, "destination.port".into()),
}
}
Token::Value(_) => panic!("Not supposed to get here!"),
}

match map.data.get("request").unwrap() {
match map.data.get("request").expect("request is some") {
Token::Node(map) => {
assert_eq!(map.len(), 2);
assert!(map.get("method").is_some());
match map.get("method").unwrap() {
match map.get("method").expect("method is some") {
Token::Node(_) => panic!("Not supposed to get here!"),
Token::Value(v) => assert_eq!(v.path, "request.method".into()),
}
assert!(map.get("referer").is_some());
match map.get("referer").unwrap() {
match map.get("referer").expect("referer is some") {
Token::Node(_) => panic!("Not supposed to get here!"),
Token::Value(v) => assert_eq!(v.path, "request.referer".into()),
}
Expand Down Expand Up @@ -635,7 +639,7 @@ mod tests {
let value = Expression::new(
"auth.identity.anonymous && auth.identity != null && auth.identity.foo > 3",
)
.unwrap();
.expect("This is valid CEL!");
assert_eq!(value.attributes.len(), 3);
assert_eq!(value.attributes[0].path, "auth.identity".into());
}
Expand All @@ -650,7 +654,7 @@ mod tests {
"true".bytes().collect(),
)));
let value = Expression::new("auth.identity.anonymous")
.unwrap()
.expect("This is valid CEL!")
.eval()
.expect("This must evaluate!");
assert_eq!(value, true.into());
Expand All @@ -660,7 +664,7 @@ mod tests {
"42".bytes().collect(),
)));
let value = Expression::new("auth.identity.age")
.unwrap()
.expect("This is valid CEL!")
.eval()
.expect("This must evaluate!");
assert_eq!(value, 42.into());
Expand All @@ -670,7 +674,7 @@ mod tests {
"42.3".bytes().collect(),
)));
let value = Expression::new("auth.identity.age")
.unwrap()
.expect("This is valid CEL!")
.eval()
.expect("This must evaluate!");
assert_eq!(value, 42.3.into());
Expand All @@ -680,7 +684,7 @@ mod tests {
"\"John\"".bytes().collect(),
)));
let value = Expression::new("auth.identity.age")
.unwrap()
.expect("This is valid CEL!")
.eval()
.expect("This must evaluate!");
assert_eq!(value, "John".into());
Expand All @@ -690,7 +694,7 @@ mod tests {
"-42".bytes().collect(),
)));
let value = Expression::new("auth.identity.name")
.unwrap()
.expect("This is valid CEL!")
.eval()
.expect("This must evaluate!");
assert_eq!(value, (-42).into());
Expand All @@ -701,7 +705,7 @@ mod tests {
"some random crap".bytes().collect(),
)));
let value = Expression::new("auth.identity.age")
.unwrap()
.expect("This is valid CEL!")
.eval()
.expect("This must evaluate!");
assert_eq!(value, "some random crap".into());
Expand Down Expand Up @@ -775,12 +779,14 @@ mod tests {
80_i64.to_le_bytes().into(),
)));
let value = known_attribute_for(&"destination.port".into())
.unwrap()
.expect("destination.port known attribute exists")
.get();
assert_eq!(value, 80.into());
property::test::TEST_PROPERTY_VALUE
.set(Some(("request.method".into(), "GET".bytes().collect())));
let value = known_attribute_for(&"request.method".into()).unwrap().get();
let value = known_attribute_for(&"request.method".into())
.expect("request.method known attribute exists")
.get();
assert_eq!(value, "GET".into());
}

Expand All @@ -802,7 +808,7 @@ mod tests {
b"\xCA\xFE".to_vec(),
)));
let value = Expression::new("getHostProperty(['foo', 'bar.baz'])")
.unwrap()
.expect("This is valid CEL!")
.eval()
.expect("This must evaluate!");
assert_eq!(value, Value::Bytes(Arc::new(b"\xCA\xFE".to_vec())));
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ mod tests {
.push(header("test", "some value"));
resp.response_headers_to_add
.push(header("other", "header value"));
let buffer = resp.write_to_bytes().unwrap();
let buffer = resp
.write_to_bytes()
.expect("must be able to write RateLimitResponse to bytes");
let expected: [u8; 45] = [
8, 1, 26, 18, 10, 4, 116, 101, 115, 116, 18, 10, 115, 111, 109, 101, 32, 118, 97, 108,
117, 101, 26, 21, 10, 5, 111, 116, 104, 101, 114, 18, 12, 104, 101, 97, 100, 101, 114,
Expand Down
72 changes: 61 additions & 11 deletions src/operation_dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,34 +463,84 @@ mod tests {
assert_eq!(operation_dispatcher.waiting_operations.len(), 0);

let mut op = operation_dispatcher.next();
assert_eq!(op.clone().unwrap().unwrap().get_result(), Ok(66));
assert_eq!(
*op.clone().unwrap().unwrap().get_service_type(),
op.clone()
.expect("ok result")
.expect("operation is some")
.get_result(),
Ok(66)
);
assert_eq!(
*op.clone()
.expect("ok result")
.expect("operation is some")
.get_service_type(),
ServiceType::RateLimit
);
assert_eq!(op.unwrap().unwrap().get_state(), State::Waiting);
assert_eq!(
op.expect("ok result")
.expect("operation is some")
.get_state(),
State::Waiting
);
assert_eq!(operation_dispatcher.waiting_operations.len(), 1);

op = operation_dispatcher.next();
assert_eq!(op.clone().unwrap().unwrap().get_result(), Ok(66));
assert_eq!(op.unwrap().unwrap().get_state(), State::Done);
assert_eq!(
op.clone()
.expect("ok result")
.expect("operation is some")
.get_result(),
Ok(66)
);
assert_eq!(
op.expect("ok result")
.expect("operation is some")
.get_state(),
State::Done
);

op = operation_dispatcher.next();
assert_eq!(op.clone().unwrap().unwrap().get_result(), Ok(77));
assert_eq!(
*op.clone().unwrap().unwrap().get_service_type(),
op.clone()
.expect("ok result")
.expect("operation is some")
.get_result(),
Ok(77)
);
assert_eq!(
*op.clone()
.expect("ok result")
.expect("operation is some")
.get_service_type(),
ServiceType::Auth
);
assert_eq!(op.unwrap().unwrap().get_state(), State::Waiting);
assert_eq!(
op.expect("ok result")
.expect("operation is some")
.get_state(),
State::Waiting
);
assert_eq!(operation_dispatcher.waiting_operations.len(), 1);

op = operation_dispatcher.next();
assert_eq!(op.clone().unwrap().unwrap().get_result(), Ok(77));
assert_eq!(op.unwrap().unwrap().get_state(), State::Done);
assert_eq!(
op.clone()
.expect("ok result")
.expect("operation is some")
.get_result(),
Ok(77)
);
assert_eq!(
op.expect("ok result")
.expect("operation is some")
.get_state(),
State::Done
);
assert_eq!(operation_dispatcher.waiting_operations.len(), 1);

op = operation_dispatcher.next();
assert!(op.unwrap().is_none());
assert!(op.expect("ok result").is_none());
assert!(operation_dispatcher.get_current_operation_state().is_none());
assert_eq!(operation_dispatcher.waiting_operations.len(), 0);
}
Expand Down
18 changes: 16 additions & 2 deletions src/service/rate_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,22 @@ mod tests {

assert_eq!(msg.hits_addend, 1);
assert_eq!(msg.domain, "rlp1".to_string());
assert_eq!(msg.descriptors.first().unwrap().entries[0].key, "key1");
assert_eq!(msg.descriptors.first().unwrap().entries[0].value, "value1");
assert_eq!(
msg.descriptors
.first()
.expect("must have a descriptor")
.entries[0]
.key,
"key1"
);
assert_eq!(
msg.descriptors
.first()
.expect("must have a descriptor")
.entries[0]
.value,
"value1"
);
assert_eq!(msg.unknown_fields, UnknownFields::default());
assert_eq!(msg.cached_size, CachedSize::default());
}
Expand Down

0 comments on commit b3d4307

Please sign in to comment.