From d8d10a292a0d6bd0134fcfe785718bb8f48dd085 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Tue, 25 Aug 2020 14:10:44 -0700 Subject: tests: proper URIs; filter request bodies --- tests/common/mod.rs | 23 ++++++++++++---- tests/files/GET_highlight.txt | 2 +- tests/files/GET_highlight_complex.txt | 2 +- tests/files/GET_highlight_fields.txt | 2 +- tests/files/GET_highlight_query.txt | 2 +- tests/files/GET_search.txt | 2 +- tests/files/GET_search_bool.txt | 2 +- tests/files/GET_search_boosting.txt | 2 +- tests/files/GET_search_docvalues.txt | 2 +- tests/files/GET_search_multi_match.txt | 2 +- tests/files/POST_search_inner.txt | 2 +- tests/files/POST_search_rescore.txt | 2 +- tests/files/POST_search_sort_mode.txt | 2 +- tests/parse_es_requests.rs | 50 ++++++++++++++++++++++++---------- 14 files changed, 65 insertions(+), 32 deletions(-) (limited to 'tests') diff --git a/tests/common/mod.rs b/tests/common/mod.rs index a65fb2e..890dead 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,20 +1,21 @@ +use hyper::{Request, Body, Method}; use std::path::Path; use std::io::BufRead; -pub struct ExampleRequest { +pub struct ExampleParts { pub method: String, pub path_and_query: String, pub body: Option, } -pub fn load_request_by_name(name: &str) -> ExampleRequest { +pub fn load_parts_by_name(name: &str) -> ExampleParts { let path = format!("tests/files/{}.txt", name); let path = Path::new(&path); - load_request(&path) + load_parts(&path) } -pub fn load_request(path: &Path) -> ExampleRequest { +pub fn load_parts(path: &Path) -> ExampleParts { let file = std::fs::File::open(path).unwrap(); let mut lines = std::io::BufReader::new(file).lines(); @@ -26,9 +27,21 @@ pub fn load_request(path: &Path) -> ExampleRequest { Some(body.join("\n")) }; - ExampleRequest { + ExampleParts { method: first_line[0].clone(), path_and_query: first_line[1].clone(), body: body, } } + +pub fn load_request(path: &Path) -> Request { + let parts = load_parts(path); + Request::builder() + .uri(parts.path_and_query) + .method(Method::from_bytes(parts.method.as_bytes()).expect("valid method in example text file")) + .body(match parts.body { + Some(data) => Body::from(data), + None => Body::empty(), + }) + .expect("constructing upstream request") +} diff --git a/tests/files/GET_highlight.txt b/tests/files/GET_highlight.txt index 15346fb..8dc23ea 100644 --- a/tests/files/GET_highlight.txt +++ b/tests/files/GET_highlight.txt @@ -1,4 +1,4 @@ -GET my-index-000001/_search +GET /my-index-000001/_search { "query": { "match_phrase": { "message": "number 1" } diff --git a/tests/files/GET_highlight_complex.txt b/tests/files/GET_highlight_complex.txt index 94e4221..288aa9f 100644 --- a/tests/files/GET_highlight_complex.txt +++ b/tests/files/GET_highlight_complex.txt @@ -1,4 +1,4 @@ -GET /_search +GET /some-index/_search { "query" : { "match": { "user.id": "kimchy" } diff --git a/tests/files/GET_highlight_fields.txt b/tests/files/GET_highlight_fields.txt index bbd474a..85b55cb 100644 --- a/tests/files/GET_highlight_fields.txt +++ b/tests/files/GET_highlight_fields.txt @@ -1,4 +1,4 @@ -GET /_search +GET /some-index/_search { "query": { "query_string": { diff --git a/tests/files/GET_highlight_query.txt b/tests/files/GET_highlight_query.txt index 2540b45..7c7f0ea 100644 --- a/tests/files/GET_highlight_query.txt +++ b/tests/files/GET_highlight_query.txt @@ -1,4 +1,4 @@ -GET /_search +GET /some-index/_search { "query": { "match": { diff --git a/tests/files/GET_search.txt b/tests/files/GET_search.txt index 1ed6157..7ff7ca6 100644 --- a/tests/files/GET_search.txt +++ b/tests/files/GET_search.txt @@ -1,4 +1,4 @@ -GET /_search +GET /some-index/_search { "query": { "match": { diff --git a/tests/files/GET_search_bool.txt b/tests/files/GET_search_bool.txt index f04ac5a..3f402f9 100644 --- a/tests/files/GET_search_bool.txt +++ b/tests/files/GET_search_bool.txt @@ -1,4 +1,4 @@ -GET _search +GET /some-index/_search { "query": { "bool": { diff --git a/tests/files/GET_search_boosting.txt b/tests/files/GET_search_boosting.txt index 4db1814..9045a54 100644 --- a/tests/files/GET_search_boosting.txt +++ b/tests/files/GET_search_boosting.txt @@ -1,4 +1,4 @@ -GET /_search +GET /some-index/_search { "query": { "boosting": { diff --git a/tests/files/GET_search_docvalues.txt b/tests/files/GET_search_docvalues.txt index 5b92b39..2fd57b1 100644 --- a/tests/files/GET_search_docvalues.txt +++ b/tests/files/GET_search_docvalues.txt @@ -1,4 +1,4 @@ -GET /_search +GET /some-index/_search { "query": { "match_all": {} diff --git a/tests/files/GET_search_multi_match.txt b/tests/files/GET_search_multi_match.txt index 17aca77..cf07050 100644 --- a/tests/files/GET_search_multi_match.txt +++ b/tests/files/GET_search_multi_match.txt @@ -1,4 +1,4 @@ -GET /_search +GET /some-index/_search { "query": { "multi_match" : { diff --git a/tests/files/POST_search_inner.txt b/tests/files/POST_search_inner.txt index b6b3804..56c5acc 100644 --- a/tests/files/POST_search_inner.txt +++ b/tests/files/POST_search_inner.txt @@ -1,4 +1,4 @@ -POST test/_search +POST /test/_search { "query": { "nested": { diff --git a/tests/files/POST_search_rescore.txt b/tests/files/POST_search_rescore.txt index 81fcd96..071cf5f 100644 --- a/tests/files/POST_search_rescore.txt +++ b/tests/files/POST_search_rescore.txt @@ -1,4 +1,4 @@ -POST /_search +POST /some-index/_search { "query" : { "match" : { diff --git a/tests/files/POST_search_sort_mode.txt b/tests/files/POST_search_sort_mode.txt index 1d6988f..54155ca 100644 --- a/tests/files/POST_search_sort_mode.txt +++ b/tests/files/POST_search_sort_mode.txt @@ -1,4 +1,4 @@ -POST /_search +POST /some-index/_search { "query" : { "term" : { "product" : "chocolate" } diff --git a/tests/parse_es_requests.rs b/tests/parse_es_requests.rs index c34dfad..bd38ce6 100644 --- a/tests/parse_es_requests.rs +++ b/tests/parse_es_requests.rs @@ -2,27 +2,28 @@ use std::fs; use std::ffi::OsStr; use es_public_proxy::parse::{ScrollBody, SearchBody}; +use es_public_proxy::{ProxyConfig, filter_request}; mod common; #[test] fn basic_load() { - let request = common::load_request_by_name("GET_search"); - assert_eq!(request.method, "GET"); - assert_eq!(request.path_and_query, "/_search"); + let parts = common::load_parts_by_name("GET_search"); + assert_eq!(parts.method, "GET"); + assert_eq!(parts.path_and_query, "/some-index/_search"); } #[test] fn basic_parse() { - let request = common::load_request_by_name("GET_search"); - assert_eq!(request.method, "GET"); - assert_eq!(request.path_and_query, "/_search"); + let parts = common::load_parts_by_name("GET_search"); + assert_eq!(parts.method, "GET"); + assert_eq!(parts.path_and_query, "/some-index/_search"); - let _parsed: SearchBody = serde_json::from_str(&request.body.unwrap()).unwrap(); + let _parsed: SearchBody = serde_json::from_str(&parts.body.unwrap()).unwrap(); } #[test] -fn parse_search_requests() { +fn parse_search_bodies() { let file_paths = fs::read_dir("tests/files").unwrap(); @@ -31,8 +32,8 @@ fn parse_search_requests() { if path.extension() != Some(OsStr::new("txt")) { continue } - let request = common::load_request(&path); - if let Some(body) = request.body { + let parts = common::load_parts(&path); + if let Some(body) = parts.body { println!("parsing: {}", path.display()); println!("BODY: {}", body); let _parsed: SearchBody = serde_json::from_str(&body).unwrap(); @@ -41,7 +42,7 @@ fn parse_search_requests() { } #[test] -fn parse_scroll_requests() { +fn parse_scroll_bodies() { let file_paths = fs::read_dir("tests/files/scroll").unwrap(); @@ -50,11 +51,30 @@ fn parse_scroll_requests() { 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 parts = common::load_parts(&path); + if let Some(body) = parts.body { + println!(" parsing: {}", path.display()); + //println!("BODY: {}", body); let _parsed: ScrollBody = serde_json::from_str(&body).unwrap(); } } } + +#[test] +fn filter_search_requests() { + + let file_paths = fs::read_dir("tests/files").unwrap(); + let mut config = ProxyConfig::default(); + config.allow_all_indices = Some(true); + let mut rt = tokio::runtime::Runtime::new().unwrap(); + + for path in file_paths { + let path = path.unwrap().path(); + if path.extension() != Some(OsStr::new("txt")) { + continue + } + println!(" filtering: {}", path.display()); + let req = common::load_request(&path); + rt.block_on(filter_request(req, &config)).unwrap(); + } +} -- cgit v1.2.3