From 9f4fec0dce1e931356c567fa11d9435f44fbbf50 Mon Sep 17 00:00:00 2001 From: Guillaume Pasquet Date: Thu, 10 Feb 2022 23:13:11 +0000 Subject: Working barbfile parsing --- src/barbfile.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/barbfile.rs b/src/barbfile.rs index f203c84..26ab583 100644 --- a/src/barbfile.rs +++ b/src/barbfile.rs @@ -127,9 +127,11 @@ impl FromStr for BarbFile { let (method, url) = decode_url_line(lines.next().ok_or(BarbParseError {})?)?; let mut headers: Vec
= vec![]; let mut filter = None; - for line in lines { - if !matches!(line.chars().nth(0), Some('#')) { - break; // Reached the end of the header. + + for line in &mut lines { + if line == "" { + // End of header. + break; } if let Some(_) = line.find(':') { @@ -144,9 +146,11 @@ impl FromStr for BarbFile { } } + let body = lines.fold(String::from(""), |acc, x| acc + x); + Ok(BarbFile { header: BarbHeader::new(method, url, headers, filter), - body: None, + body: if body == "" {None} else {Some(body)}, }) } } @@ -183,22 +187,30 @@ mod tests { } #[test] - fn test_parse_barbfile() { + fn test_parse_barbfile_no_body() { let barbfile = BarbFile::from_str("#GET^https://blah.com/api/blah\n#Authorization: BLAH\n#|filtr\n") .unwrap(); - assert_eq!(barbfile.body, None); assert!(matches!(barbfile.header.method, Method::GET)); assert_eq!(barbfile.header.url, "https://blah.com/api/blah"); assert_eq!(barbfile.header.filter, Some(String::from("filtr"))); - println!("{:?}", barbfile.header.headers); assert_eq!(barbfile.header.headers.len(), 1); assert_eq!(barbfile.header.headers[0].name, "Authorization"); assert_eq!(barbfile.header.headers[0].value, "BLAH"); + assert_eq!(barbfile.body, None); } #[test] - fn test_toto() { - assert!(true); + fn test_parse_barbfile_body() { + let barbfile = + BarbFile::from_str("#POST^https://blah.com/api/blah\n#Authorization: BLAH\n#|filtr\n\n{\"key\":\"value\"}\n") + .unwrap(); + assert!(matches!(barbfile.header.method, Method::POST)); + assert_eq!(barbfile.header.url, "https://blah.com/api/blah"); + assert_eq!(barbfile.header.filter, Some(String::from("filtr"))); + assert_eq!(barbfile.header.headers.len(), 1); + assert_eq!(barbfile.header.headers[0].name, "Authorization"); + assert_eq!(barbfile.header.headers[0].value, "BLAH"); + assert_eq!(barbfile.body, Some(String::from("{\"key\":\"value\"}"))) } } -- cgit v1.2.3