relentless_http/
command.rs

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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use std::marker::PhantomData;

use bytes::Bytes;
use http_body::Body;
use relentless::interface::command::{Assault, Relentless};
use serde::{Deserialize, Serialize};

use crate::{evaluate::HttpResponse, factory::HttpRequest, record::HttpIoRecorder};

#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct HttpAssault<ReqB, ResB> {
    relentless: Relentless,
    phantom: PhantomData<(ReqB, ResB)>,
}
impl<ReqB, ResB> Assault<http::Request<ReqB>, http::Response<ResB>> for HttpAssault<ReqB, ResB>
where
    ReqB: Body + From<Bytes> + Default,
    ResB: Body + From<Bytes> + Default,
    ResB::Error: std::error::Error + Send + Sync + 'static,
{
    type Request = HttpRequest;
    type Response = HttpResponse;
    type Recorder = HttpIoRecorder;

    fn command(&self) -> &Relentless {
        &self.relentless
    }
    fn recorder(&self) -> Self::Recorder {
        HttpIoRecorder
    }
}

impl<ReqB, ResB> HttpAssault<ReqB, ResB> {
    pub fn new(relentless: Relentless) -> Self {
        Self { relentless, phantom: PhantomData }
    }
}

#[cfg(test)]
#[cfg(all(feature = "yaml", feature = "json"))]
mod tests {
    use relentless::{
        assault::{
            destinations::{AllOr, Destinations},
            evaluator::json::JsonEvaluator,
        },
        interface::config::{Config, Format, Setting, Severity, Testcase, WorkerConfig},
    };

    use crate::{
        evaluate::{HttpBody, HttpHeaders, HttpResponse},
        factory::HttpRequest,
    };

    #[test]
    fn test_config_roundtrip() {
        let example = Config {
            worker_config: WorkerConfig {
                name: Some("example".to_string()),
                setting: Setting {
                    request: HttpRequest::default(),
                    response: HttpResponse { header: HttpHeaders::Ignore, ..Default::default() },
                    ..Default::default()
                },
                ..Default::default()
            },
            testcases: vec![Testcase {
                description: Some("test description".to_string()),
                target: "/information".to_string(),
                setting: Setting {
                    request: HttpRequest::default(),
                    allow: Some(true),
                    response: HttpResponse {
                        body: HttpBody::Json(JsonEvaluator {
                            ignore: vec!["/datetime".to_string()],
                            // patch: Some(PatchTo::All(
                            //     serde_json::from_value(
                            //         serde_json::json!([{"op": "replace", "path": "/datetime", "value": "2021-01-01"}]),
                            //     )
                            //     .unwrap(),
                            // )),
                            patch: Some(AllOr::Destinations(Destinations::from_iter([
                                (
                                    "actual".to_string(),
                                    serde_json::from_value(serde_json::json!([{"op": "remove", "path": "/datetime"}]))
                                        .unwrap(),
                                ),
                                (
                                    "expect".to_string(),
                                    serde_json::from_value(serde_json::json!([{"op": "remove", "path": "/datetime"}]))
                                        .unwrap(),
                                ),
                            ]))),
                            patch_fail: Some(Severity::Deny),
                        }),
                        ..Default::default()
                    },
                    ..Default::default()
                },
            }],
        };
        let yaml = serde_yaml::to_string(&example).unwrap();
        // println!("{}", yaml);

        let round_trip = Config::read_str(&yaml, Format::Yaml).unwrap();
        assert_eq!(example, round_trip);
    }

    #[test]
    fn test_config_json_patch() {
        let all_yaml = r#"
        name: json patch to all
        destinations:
          actual: http://localhost:3000
          expect: http://localhost:3000
        testcases:
        - description: test description
          target: /information
          setting:
            response:
              body:
                json:
                  patch:
                  - op: replace
                    path: /datetime
                    value: 2021-01-01
        "#;
        let config = Config::read_str(all_yaml, Format::Yaml).unwrap();
        assert_eq!(
            config.testcases[0].setting,
            Setting {
                request: HttpRequest::default(),
                response: HttpResponse {
                    body: HttpBody::Json(JsonEvaluator {
                        ignore: vec![],
                        patch: Some(AllOr::All(
                            serde_json::from_value(
                                serde_json::json!([{"op": "replace", "path": "/datetime", "value": "2021-01-01"}])
                            )
                            .unwrap(),
                        )),
                        patch_fail: None,
                    }),
                    ..Default::default()
                },
                ..Default::default()
            },
        );

        let destinations_yaml = r#"
        name: json patch to destinations
        destinations:
          actual: http://localhost:3000
          expect: http://localhost:3000
        testcases:
        - description: test description
          target: /information
          setting:
            response:
              body:
                json:
                  patch:
                    actual:
                    - op: remove
                      path: /datetime
                    expect:
                    - op: remove
                      path: /datetime
                  patch-fail: warn
        "#;
        let config = Config::read_str(destinations_yaml, Format::Yaml).unwrap();
        assert_eq!(
            config.testcases[0].setting,
            Setting {
                request: HttpRequest::default(),
                response: HttpResponse {
                    body: HttpBody::Json(JsonEvaluator {
                        ignore: vec![],
                        patch: Some(AllOr::Destinations(Destinations::from_iter([
                            (
                                "actual".to_string(),
                                serde_json::from_value(serde_json::json!([{"op": "remove", "path": "/datetime"}]))
                                    .unwrap(),
                            ),
                            (
                                "expect".to_string(),
                                serde_json::from_value(serde_json::json!([{"op": "remove", "path": "/datetime"}]))
                                    .unwrap(),
                            ),
                        ]))),
                        patch_fail: Some(Severity::Warn),
                    }),
                    ..Default::default()
                },
                ..Default::default()
            },
        );
    }
}