diff options
Diffstat (limited to 'src/barbfile.rs')
-rw-r--r-- | src/barbfile.rs | 60 |
1 files changed, 54 insertions, 6 deletions
diff --git a/src/barbfile.rs b/src/barbfile.rs index f3cb58f..858d035 100644 --- a/src/barbfile.rs +++ b/src/barbfile.rs @@ -1,11 +1,10 @@ +use jsonpath_rust::JsonPathQuery; use regex::Regex; +use serde_json::Value; use std::str::FromStr; use std::string::ToString; use std::{error::Error, fmt}; -use jsonpath_rust::JsonPathQuery; -use serde_json::{json, Value}; - #[cfg(feature = "jq")] use jq_rs; @@ -157,12 +156,20 @@ impl BarbFilter { #[cfg(feature = "jq")] fn apply_jq(&self, body: &String) -> Result<String, String> { + if let FilterType::JQ = self.filter_type { + return Err(String::from("Incorrect filter type")); + } + jq_rs::run(self.filter.as_str(), body.as_str()) .map_err(|x| x.to_string()) .map(|x| String::from(x.trim().trim_matches('"'))) } fn apply_path(&self, body: &String) -> Result<String, String> { + if let FilterType::JQ = self.filter_type { + return Err(String::from("Incorrect filter type")); + } + let json: Value = serde_json::from_str(body.as_str()) .map_err(|_| String::from("Failed to decode body"))?; let path = &json.path(self.filter.as_str())?; @@ -300,7 +307,6 @@ mod tests { #[test] fn test_method_from_str() { assert!(matches!(Method::from_str("GET").unwrap(), Method::GET)); - assert!(matches!(Method::from_str("GET").unwrap(), Method::GET)); assert!(matches!(Method::from_str("PUT").unwrap(), Method::PUT)); assert!(matches!(Method::from_str("POST").unwrap(), Method::POST)); assert!(matches!(Method::from_str("PATCH").unwrap(), Method::PATCH)); @@ -311,6 +317,15 @@ mod tests { } #[test] + fn test_method_takes_body() { + assert!(!Method::GET.takes_body()); + assert!(Method::PUT.takes_body()); + assert!(Method::POST.takes_body()); + assert!(Method::PATCH.takes_body()); + assert!(!Method::DELETE.takes_body()); + } + + #[test] fn test_decode_url_line() { let (method, url) = decode_url_line("#GET^http://blahblah").unwrap(); assert!(matches!(method, Method::GET)); @@ -359,16 +374,49 @@ mod tests { } #[test] - fn test_parse_named_filter() { + fn test_jq_parse_named_filter() { let filter = BarbFilter::from_str("#FOO|.bar.foo").unwrap(); assert_eq!(filter.name, Some(String::from("FOO"))); assert_eq!(filter.filter, String::from(".bar.foo")); + assert!(matches!(filter.filter_type, FilterType::JQ)); } #[test] - fn test_parse_named_filter_no_name() { + fn test_jq_parse_named_filter_no_name() { let filter = BarbFilter::from_str("#|.bar.foo").unwrap(); assert_eq!(filter.name, None); assert_eq!(filter.filter, String::from(".bar.foo")); + assert!(matches!(filter.filter_type, FilterType::JQ)); + } + + #[test] + fn test_path_parse_named_filter() { + let filter = BarbFilter::from_str("#FOO$$.bar.foo").unwrap(); + assert_eq!(filter.name, Some(String::from("FOO"))); + assert_eq!(filter.filter, String::from("$.bar.foo")); + assert!(matches!(filter.filter_type, FilterType::PATH)); + } + + #[test] + fn test_path_parse_named_filter_no_name() { + let filter = BarbFilter::from_str("#$$.bar.foo").unwrap(); + assert_eq!(filter.name, None); + assert_eq!(filter.filter, String::from("$.bar.foo")); + assert!(matches!(filter.filter_type, FilterType::PATH)); + } + + #[cfg(feature = "jq")] + #[test] + fn test_apply_filter_jq() { + let jq_f = BarbFilter::from_str("#|.status").unwrap(); + let subject = String::from(r#"{"status": "OK"}"#); + assert_eq!(jq_f.apply(&subject).unwrap(), String::from("OK")); + } + + #[test] + fn test_apply_filter_path() { + let path_f = BarbFilter::from_str("#$$.status").unwrap(); + let subject = String::from(r#"{"status": "OK"}"#); + assert_eq!(path_f.apply(&subject).unwrap(), String::from("OK")); } } |