From 03bb85c57086e4371d827b5fbd496d3e820496b7 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 24 Aug 2020 13:43:00 -0700 Subject: tests: try all query files; some small refactors --- tests/common/mod.rs | 18 ++++++++++++++---- tests/parse_es_requests.rs | 29 +++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 8 deletions(-) (limited to 'tests') diff --git a/tests/common/mod.rs b/tests/common/mod.rs index f0b9bea..a65fb2e 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,4 +1,5 @@ +use std::path::Path; use std::io::BufRead; pub struct ExampleRequest { @@ -7,18 +8,27 @@ pub struct ExampleRequest { pub body: Option, } -pub fn load_request(name: &str) -> ExampleRequest { - +pub fn load_request_by_name(name: &str) -> ExampleRequest { let path = format!("tests/files/{}.txt", name); + let path = Path::new(&path); + load_request(&path) +} + +pub fn load_request(path: &Path) -> ExampleRequest { + let file = std::fs::File::open(path).unwrap(); let mut lines = std::io::BufReader::new(file).lines(); let first_line: Vec = lines.next().unwrap().unwrap().split(" ").map(|v| v.into()).collect(); let body: Vec = lines.map(|v| v.into()).collect::, _>>().unwrap(); - let body: String = body.join("\n"); + let body: Option = if body.len() <= 1 { + None + } else { + Some(body.join("\n")) + }; ExampleRequest { method: first_line[0].clone(), path_and_query: first_line[1].clone(), - body: Some(body), + body: body, } } diff --git a/tests/parse_es_requests.rs b/tests/parse_es_requests.rs index 01ea64a..57be06a 100644 --- a/tests/parse_es_requests.rs +++ b/tests/parse_es_requests.rs @@ -1,20 +1,41 @@ -use es_public_proxy::ApiBody; +use std::fs; +use std::ffi::OsStr; +use es_public_proxy::SearchBody; mod common; #[test] fn basic_load() { - let request = common::load_request("GET_search"); + let request = common::load_request_by_name("GET_search"); assert_eq!(request.method, "GET"); assert_eq!(request.path_and_query, "/_search"); } #[test] fn basic_parse() { - let request = common::load_request("GET_search"); + let request = common::load_request_by_name("GET_search"); assert_eq!(request.method, "GET"); assert_eq!(request.path_and_query, "/_search"); - let thing: ApiBody = serde_json::from_str(&request.body.unwrap()).unwrap(); + let _parsed: SearchBody = serde_json::from_str(&request.body.unwrap()).unwrap(); +} + +#[test] +fn parse_all_requests() { + + let file_paths = fs::read_dir("tests/files").unwrap(); + + for path in file_paths { + let path = path.unwrap().path(); + if path.extension() != Some(OsStr::new("txt")) { + continue + } + let request = common::load_request(&path); + if let Some(body) = request.body { + println!("parsing: {}", path.display()); + println!("BODY: {}", body); + let _parsed: SearchBody = serde_json::from_str(&body).unwrap(); + } + } } -- cgit v1.2.3