summaryrefslogtreecommitdiffstats
path: root/tests/parse_es_requests.rs
blob: c8081c7a4022ab40b57d89393b40ab8a7120f79d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use es_public_proxy::parse::{ScrollBody, SearchBody};
use es_public_proxy::{filter_request, ProxyConfig};
use std::ffi::OsStr;
use std::fs;

mod common;

#[test]
fn basic_load() {
    let parts = common::load_parts(std::path::Path::new("tests/files/search/GET_search.txt"));
    assert_eq!(parts.method, "GET");
    assert_eq!(parts.path_and_query, "/some-index/_search");
}

#[test]
fn parse_search_bodies() {
    let file_paths = fs::read_dir("tests/files/search").unwrap();

    for path in file_paths {
        let path = path.unwrap().path();
        if path.extension() != Some(OsStr::new("txt")) {
            continue;
        }
        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();
            let raw_val: serde_json::Value = serde_json::from_str(&body).unwrap();
            assert_eq!(raw_val, serde_json::to_value(parsed).unwrap());
        }
    }
}

#[test]
fn parse_scroll_bodies() {
    let file_paths = fs::read_dir("tests/files/scroll").unwrap();

    for path in file_paths {
        let path = path.unwrap().path();
        if path.extension() != Some(OsStr::new("txt")) {
            continue;
        }
        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/search").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();
    }
}

#[test]
fn filter_scroll_requests() {
    let file_paths = fs::read_dir("tests/files/scroll").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();
    }
}

#[test]
fn filter_failures() {
    let file_paths = fs::read_dir("tests/files/search_fail").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);
        let result = rt.block_on(filter_request(req, &config));
        assert!(result.is_err());
    }

    let file_paths = fs::read_dir("tests/files/scroll_fail").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);
        let result = rt.block_on(filter_request(req, &config));
        assert!(result.is_err());
    }
}