Skip to main content

mockforge_bench/conformance/
custom.rs

1//! Custom conformance test authoring via YAML
2//!
3//! Allows users to define additional conformance checks beyond the built-in
4//! OpenAPI 3.0.0 feature set. Custom checks are grouped under a "Custom"
5//! category in the conformance report.
6
7use crate::error::{BenchError, Result};
8use serde::Deserialize;
9use std::path::Path;
10
11/// Top-level YAML configuration for custom conformance checks
12#[derive(Debug, Deserialize)]
13pub struct CustomConformanceConfig {
14    /// List of custom checks to run
15    pub custom_checks: Vec<CustomCheck>,
16}
17
18/// A single custom conformance check
19#[derive(Debug, Deserialize)]
20pub struct CustomCheck {
21    /// Check name (should start with "custom:" for report aggregation)
22    pub name: String,
23    /// Request path (e.g., "/api/users")
24    pub path: String,
25    /// HTTP method (GET, POST, PUT, DELETE, etc.)
26    pub method: String,
27    /// Expected HTTP status code
28    pub expected_status: u16,
29    /// Optional request body (JSON string)
30    #[serde(default)]
31    pub body: Option<String>,
32    /// Optional expected response headers (name -> regex pattern)
33    #[serde(default)]
34    pub expected_headers: std::collections::HashMap<String, String>,
35    /// Optional expected body fields with type validation
36    #[serde(default)]
37    pub expected_body_fields: Vec<ExpectedBodyField>,
38    /// Optional request headers
39    #[serde(default)]
40    pub headers: std::collections::HashMap<String, String>,
41}
42
43/// Expected field in the response body with type checking
44#[derive(Debug, Deserialize)]
45pub struct ExpectedBodyField {
46    /// Field name in the JSON response
47    pub name: String,
48    /// Expected JSON type: "string", "integer", "number", "boolean", "array", "object"
49    #[serde(rename = "type")]
50    pub field_type: String,
51}
52
53impl CustomConformanceConfig {
54    /// Parse a custom conformance config from a YAML file
55    pub fn from_file(path: &Path) -> Result<Self> {
56        let content = std::fs::read_to_string(path).map_err(|e| {
57            BenchError::Other(format!(
58                "Failed to read custom conformance file '{}': {}",
59                path.display(),
60                e
61            ))
62        })?;
63        serde_yaml::from_str(&content).map_err(|e| {
64            BenchError::Other(format!(
65                "Failed to parse custom conformance YAML '{}': {}",
66                path.display(),
67                e
68            ))
69        })
70    }
71
72    /// Generate a k6 `group('Custom', ...)` block for all custom checks.
73    ///
74    /// `base_url` is the JS expression for the base URL (e.g., `"BASE_URL"`).
75    /// `custom_headers` are additional headers to inject into every request.
76    pub fn generate_k6_group(&self, base_url: &str, custom_headers: &[(String, String)]) -> String {
77        let mut script = String::with_capacity(4096);
78        script.push_str("  group('Custom', function () {\n");
79
80        for check in &self.custom_checks {
81            script.push_str("    {\n");
82
83            // Build headers object
84            let mut all_headers: Vec<(String, String)> = Vec::new();
85            // Add check-specific headers
86            for (k, v) in &check.headers {
87                all_headers.push((k.clone(), v.clone()));
88            }
89            // Add global custom headers (check-specific take priority)
90            for (k, v) in custom_headers {
91                if !check.headers.contains_key(k) {
92                    all_headers.push((k.clone(), v.clone()));
93                }
94            }
95            // If posting JSON body, add Content-Type
96            if check.body.is_some()
97                && !all_headers.iter().any(|(k, _)| k.eq_ignore_ascii_case("content-type"))
98            {
99                all_headers.push(("Content-Type".to_string(), "application/json".to_string()));
100            }
101
102            let headers_js = if all_headers.is_empty() {
103                "{}".to_string()
104            } else {
105                let entries: Vec<String> = all_headers
106                    .iter()
107                    .map(|(k, v)| format!("'{}': '{}'", k, v.replace('\'', "\\'")))
108                    .collect();
109                format!("{{ {} }}", entries.join(", "))
110            };
111
112            let method = check.method.to_uppercase();
113            let url = format!("${{{}}}{}", base_url, check.path);
114            let escaped_name = check.name.replace('\'', "\\'");
115
116            match method.as_str() {
117                "GET" | "HEAD" | "OPTIONS" | "DELETE" => {
118                    let k6_method = match method.as_str() {
119                        "DELETE" => "del",
120                        other => &other.to_lowercase(),
121                    };
122                    if all_headers.is_empty() {
123                        script
124                            .push_str(&format!("      let res = http.{}(`{}`);\n", k6_method, url));
125                    } else {
126                        script.push_str(&format!(
127                            "      let res = http.{}(`{}`, {{ headers: {} }});\n",
128                            k6_method, url, headers_js
129                        ));
130                    }
131                }
132                _ => {
133                    // POST, PUT, PATCH
134                    let k6_method = method.to_lowercase();
135                    let body_expr = match &check.body {
136                        Some(b) => format!("'{}'", b.replace('\'', "\\'")),
137                        None => "null".to_string(),
138                    };
139                    script.push_str(&format!(
140                        "      let res = http.{}(`{}`, {}, {{ headers: {} }});\n",
141                        k6_method, url, body_expr, headers_js
142                    ));
143                }
144            }
145
146            // Status check
147            script.push_str(&format!(
148                "      check(res, {{ '{}': (r) => r.status === {} }});\n",
149                escaped_name, check.expected_status
150            ));
151
152            // Header checks
153            for (header_name, pattern) in &check.expected_headers {
154                let header_check_name = format!("{}:header:{}", escaped_name, header_name);
155                let escaped_pattern = pattern.replace('\\', "\\\\").replace('\'', "\\'");
156                script.push_str(&format!(
157                    "      check(res, {{ '{}': (r) => new RegExp('{}').test(r.headers['{}'] || r.headers['{}'] || '') }});\n",
158                    header_check_name,
159                    escaped_pattern,
160                    header_name,
161                    header_name.to_lowercase()
162                ));
163            }
164
165            // Body field checks
166            for field in &check.expected_body_fields {
167                let field_check_name =
168                    format!("{}:body:{}:{}", escaped_name, field.name, field.field_type);
169                let type_check = match field.field_type.as_str() {
170                    "string" => format!(
171                        "typeof JSON.parse(r.body)['{}'] === 'string'",
172                        field.name
173                    ),
174                    "integer" => format!(
175                        "Number.isInteger(JSON.parse(r.body)['{}'])",
176                        field.name
177                    ),
178                    "number" => format!(
179                        "typeof JSON.parse(r.body)['{}'] === 'number'",
180                        field.name
181                    ),
182                    "boolean" => format!(
183                        "typeof JSON.parse(r.body)['{}'] === 'boolean'",
184                        field.name
185                    ),
186                    "array" => format!(
187                        "Array.isArray(JSON.parse(r.body)['{}'])",
188                        field.name
189                    ),
190                    "object" => format!(
191                        "typeof JSON.parse(r.body)['{}'] === 'object' && !Array.isArray(JSON.parse(r.body)['{}'])",
192                        field.name, field.name
193                    ),
194                    _ => format!(
195                        "JSON.parse(r.body)['{}'] !== undefined",
196                        field.name
197                    ),
198                };
199                script.push_str(&format!(
200                    "      check(res, {{ '{}': (r) => {{ try {{ return {}; }} catch(e) {{ return false; }} }} }});\n",
201                    field_check_name, type_check
202                ));
203            }
204
205            script.push_str("    }\n");
206        }
207
208        script.push_str("  });\n\n");
209        script
210    }
211}
212
213#[cfg(test)]
214mod tests {
215    use super::*;
216
217    #[test]
218    fn test_parse_custom_yaml() {
219        let yaml = r#"
220custom_checks:
221  - name: "custom:pets-returns-200"
222    path: /pets
223    method: GET
224    expected_status: 200
225  - name: "custom:create-product"
226    path: /api/products
227    method: POST
228    expected_status: 201
229    body: '{"sku": "TEST-001", "name": "Test"}'
230    expected_body_fields:
231      - name: id
232        type: integer
233    expected_headers:
234      content-type: "application/json"
235"#;
236        let config: CustomConformanceConfig = serde_yaml::from_str(yaml).unwrap();
237        assert_eq!(config.custom_checks.len(), 2);
238        assert_eq!(config.custom_checks[0].name, "custom:pets-returns-200");
239        assert_eq!(config.custom_checks[0].expected_status, 200);
240        assert_eq!(config.custom_checks[1].expected_body_fields.len(), 1);
241        assert_eq!(config.custom_checks[1].expected_body_fields[0].name, "id");
242        assert_eq!(config.custom_checks[1].expected_body_fields[0].field_type, "integer");
243    }
244
245    #[test]
246    fn test_generate_k6_group_get() {
247        let config = CustomConformanceConfig {
248            custom_checks: vec![CustomCheck {
249                name: "custom:test-get".to_string(),
250                path: "/api/test".to_string(),
251                method: "GET".to_string(),
252                expected_status: 200,
253                body: None,
254                expected_headers: std::collections::HashMap::new(),
255                expected_body_fields: vec![],
256                headers: std::collections::HashMap::new(),
257            }],
258        };
259
260        let script = config.generate_k6_group("BASE_URL", &[]);
261        assert!(script.contains("group('Custom'"));
262        assert!(script.contains("http.get(`${BASE_URL}/api/test`)"));
263        assert!(script.contains("'custom:test-get': (r) => r.status === 200"));
264    }
265
266    #[test]
267    fn test_generate_k6_group_post_with_body() {
268        let config = CustomConformanceConfig {
269            custom_checks: vec![CustomCheck {
270                name: "custom:create".to_string(),
271                path: "/api/items".to_string(),
272                method: "POST".to_string(),
273                expected_status: 201,
274                body: Some(r#"{"name": "test"}"#.to_string()),
275                expected_headers: std::collections::HashMap::new(),
276                expected_body_fields: vec![ExpectedBodyField {
277                    name: "id".to_string(),
278                    field_type: "integer".to_string(),
279                }],
280                headers: std::collections::HashMap::new(),
281            }],
282        };
283
284        let script = config.generate_k6_group("BASE_URL", &[]);
285        assert!(script.contains("http.post("));
286        assert!(script.contains("'custom:create': (r) => r.status === 201"));
287        assert!(script.contains("custom:create:body:id:integer"));
288        assert!(script.contains("Number.isInteger"));
289    }
290
291    #[test]
292    fn test_generate_k6_group_with_header_checks() {
293        let mut expected_headers = std::collections::HashMap::new();
294        expected_headers.insert("content-type".to_string(), "application/json".to_string());
295
296        let config = CustomConformanceConfig {
297            custom_checks: vec![CustomCheck {
298                name: "custom:header-check".to_string(),
299                path: "/api/test".to_string(),
300                method: "GET".to_string(),
301                expected_status: 200,
302                body: None,
303                expected_headers,
304                expected_body_fields: vec![],
305                headers: std::collections::HashMap::new(),
306            }],
307        };
308
309        let script = config.generate_k6_group("BASE_URL", &[]);
310        assert!(script.contains("custom:header-check:header:content-type"));
311        assert!(script.contains("new RegExp('application/json')"));
312    }
313
314    #[test]
315    fn test_generate_k6_group_with_custom_headers() {
316        let config = CustomConformanceConfig {
317            custom_checks: vec![CustomCheck {
318                name: "custom:auth-test".to_string(),
319                path: "/api/secure".to_string(),
320                method: "GET".to_string(),
321                expected_status: 200,
322                body: None,
323                expected_headers: std::collections::HashMap::new(),
324                expected_body_fields: vec![],
325                headers: std::collections::HashMap::new(),
326            }],
327        };
328
329        let custom_headers = vec![("Authorization".to_string(), "Bearer token123".to_string())];
330        let script = config.generate_k6_group("BASE_URL", &custom_headers);
331        assert!(script.contains("'Authorization': 'Bearer token123'"));
332    }
333
334    #[test]
335    fn test_from_file_nonexistent() {
336        let result = CustomConformanceConfig::from_file(Path::new("/nonexistent/file.yaml"));
337        assert!(result.is_err());
338        let err = result.unwrap_err().to_string();
339        assert!(err.contains("Failed to read custom conformance file"));
340    }
341
342    #[test]
343    fn test_generate_k6_group_delete() {
344        let config = CustomConformanceConfig {
345            custom_checks: vec![CustomCheck {
346                name: "custom:delete-item".to_string(),
347                path: "/api/items/1".to_string(),
348                method: "DELETE".to_string(),
349                expected_status: 204,
350                body: None,
351                expected_headers: std::collections::HashMap::new(),
352                expected_body_fields: vec![],
353                headers: std::collections::HashMap::new(),
354            }],
355        };
356
357        let script = config.generate_k6_group("BASE_URL", &[]);
358        assert!(script.contains("http.del("));
359        assert!(script.contains("r.status === 204"));
360    }
361}