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 with failure detail capture
147            script.push_str(&format!(
148                "      {{ let ok = check(res, {{ '{}': (r) => r.status === {} }}); if (!ok) __captureFailure('{}', res, 'status === {}'); }}\n",
149                escaped_name, check.expected_status, escaped_name, check.expected_status
150            ));
151
152            // Header checks with failure detail capture
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                    "      {{ let ok = check(res, {{ '{}': (r) => new RegExp('{}').test(r.headers['{}'] || r.headers['{}'] || '') }}); if (!ok) __captureFailure('{}', res, 'header {} matches /{}/ '); }}\n",
158                    header_check_name,
159                    escaped_pattern,
160                    header_name,
161                    header_name.to_lowercase(),
162                    header_check_name,
163                    header_name,
164                    escaped_pattern
165                ));
166            }
167
168            // Body field checks
169            for field in &check.expected_body_fields {
170                let field_check_name =
171                    format!("{}:body:{}:{}", escaped_name, field.name, field.field_type);
172                let type_check = match field.field_type.as_str() {
173                    "string" => format!(
174                        "typeof JSON.parse(r.body)['{}'] === 'string'",
175                        field.name
176                    ),
177                    "integer" => format!(
178                        "Number.isInteger(JSON.parse(r.body)['{}'])",
179                        field.name
180                    ),
181                    "number" => format!(
182                        "typeof JSON.parse(r.body)['{}'] === 'number'",
183                        field.name
184                    ),
185                    "boolean" => format!(
186                        "typeof JSON.parse(r.body)['{}'] === 'boolean'",
187                        field.name
188                    ),
189                    "array" => format!(
190                        "Array.isArray(JSON.parse(r.body)['{}'])",
191                        field.name
192                    ),
193                    "object" => format!(
194                        "typeof JSON.parse(r.body)['{}'] === 'object' && !Array.isArray(JSON.parse(r.body)['{}'])",
195                        field.name, field.name
196                    ),
197                    _ => format!(
198                        "JSON.parse(r.body)['{}'] !== undefined",
199                        field.name
200                    ),
201                };
202                script.push_str(&format!(
203                    "      {{ let ok = check(res, {{ '{}': (r) => {{ try {{ return {}; }} catch(e) {{ return false; }} }} }}); if (!ok) __captureFailure('{}', res, 'body field {} is {}'); }}\n",
204                    field_check_name, type_check, field_check_name, field.name, field.field_type
205                ));
206            }
207
208            script.push_str("    }\n");
209        }
210
211        script.push_str("  });\n\n");
212        script
213    }
214}
215
216#[cfg(test)]
217mod tests {
218    use super::*;
219
220    #[test]
221    fn test_parse_custom_yaml() {
222        let yaml = r#"
223custom_checks:
224  - name: "custom:pets-returns-200"
225    path: /pets
226    method: GET
227    expected_status: 200
228  - name: "custom:create-product"
229    path: /api/products
230    method: POST
231    expected_status: 201
232    body: '{"sku": "TEST-001", "name": "Test"}'
233    expected_body_fields:
234      - name: id
235        type: integer
236    expected_headers:
237      content-type: "application/json"
238"#;
239        let config: CustomConformanceConfig = serde_yaml::from_str(yaml).unwrap();
240        assert_eq!(config.custom_checks.len(), 2);
241        assert_eq!(config.custom_checks[0].name, "custom:pets-returns-200");
242        assert_eq!(config.custom_checks[0].expected_status, 200);
243        assert_eq!(config.custom_checks[1].expected_body_fields.len(), 1);
244        assert_eq!(config.custom_checks[1].expected_body_fields[0].name, "id");
245        assert_eq!(config.custom_checks[1].expected_body_fields[0].field_type, "integer");
246    }
247
248    #[test]
249    fn test_generate_k6_group_get() {
250        let config = CustomConformanceConfig {
251            custom_checks: vec![CustomCheck {
252                name: "custom:test-get".to_string(),
253                path: "/api/test".to_string(),
254                method: "GET".to_string(),
255                expected_status: 200,
256                body: None,
257                expected_headers: std::collections::HashMap::new(),
258                expected_body_fields: vec![],
259                headers: std::collections::HashMap::new(),
260            }],
261        };
262
263        let script = config.generate_k6_group("BASE_URL", &[]);
264        assert!(script.contains("group('Custom'"));
265        assert!(script.contains("http.get(`${BASE_URL}/api/test`)"));
266        assert!(script.contains("'custom:test-get': (r) => r.status === 200"));
267    }
268
269    #[test]
270    fn test_generate_k6_group_post_with_body() {
271        let config = CustomConformanceConfig {
272            custom_checks: vec![CustomCheck {
273                name: "custom:create".to_string(),
274                path: "/api/items".to_string(),
275                method: "POST".to_string(),
276                expected_status: 201,
277                body: Some(r#"{"name": "test"}"#.to_string()),
278                expected_headers: std::collections::HashMap::new(),
279                expected_body_fields: vec![ExpectedBodyField {
280                    name: "id".to_string(),
281                    field_type: "integer".to_string(),
282                }],
283                headers: std::collections::HashMap::new(),
284            }],
285        };
286
287        let script = config.generate_k6_group("BASE_URL", &[]);
288        assert!(script.contains("http.post("));
289        assert!(script.contains("'custom:create': (r) => r.status === 201"));
290        assert!(script.contains("custom:create:body:id:integer"));
291        assert!(script.contains("Number.isInteger"));
292    }
293
294    #[test]
295    fn test_generate_k6_group_with_header_checks() {
296        let mut expected_headers = std::collections::HashMap::new();
297        expected_headers.insert("content-type".to_string(), "application/json".to_string());
298
299        let config = CustomConformanceConfig {
300            custom_checks: vec![CustomCheck {
301                name: "custom:header-check".to_string(),
302                path: "/api/test".to_string(),
303                method: "GET".to_string(),
304                expected_status: 200,
305                body: None,
306                expected_headers,
307                expected_body_fields: vec![],
308                headers: std::collections::HashMap::new(),
309            }],
310        };
311
312        let script = config.generate_k6_group("BASE_URL", &[]);
313        assert!(script.contains("custom:header-check:header:content-type"));
314        assert!(script.contains("new RegExp('application/json')"));
315    }
316
317    #[test]
318    fn test_generate_k6_group_with_custom_headers() {
319        let config = CustomConformanceConfig {
320            custom_checks: vec![CustomCheck {
321                name: "custom:auth-test".to_string(),
322                path: "/api/secure".to_string(),
323                method: "GET".to_string(),
324                expected_status: 200,
325                body: None,
326                expected_headers: std::collections::HashMap::new(),
327                expected_body_fields: vec![],
328                headers: std::collections::HashMap::new(),
329            }],
330        };
331
332        let custom_headers = vec![("Authorization".to_string(), "Bearer token123".to_string())];
333        let script = config.generate_k6_group("BASE_URL", &custom_headers);
334        assert!(script.contains("'Authorization': 'Bearer token123'"));
335    }
336
337    #[test]
338    fn test_failure_capture_emitted() {
339        let config = CustomConformanceConfig {
340            custom_checks: vec![CustomCheck {
341                name: "custom:capture-test".to_string(),
342                path: "/api/test".to_string(),
343                method: "GET".to_string(),
344                expected_status: 200,
345                body: None,
346                expected_headers: {
347                    let mut m = std::collections::HashMap::new();
348                    m.insert("X-Rate-Limit".to_string(), ".*".to_string());
349                    m
350                },
351                expected_body_fields: vec![ExpectedBodyField {
352                    name: "id".to_string(),
353                    field_type: "integer".to_string(),
354                }],
355                headers: std::collections::HashMap::new(),
356            }],
357        };
358
359        let script = config.generate_k6_group("BASE_URL", &[]);
360        // Status check should call __captureFailure on failure
361        assert!(
362            script.contains("__captureFailure('custom:capture-test', res, 'status === 200')"),
363            "Status check should emit __captureFailure"
364        );
365        // Header check should call __captureFailure on failure
366        assert!(
367            script.contains("__captureFailure('custom:capture-test:header:X-Rate-Limit'"),
368            "Header check should emit __captureFailure"
369        );
370        // Body field check should call __captureFailure on failure
371        assert!(
372            script.contains("__captureFailure('custom:capture-test:body:id:integer'"),
373            "Body field check should emit __captureFailure"
374        );
375    }
376
377    #[test]
378    fn test_from_file_nonexistent() {
379        let result = CustomConformanceConfig::from_file(Path::new("/nonexistent/file.yaml"));
380        assert!(result.is_err());
381        let err = result.unwrap_err().to_string();
382        assert!(err.contains("Failed to read custom conformance file"));
383    }
384
385    #[test]
386    fn test_generate_k6_group_delete() {
387        let config = CustomConformanceConfig {
388            custom_checks: vec![CustomCheck {
389                name: "custom:delete-item".to_string(),
390                path: "/api/items/1".to_string(),
391                method: "DELETE".to_string(),
392                expected_status: 204,
393                body: None,
394                expected_headers: std::collections::HashMap::new(),
395                expected_body_fields: vec![],
396                headers: std::collections::HashMap::new(),
397            }],
398        };
399
400        let script = config.generate_k6_group("BASE_URL", &[]);
401        assert!(script.contains("http.del("));
402        assert!(script.contains("r.status === 204"));
403    }
404}