diff --git a/limitador-server/src/envoy_rls/server.rs b/limitador-server/src/envoy_rls/server.rs index 3975d08e..b6c3e7a0 100644 --- a/limitador-server/src/envoy_rls/server.rs +++ b/limitador-server/src/envoy_rls/server.rs @@ -253,10 +253,9 @@ mod tests { namespace, 1, 60, - vec!["req_method == 'GET'".to_string()], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); let limiter = RateLimiter::new(10_000); limiter.add_limit(limit); @@ -395,16 +394,23 @@ mod tests { let namespace = "test_namespace"; vec![ - Limit::new(namespace, 10, 60, vec!["x == '1'".to_string()], vec!["z"]) - .expect("This must be a valid limit!"), + Limit::new( + namespace, + 10, + 60, + vec!["x == '1'".try_into().expect("failed parsing!")], + vec!["z"], + ), Limit::new( namespace, 0, 60, - vec!["x == '1'".to_string(), "y == '2'".to_string()], + vec![ + "x == '1'".try_into().expect("failed parsing!"), + "y == '2'".try_into().expect("failed parsing!"), + ], vec!["z"], - ) - .expect("This must be a valid limit!"), + ), ] .into_iter() .for_each(|limit| { @@ -468,8 +474,13 @@ mod tests { #[tokio::test] async fn test_takes_into_account_the_hits_addend_param() { let namespace = "test_namespace"; - let limit = Limit::new(namespace, 10, 60, vec!["x == '1'".to_string()], vec!["y"]) - .expect("This must be a valid limit!"); + let limit = Limit::new( + namespace, + 10, + 60, + vec!["x == '1'".try_into().expect("failed parsing!")], + vec!["y"], + ); let limiter = RateLimiter::new(10_000); limiter.add_limit(limit); @@ -538,8 +549,13 @@ mod tests { // "hits_addend" is optional according to the spec, and should default // to 1, However, with the autogenerated structs it defaults to 0. let namespace = "test_namespace"; - let limit = Limit::new(namespace, 1, 60, vec!["x == '1'".to_string()], vec!["y"]) - .expect("This must be a valid limit!"); + let limit = Limit::new( + namespace, + 1, + 60, + vec!["x == '1'".try_into().expect("failed parsing!")], + vec!["y"], + ); let limiter = RateLimiter::new(10_000); limiter.add_limit(limit); diff --git a/limitador-server/src/http_api/request_types.rs b/limitador-server/src/http_api/request_types.rs index af80e7b5..99adc8b6 100644 --- a/limitador-server/src/http_api/request_types.rs +++ b/limitador-server/src/http_api/request_types.rs @@ -1,5 +1,5 @@ use limitador::counter::Counter as LimitadorCounter; -use limitador::limit::{Limit as LimitadorLimit, ParseError}; +use limitador::limit::{Limit as LimitadorLimit, ParseError, Predicate}; use paperclip::actix::Apiv2Schema; use serde::{Deserialize, Serialize}; use std::collections::{BTreeMap, HashMap}; @@ -44,23 +44,26 @@ impl TryFrom for LimitadorLimit { type Error = ParseError; fn try_from(limit: Limit) -> Result { + let conditions: Result, ParseError> = + limit.conditions.into_iter().map(|p| p.try_into()).collect(); + let mut limitador_limit = if let Some(id) = limit.id { Self::with_id( id, limit.namespace, limit.max_value, limit.seconds, - limit.conditions, + conditions?, limit.variables, - )? + ) } else { Self::new( limit.namespace, limit.max_value, limit.seconds, - limit.conditions, + conditions?, limit.variables, - )? + ) }; if let Some(name) = limit.name { diff --git a/limitador-server/src/http_api/server.rs b/limitador-server/src/http_api/server.rs index 08825b1d..d2c26b03 100644 --- a/limitador-server/src/http_api/server.rs +++ b/limitador-server/src/http_api/server.rs @@ -553,10 +553,9 @@ mod tests { namespace, max, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); match &limiter { Limiter::Blocking(limiter) => limiter.add_limit(limit.clone()), diff --git a/limitador/benches/bench.rs b/limitador/benches/bench.rs index 5dcb3677..edccd9e2 100644 --- a/limitador/benches/bench.rs +++ b/limitador/benches/bench.rs @@ -529,7 +529,11 @@ fn generate_test_limits(scenario: &TestScenario) -> (Vec, Vec (Vec, Vec( - namespace, - 42, - 100, - Vec::::default(), - Vec::::default(), - ) - .expect("This must be a valid limit!"); + let l = Limit::new(namespace, 42, 100, vec![], Vec::::default()); rl.add_limit(l.clone()); let limits = rl.get_limits(&namespace.into()); assert_eq!(limits.len(), 1); diff --git a/limitador/src/limit.rs b/limitador/src/limit.rs index d336b5eb..afc3f054 100644 --- a/limitador/src/limit.rs +++ b/limitador/src/limit.rs @@ -49,57 +49,43 @@ pub struct Limit { } impl Limit { - pub fn new, T: TryInto>( + pub fn new>( namespace: N, max_value: u64, seconds: u64, - conditions: impl IntoIterator, + conditions: impl IntoIterator, variables: impl IntoIterator>, - ) -> Result + ) -> Self where - >::Error: core::fmt::Debug, - >::Error: core::fmt::Debug, - ParseError: From<>::Error>, + >::Error: Debug, { - // the above where-clause is needed in order to call unwrap(). - let conditions: Result, _> = - conditions.into_iter().map(|cond| cond.try_into()).collect(); - match conditions { - Ok(conditions) => Ok(Self { - id: None, - namespace: namespace.into(), - max_value, - seconds, - name: None, - conditions, - variables: variables.into_iter().map(|var| var.into()).collect(), - }), - Err(err) => Err(err.into()), + Self { + id: None, + namespace: namespace.into(), + max_value, + seconds, + name: None, + conditions: conditions.into_iter().collect(), + variables: variables.into_iter().map(|var| var.into()).collect(), } } - pub fn with_id, N: Into, T: TryInto>( + pub fn with_id, N: Into>( id: S, namespace: N, max_value: u64, seconds: u64, - conditions: impl IntoIterator, + conditions: impl IntoIterator, variables: impl IntoIterator>, - ) -> Result - where - ParseError: From<>::Error>, - { - match conditions.into_iter().map(|cond| cond.try_into()).collect() { - Ok(conditions) => Ok(Self { - id: Some(id.into()), - namespace: namespace.into(), - max_value, - seconds, - name: None, - conditions, - variables: variables.into_iter().map(|var| var.into()).collect(), - }), - Err(err) => Err(err.into()), + ) -> Self { + Self { + id: Some(id.into()), + namespace: namespace.into(), + max_value, + seconds, + name: None, + conditions: conditions.into_iter().collect(), + variables: variables.into_iter().map(|var| var.into()).collect(), } } @@ -215,8 +201,13 @@ mod tests { #[test] fn limit_can_have_an_optional_name() { - let mut limit = Limit::new("test_namespace", 10, 60, vec!["x == \"5\""], vec!["y"]) - .expect("This must be a valid limit!"); + let mut limit = Limit::new( + "test_namespace", + 10, + 60, + vec!["x == \"5\"".try_into().expect("failed parsing!")], + vec!["y"], + ); assert!(limit.name.is_none()); let name = "Test Limit"; @@ -226,8 +217,13 @@ mod tests { #[test] fn limit_applies() { - let limit = Limit::new("test_namespace", 10, 60, vec!["x == \"5\""], vec!["y"]) - .expect("This must be a valid limit!"); + let limit = Limit::new( + "test_namespace", + 10, + 60, + vec!["x == \"5\"".try_into().expect("failed parsing!")], + vec!["y"], + ); let mut values: HashMap = HashMap::new(); values.insert("x".into(), "5".into()); @@ -238,8 +234,13 @@ mod tests { #[test] fn limit_does_not_apply_when_cond_is_false() { - let limit = Limit::new("test_namespace", 10, 60, vec!["x == \"5\""], vec!["y"]) - .expect("This must be a valid limit!"); + let limit = Limit::new( + "test_namespace", + 10, + 60, + vec!["x == \"5\"".try_into().expect("failed parsing!")], + vec!["y"], + ); let mut values: HashMap = HashMap::new(); values.insert("x".into(), "1".into()); @@ -250,8 +251,13 @@ mod tests { #[test] fn limit_does_not_apply_when_cond_var_is_not_set() { - let limit = Limit::new("test_namespace", 10, 60, vec!["x == \"5\""], vec!["y"]) - .expect("This must be a valid limit!"); + let limit = Limit::new( + "test_namespace", + 10, + 60, + vec!["x == \"5\"".try_into().expect("failed parsing!")], + vec!["y"], + ); // Notice that "x" is not set let mut values: HashMap = HashMap::new(); @@ -263,8 +269,13 @@ mod tests { #[test] fn limit_does_not_apply_when_var_not_set() { - let limit = Limit::new("test_namespace", 10, 60, vec!["x == \"5\""], vec!["y"]) - .expect("This must be a valid limit!"); + let limit = Limit::new( + "test_namespace", + 10, + 60, + vec!["x == \"5\"".try_into().expect("failed parsing!")], + vec!["y"], + ); // Notice that "y" is not set let mut values: HashMap = HashMap::new(); @@ -279,10 +290,12 @@ mod tests { "test_namespace", 10, 60, - vec!["x == \"5\"", "y == \"2\""], + vec![ + "x == \"5\"".try_into().expect("failed parsing!"), + "y == \"2\"".try_into().expect("failed parsing!"), + ], vec!["z"], - ) - .expect("This must be a valid limit!"); + ); let mut values: HashMap = HashMap::new(); values.insert("x".into(), "5".into()); @@ -298,10 +311,12 @@ mod tests { "test_namespace", 10, 60, - vec!["x == \"5\"", "y == \"2\""], + vec![ + "x == \"5\"".try_into().expect("failed parsing!"), + "y == \"2\"".try_into().expect("failed parsing!"), + ], vec!["z"], - ) - .expect("This must be a valid limit!"); + ); let mut values: HashMap = HashMap::new(); values.insert("x".into(), "3".into()); @@ -318,10 +333,9 @@ mod tests { "test_namespace", 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); assert_eq!(limit.id(), Some("test_id")) } @@ -333,19 +347,17 @@ mod tests { "test_namespace", 42, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); let mut limit2 = Limit::new( limit1.namespace.clone(), limit1.max_value + 10, limit1.seconds, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], limit1.variables.clone(), - ) - .expect("This must be a valid limit!"); + ); limit2.set_name("Who cares?".to_string()); assert_eq!(limit1.partial_cmp(&limit2), Some(Equal)); @@ -358,10 +370,11 @@ mod tests { "ns", 42, 10, - vec!["limit.name == 'named_limit'"], + vec!["limit.name == 'named_limit'" + .try_into() + .expect("failed parsing!")], Vec::::default(), - ) - .expect("failed to create"); + ); assert!(!limit.applies(&HashMap::default())); limit.set_name("named_limit".to_string()); @@ -372,10 +385,12 @@ mod tests { "ns", 42, 10, - vec!["limit.id == 'my_id'", "limit.name == null"], + vec![ + "limit.id == 'my_id'".try_into().expect("failed parsing!"), + "limit.name == null".try_into().expect("failed parsing!"), + ], Vec::::default(), - ) - .expect("failed to create"); + ); assert!(limit.applies(&HashMap::default())); let limit = Limit::with_id( @@ -383,10 +398,11 @@ mod tests { "ns", 42, 10, - vec!["limit.id == 'other_id'"], + vec!["limit.id == 'other_id'" + .try_into() + .expect("failed parsing!")], Vec::::default(), - ) - .expect("failed to create"); + ); assert!(!limit.applies(&HashMap::default())); } } diff --git a/limitador/src/storage/disk/rocksdb_storage.rs b/limitador/src/storage/disk/rocksdb_storage.rs index b9344213..68ef7369 100644 --- a/limitador/src/storage/disk/rocksdb_storage.rs +++ b/limitador/src/storage/disk/rocksdb_storage.rs @@ -242,8 +242,13 @@ mod tests { #[test] fn opens_db_on_disk() { let namespace = "test_namespace"; - let limit = Limit::new(namespace, 1, 2, vec!["req_method == 'GET'"], vec!["app_id"]) - .expect("This must be a valid limit!"); + let limit = Limit::new( + namespace, + 1, + 2, + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], + vec!["app_id"], + ); let counter = Counter::new(limit, HashMap::default()); let tmp = TempDir::new().expect("We should have a dir!"); diff --git a/limitador/src/storage/in_memory.rs b/limitador/src/storage/in_memory.rs index c71ab3a0..dca294b7 100644 --- a/limitador/src/storage/in_memory.rs +++ b/limitador/src/storage/in_memory.rs @@ -252,16 +252,20 @@ mod tests { fn counters_for_multiple_limit_per_ns() { let storage = InMemoryStorage::default(); let namespace = "test_namespace"; - let limit_1 = Limit::new(namespace, 1, 1, vec!["req_method == 'GET'"], vec!["app_id"]) - .expect("This must be a valid limit!"); + let limit_1 = Limit::new( + namespace, + 1, + 1, + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], + vec!["app_id"], + ); let limit_2 = Limit::new( namespace, 1, 10, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); let counter_1 = Counter::new(limit_1, HashMap::default()); let counter_2 = Counter::new(limit_2, HashMap::default()); storage.update_counter(&counter_1, 1).unwrap(); diff --git a/limitador/src/storage/keys.rs b/limitador/src/storage/keys.rs index e426f6b0..9bbeddfa 100644 --- a/limitador/src/storage/keys.rs +++ b/limitador/src/storage/keys.rs @@ -119,10 +119,9 @@ mod tests { "example.com", 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); assert_eq!( "namespace:{example.com},counters_of_limit:{\"namespace\":\"example.com\",\"seconds\":60,\"conditions\":[\"req_method == 'GET'\"],\"variables\":[\"app_id\"]}".as_bytes(), key_for_counters_of_limit(&limit)) @@ -135,10 +134,9 @@ mod tests { "example.com", 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); assert_eq!( "\u{2}\u{7}test_id".as_bytes(), key_for_counters_of_limit(&limit) @@ -148,8 +146,13 @@ mod tests { #[test] fn counter_key_and_counter_are_symmetric() { let namespace = "ns_counter:"; - let limit = Limit::new(namespace, 1, 1, vec!["req_method == 'GET'"], vec!["app_id"]) - .expect("This must be a valid limit!"); + let limit = Limit::new( + namespace, + 1, + 1, + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], + vec!["app_id"], + ); let counter = Counter::new(limit.clone(), HashMap::default()); let raw = key_for_counter(&counter); assert_eq!(counter, partial_counter_from_counter_key(&raw)); @@ -158,8 +161,13 @@ mod tests { #[test] fn counter_key_does_not_include_transient_state() { let namespace = "ns_counter:"; - let limit = Limit::new(namespace, 1, 1, vec!["req_method == 'GET'"], vec!["app_id"]) - .expect("This must be a valid limit!"); + let limit = Limit::new( + namespace, + 1, + 1, + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], + vec!["app_id"], + ); let counter = Counter::new(limit.clone(), HashMap::default()); let mut other = counter.clone(); other.set_remaining(123); @@ -174,7 +182,7 @@ pub mod bin { use std::collections::HashMap; use crate::counter::Counter; - use crate::limit::Limit; + use crate::limit::{Limit, Predicate}; #[derive(PartialEq, Debug, Serialize, Deserialize)] struct IdCounterKey<'a> { @@ -272,8 +280,16 @@ pub mod bin { .into_iter() .map(|(var, value)| (var.to_string(), value.to_string())) .collect(); - let limit = - Limit::new(ns, u64::default(), seconds, conditions, map.keys()).unwrap(); + let limit = Limit::new( + ns, + u64::default(), + seconds, + conditions + .into_iter() + .map(|p| p.try_into().expect("condition corrupted!")) + .collect::>(), + map.keys(), + ); Counter::new(limit, map) } 2u8 => { @@ -284,15 +300,8 @@ pub mod bin { .collect(); // we are not able to rebuild the full limit since we only have the id and variables. - let limit = Limit::with_id::<&str, &str, &str>( - id, - "", - u64::default(), - 0, - vec![], - map.keys(), - ) - .unwrap(); + let limit = + Limit::with_id::<&str, &str>(id, "", u64::default(), 0, vec![], map.keys()); Counter::new(limit, map) } _ => panic!("Unknown version: {}", version), @@ -321,8 +330,17 @@ pub mod bin { .into_iter() .map(|(var, value)| (var.to_string(), value.to_string())) .collect(); - let limit = Limit::new(ns, u64::default(), seconds, conditions, map.keys()); - Counter::new(limit.unwrap(), map) + let limit = Limit::new( + ns, + u64::default(), + seconds, + conditions + .into_iter() + .map(|p| p.try_into().expect("condition corrupted!")) + .collect::>(), + map.keys(), + ); + Counter::new(limit, map) } #[cfg(test)] @@ -342,10 +360,9 @@ pub mod bin { namespace, 1, 2, - vec!["foo == 'bar'"], + vec!["foo == 'bar'".try_into().expect("failed parsing!")], vec!["app_id", "role", "wat"], - ) - .expect("This must be a valid limit!"); + ); let mut vars = HashMap::default(); vars.insert("role".to_string(), "admin".to_string()); vars.insert("app_id".to_string(), "123".to_string()); @@ -362,8 +379,13 @@ pub mod bin { #[test] fn counter_key_and_counter_are_symmetric() { let namespace = "ns_counter:"; - let limit = Limit::new(namespace, 1, 1, vec!["req_method == 'GET'"], vec!["app_id"]) - .expect("This must be a valid limit!"); + let limit = Limit::new( + namespace, + 1, + 1, + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], + vec!["app_id"], + ); let mut variables = HashMap::default(); variables.insert("app_id".to_string(), "123".to_string()); let counter = Counter::new(limit.clone(), variables); @@ -374,8 +396,13 @@ pub mod bin { #[test] fn counter_key_starts_with_namespace_prefix() { let namespace = "ns_counter:"; - let limit = Limit::new(namespace, 1, 1, vec!["req_method == 'GET'"], vec!["app_id"]) - .expect("This must be a valid limit!"); + let limit = Limit::new( + namespace, + 1, + 1, + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], + vec!["app_id"], + ); let counter = Counter::new(limit, HashMap::default()); let serialized_counter = key_for_counter(&counter); @@ -386,18 +413,21 @@ pub mod bin { #[test] fn counters_with_id() { let namespace = "ns_counter:"; - let limit_without_id = - Limit::new(namespace, 1, 1, vec!["req_method == 'GET'"], vec!["app_id"]) - .expect("This must be a valid limit!"); + let limit_without_id = Limit::new( + namespace, + 1, + 1, + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], + vec!["app_id"], + ); let limit_with_id = Limit::with_id( "id200", namespace, 1, 1, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); let counter_with_id = Counter::new(limit_with_id, HashMap::default()); let serialized_with_id_counter = key_for_counter(&counter_with_id); diff --git a/limitador/src/storage/redis/counters_cache.rs b/limitador/src/storage/redis/counters_cache.rs index b1e4b085..1924d63f 100644 --- a/limitador/src/storage/redis/counters_cache.rs +++ b/limitador/src/storage/redis/counters_cache.rs @@ -680,10 +680,9 @@ mod tests { "test_namespace", max_val, 60, - vec!["req_method == 'POST'"], + vec!["req_method == 'POST'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"), + ), values, ) } diff --git a/limitador/src/storage/redis/redis_cached.rs b/limitador/src/storage/redis/redis_cached.rs index 1ef9afec..742d2986 100644 --- a/limitador/src/storage/redis/redis_cached.rs +++ b/limitador/src/storage/redis/redis_cached.rs @@ -444,10 +444,9 @@ mod tests { "test_namespace", 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"), + ), Default::default(), ); @@ -507,10 +506,9 @@ mod tests { "test_namespace", 10, 60, - vec!["req_method == 'POST'"], + vec!["req_method == 'POST'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"), + ), Default::default(), ); @@ -567,10 +565,9 @@ mod tests { "test_namespace", 10, 60, - vec!["req_method == 'POST'"], + vec!["req_method == 'POST'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"), + ), Default::default(), ); diff --git a/limitador/tests/integration_tests.rs b/limitador/tests/integration_tests.rs index 6fa8cece..7572d766 100644 --- a/limitador/tests/integration_tests.rs +++ b/limitador/tests/integration_tests.rs @@ -220,18 +220,16 @@ mod test { "first_namespace", 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"), + ), Limit::new( "second_namespace", 20, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"), + ), ]; for limit in limits { @@ -254,19 +252,17 @@ mod test { "first_namespace", 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); let lim2 = Limit::new( "second_namespace", 20, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); for limit in [&lim1, &lim2] { rate_limiter.add_limit(limit).await; @@ -288,10 +284,9 @@ mod test { "test_namespace", 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -309,10 +304,9 @@ mod test { "test_namespace", 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], Vec::::new(), - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -332,19 +326,17 @@ mod test { namespace, 10, 60, - vec!["req_method == 'POST'"], + vec!["req_method == 'POST'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); let limit_2 = Limit::new( namespace, 5, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit_1).await; rate_limiter.add_limit(&limit_2).await; @@ -361,10 +353,9 @@ mod test { "test_namespace", 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -379,10 +370,9 @@ mod test { namespace, 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -415,18 +405,16 @@ mod test { namespace, 10, 60, - vec!["req_method == 'POST'"], + vec!["req_method == 'POST'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"), + ), Limit::new( namespace, 5, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"), + ), ]; for limit in limits.iter() { @@ -445,16 +433,22 @@ mod test { let namespace2 = "test_namespace_2"; rate_limiter - .add_limit( - &Limit::new(namespace1, 10, 60, vec!["x == '10'"], vec!["z"]) - .expect("This must be a valid limit!"), - ) + .add_limit(&Limit::new( + namespace1, + 10, + 60, + vec!["x == '10'".try_into().expect("failed parsing!")], + vec!["z"], + )) .await; rate_limiter - .add_limit( - &Limit::new(namespace2, 5, 60, vec!["x == '10'"], vec!["z"]) - .expect("This must be a valid limit!"), - ) + .add_limit(&Limit::new( + namespace2, + 5, + 60, + vec!["x == '10'".try_into().expect("failed parsing!")], + vec!["z"], + )) .await; rate_limiter.delete_limits(namespace1).await.unwrap(); @@ -469,10 +463,9 @@ mod test { namespace, 5, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -504,10 +497,9 @@ mod test { namespace, max_hits, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -542,10 +534,9 @@ mod test { namespace, max_hits, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -580,18 +571,16 @@ mod test { namespace, max_hits, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"), + ), Limit::new( namespace, max_hits + 1, 60, - vec!["req_method == 'POST'"], + vec!["req_method == 'POST'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"), + ), ]; for limit in limits { @@ -650,10 +639,9 @@ mod test { namespace, 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -686,10 +674,9 @@ mod test { namespace, max, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -710,10 +697,9 @@ mod test { namespace, max_hits, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -765,10 +751,9 @@ mod test { namespace, 0, // So reporting 1 more would not be allowed 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -790,10 +775,9 @@ mod test { namespace, 0, // So reporting 1 more would not be allowed 60, - Vec::::new(), // unconditional + Vec::default(), // unconditional vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -814,10 +798,9 @@ mod test { namespace, max_hits, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -852,10 +835,9 @@ mod test { namespace, max_hits, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -903,10 +885,9 @@ mod test { namespace, 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -933,10 +914,9 @@ mod test { namespace, 0, // So reporting 1 more would not be allowed 60, - Vec::::new(), // unconditional + Vec::default(), // unconditional vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -962,10 +942,9 @@ mod test { namespace, max_hits, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -1020,10 +999,9 @@ mod test { "test_namespace", 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -1042,10 +1020,9 @@ mod test { namespace, 10, limit_time, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -1068,19 +1045,17 @@ mod test { "first_namespace", 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); let second_limit = Limit::new( "second_namespace", 20, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter .configure_with(vec![first_limit.clone(), second_limit.clone()]) @@ -1109,10 +1084,9 @@ mod test { namespace, max_value, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit).await; @@ -1149,19 +1123,17 @@ mod test { namespace, 10, 1, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); let limit_to_be_deleted = Limit::new( namespace, 20, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); for limit in [&limit_to_be_kept, &limit_to_be_deleted].iter() { rate_limiter.add_limit(limit).await; @@ -1185,19 +1157,17 @@ mod test { namespace, 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); let limit_update = Limit::new( namespace, 20, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); rate_limiter.add_limit(&limit_orig).await; @@ -1219,28 +1189,25 @@ mod test { namespace, 10, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); let limit_2 = Limit::new( namespace, 20, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); let mut limit_3 = Limit::new( namespace, 20, 60, - vec!["req_method == 'GET'"], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); limit_3.set_name("Name is irrelevant too".to_owned()); assert!(rate_limiter.add_limit(&limit_1).await); @@ -1267,10 +1234,9 @@ mod test { namespace, max_hits, 60, - vec!["req_method == 'GET'".to_string()], + vec!["req_method == 'GET'".try_into().expect("failed parsing!")], vec!["app_id"], - ) - .expect("This must be a valid limit!"); + ); for rate_limiter in rate_limiters.iter() { rate_limiter.add_limit(&limit).await;