Tests for ratelimiter config strings

Signed-off-by: Olivier 'reivilibre <olivier@librepush.net>
This commit is contained in:
Olivier 'reivilibre' 2024-07-06 14:50:31 +01:00
parent b8a48541d3
commit 8aeb19b752

View File

@ -184,6 +184,7 @@ pub struct RatelimitsConfig {
/// - "5 Hz, 20 burst"
/// - "5 per second, 10 burst"
/// - "10 per hour, 5 burst"
#[derive(Debug)]
pub struct RatelimiterConfig {
/// The inner [`Quota`], which this struct is just a wrapper for.
pub quota: Quota,
@ -196,13 +197,19 @@ impl<'de> Deserialize<'de> for RatelimiterConfig {
{
let stringy_format = String::deserialize(deserializer)?;
let Some((left, right)) = stringy_format.split_once(',') else {
return Err(D::Error::custom("no comma. ratelimiter string should be like '5 Hz, 20 burst'"));
return Err(D::Error::custom(
"no comma. ratelimiter string should be like '5 Hz, 20 burst'",
));
};
let Some((left_val, left_unit)) = left.trim().split_once(' ') else {
return Err(D::Error::custom("no units on left. ratelimiter string should be like '5 Hz, 20 burst'"));
return Err(D::Error::custom(
"no units on left. ratelimiter string should be like '5 Hz, 20 burst'",
));
};
let Some((right_val, right_unit)) = right.trim().split_once(' ') else {
return Err(D::Error::custom("no units on right. ratelimiter string should be like '5 Hz, 20 burst'"));
return Err(D::Error::custom(
"no units on right. ratelimiter string should be like '5 Hz, 20 burst'",
));
};
let Ok(left_val) = left_val.parse::<NonZeroU32>() else {
@ -237,3 +244,62 @@ impl<'de> Deserialize<'de> for RatelimiterConfig {
Ok(RatelimiterConfig { quota })
}
}
#[cfg(test)]
mod test {
use crate::config::RatelimiterConfig;
#[test]
fn test_ratelimiter_deser_errors() {
fn deser(s: &str) -> Result<RatelimiterConfig, serde_json::Error> {
serde_json::from_value(serde_json::Value::String(s.to_owned()))
}
// this is fine
deser("5 Hz, 20 burst").unwrap();
// no comma
assert_eq!(
deser("5 Hz 20 burst").unwrap_err().to_string(),
"no comma. ratelimiter string should be like '5 Hz, 20 burst'"
);
// bad numbers
assert_eq!(
deser("five Hz, 20 burst").unwrap_err().to_string(),
"bad value on left. ratelimiter string should be like '5 Hz, 20 burst'"
);
assert_eq!(
deser("5 Hz, twenty burst").unwrap_err().to_string(),
"bad value on right. ratelimiter string should be like '5 Hz, 20 burst'"
);
// wrong order
assert_eq!(
deser("20 burst, 5 Hz").unwrap_err().to_string(),
"bad units on left. ratelimiter string should be like '5 Hz, 20 burst' or '5 per hour, 20 burst'."
);
// no units
assert_eq!(
deser("5 Hz, 20").unwrap_err().to_string(),
"no units on right. ratelimiter string should be like '5 Hz, 20 burst'"
);
assert_eq!(
deser("5, 20 burst").unwrap_err().to_string(),
"no units on left. ratelimiter string should be like '5 Hz, 20 burst'"
);
// bad units
assert_eq!(
deser("20 per milleniumm, 20 burst")
.unwrap_err()
.to_string(),
"bad units on left. ratelimiter string should be like '5 Hz, 20 burst' or '5 per hour, 20 burst'."
);
assert_eq!(
deser("5 Hz, 20 wombats").unwrap_err().to_string(),
"bad units on right. ratelimiter string should be like '5 Hz, 20 burst' or '5 per hour, 20 burst'."
);
}
}