summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tests/common/mod.rs18
-rw-r--r--tests/parse_es_requests.rs29
2 files changed, 39 insertions, 8 deletions
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<String>,
}
-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<String> = lines.next().unwrap().unwrap().split(" ").map(|v| v.into()).collect();
let body: Vec<String> = lines.map(|v| v.into()).collect::<Result<Vec<String>, _>>().unwrap();
- let body: String = body.join("\n");
+ let body: Option<String> = 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();
+ }
+ }
}