Skip to content

Commit

Permalink
Add deserialization tests for Config
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzzypixelz committed Nov 30, 2023
1 parent 2ac02b3 commit 867de18
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,61 @@ impl<'de> serde::de::Visitor<'de> for PathVisitor {
Ok(v)
}
}

#[cfg(test)]
mod tests {
use super::{Config, DEFAULT_HTTP_INTERFACE};

#[test]
fn test_path_field() {
// See: https://github.com/eclipse-zenoh/zenoh-plugin-webserver/issues/19
let config =
serde_json::from_str::<Config>(r#"{"__path__": "/example/path", "http_port": 8080}"#);

assert!(config.is_ok());
let Config {
http_port,
__required__,
__path__,
} = config.unwrap();

assert_eq!(http_port, format!("{DEFAULT_HTTP_INTERFACE}:8080"));
assert_eq!(__path__, Some(vec![String::from("/example/path")]));
assert_eq!(__required__, None);
}

#[test]
fn test_required_field() {
// See: https://github.com/eclipse-zenoh/zenoh-plugin-webserver/issues/19
let config = serde_json::from_str::<Config>(r#"{"__required__": true, "http_port": 8080}"#);
assert!(config.is_ok());
let Config {
http_port,
__required__,
__path__,
} = config.unwrap();

assert_eq!(http_port, format!("{DEFAULT_HTTP_INTERFACE}:8080"));
assert_eq!(__path__, None);
assert_eq!(__required__, Some(true));
}

#[test]
fn test_path_field_and_required_field() {
// See: https://github.com/eclipse-zenoh/zenoh-plugin-webserver/issues/19
let config = serde_json::from_str::<Config>(
r#"{"__path__": "/example/path", "__required__": true, "http_port": 8080}"#,
);

assert!(config.is_ok());
let Config {
http_port,
__required__,
__path__,
} = config.unwrap();

assert_eq!(http_port, format!("{DEFAULT_HTTP_INTERFACE}:8080"));
assert_eq!(__path__, Some(vec![String::from("/example/path")]));
assert_eq!(__required__, Some(true));
}
}

0 comments on commit 867de18

Please sign in to comment.