mockforge-bench 0.3.133

Load and performance testing for MockForge
Documentation
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! Conformance feature definitions and bundled reference spec

/// OpenAPI 3.0.0 feature categories for conformance testing
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ConformanceFeature {
    // Parameters
    PathParamString,
    PathParamInteger,
    QueryParamString,
    QueryParamInteger,
    QueryParamArray,
    HeaderParam,
    CookieParam,
    // Request Bodies
    BodyJson,
    BodyFormUrlencoded,
    BodyMultipart,
    // Schema Types
    SchemaString,
    SchemaInteger,
    SchemaNumber,
    SchemaBoolean,
    SchemaArray,
    SchemaObject,
    // Composition
    CompositionOneOf,
    CompositionAnyOf,
    CompositionAllOf,
    // String Formats
    FormatDate,
    FormatDateTime,
    FormatEmail,
    FormatUuid,
    FormatUri,
    FormatIpv4,
    FormatIpv6,
    // Constraints
    ConstraintRequired,
    ConstraintOptional,
    ConstraintMinMax,
    ConstraintPattern,
    ConstraintEnum,
    // Response Codes
    Response200,
    Response201,
    Response204,
    Response400,
    Response404,
    // HTTP Methods
    MethodGet,
    MethodPost,
    MethodPut,
    MethodPatch,
    MethodDelete,
    MethodHead,
    MethodOptions,
    // Content Negotiation
    ContentNegotiation,
    // Security
    SecurityBearer,
    SecurityApiKey,
    SecurityBasic,
    // Response Validation (spec-driven mode)
    ResponseValidation,
}

impl ConformanceFeature {
    /// Get the category name for this feature
    pub fn category(&self) -> &'static str {
        match self {
            Self::PathParamString
            | Self::PathParamInteger
            | Self::QueryParamString
            | Self::QueryParamInteger
            | Self::QueryParamArray
            | Self::HeaderParam
            | Self::CookieParam => "Parameters",
            Self::BodyJson | Self::BodyFormUrlencoded | Self::BodyMultipart => "Request Bodies",
            Self::SchemaString
            | Self::SchemaInteger
            | Self::SchemaNumber
            | Self::SchemaBoolean
            | Self::SchemaArray
            | Self::SchemaObject => "Schema Types",
            Self::CompositionOneOf | Self::CompositionAnyOf | Self::CompositionAllOf => {
                "Composition"
            }
            Self::FormatDate
            | Self::FormatDateTime
            | Self::FormatEmail
            | Self::FormatUuid
            | Self::FormatUri
            | Self::FormatIpv4
            | Self::FormatIpv6 => "String Formats",
            Self::ConstraintRequired
            | Self::ConstraintOptional
            | Self::ConstraintMinMax
            | Self::ConstraintPattern
            | Self::ConstraintEnum => "Constraints",
            Self::Response200
            | Self::Response201
            | Self::Response204
            | Self::Response400
            | Self::Response404 => "Response Codes",
            Self::MethodGet
            | Self::MethodPost
            | Self::MethodPut
            | Self::MethodPatch
            | Self::MethodDelete
            | Self::MethodHead
            | Self::MethodOptions => "HTTP Methods",
            Self::ContentNegotiation => "Content Types",
            Self::SecurityBearer | Self::SecurityApiKey | Self::SecurityBasic => "Security",
            Self::ResponseValidation => "Response Validation",
        }
    }

    /// Get the check name used in k6 scripts (maps back from k6 output)
    pub fn check_name(&self) -> &'static str {
        match self {
            Self::PathParamString => "param:path:string",
            Self::PathParamInteger => "param:path:integer",
            Self::QueryParamString => "param:query:string",
            Self::QueryParamInteger => "param:query:integer",
            Self::QueryParamArray => "param:query:array",
            Self::HeaderParam => "param:header",
            Self::CookieParam => "param:cookie",
            Self::BodyJson => "body:json",
            Self::BodyFormUrlencoded => "body:form-urlencoded",
            Self::BodyMultipart => "body:multipart",
            Self::SchemaString => "schema:string",
            Self::SchemaInteger => "schema:integer",
            Self::SchemaNumber => "schema:number",
            Self::SchemaBoolean => "schema:boolean",
            Self::SchemaArray => "schema:array",
            Self::SchemaObject => "schema:object",
            Self::CompositionOneOf => "composition:oneOf",
            Self::CompositionAnyOf => "composition:anyOf",
            Self::CompositionAllOf => "composition:allOf",
            Self::FormatDate => "format:date",
            Self::FormatDateTime => "format:date-time",
            Self::FormatEmail => "format:email",
            Self::FormatUuid => "format:uuid",
            Self::FormatUri => "format:uri",
            Self::FormatIpv4 => "format:ipv4",
            Self::FormatIpv6 => "format:ipv6",
            Self::ConstraintRequired => "constraint:required",
            Self::ConstraintOptional => "constraint:optional",
            Self::ConstraintMinMax => "constraint:minmax",
            Self::ConstraintPattern => "constraint:pattern",
            Self::ConstraintEnum => "constraint:enum",
            Self::Response200 => "response:200",
            Self::Response201 => "response:201",
            Self::Response204 => "response:204",
            Self::Response400 => "response:400",
            Self::Response404 => "response:404",
            Self::MethodGet => "method:GET",
            Self::MethodPost => "method:POST",
            Self::MethodPut => "method:PUT",
            Self::MethodPatch => "method:PATCH",
            Self::MethodDelete => "method:DELETE",
            Self::MethodHead => "method:HEAD",
            Self::MethodOptions => "method:OPTIONS",
            Self::ContentNegotiation => "content:negotiation",
            Self::SecurityBearer => "security:bearer",
            Self::SecurityApiKey => "security:apikey",
            Self::SecurityBasic => "security:basic",
            Self::ResponseValidation => "response:schema:validation",
        }
    }

    /// All feature variants
    pub fn all() -> &'static [ConformanceFeature] {
        &[
            Self::PathParamString,
            Self::PathParamInteger,
            Self::QueryParamString,
            Self::QueryParamInteger,
            Self::QueryParamArray,
            Self::HeaderParam,
            Self::CookieParam,
            Self::BodyJson,
            Self::BodyFormUrlencoded,
            Self::BodyMultipart,
            Self::SchemaString,
            Self::SchemaInteger,
            Self::SchemaNumber,
            Self::SchemaBoolean,
            Self::SchemaArray,
            Self::SchemaObject,
            Self::CompositionOneOf,
            Self::CompositionAnyOf,
            Self::CompositionAllOf,
            Self::FormatDate,
            Self::FormatDateTime,
            Self::FormatEmail,
            Self::FormatUuid,
            Self::FormatUri,
            Self::FormatIpv4,
            Self::FormatIpv6,
            Self::ConstraintRequired,
            Self::ConstraintOptional,
            Self::ConstraintMinMax,
            Self::ConstraintPattern,
            Self::ConstraintEnum,
            Self::Response200,
            Self::Response201,
            Self::Response204,
            Self::Response400,
            Self::Response404,
            Self::MethodGet,
            Self::MethodPost,
            Self::MethodPut,
            Self::MethodPatch,
            Self::MethodDelete,
            Self::MethodHead,
            Self::MethodOptions,
            Self::ContentNegotiation,
            Self::SecurityBearer,
            Self::SecurityApiKey,
            Self::SecurityBasic,
            Self::ResponseValidation,
        ]
    }

    /// All unique categories
    pub fn categories() -> &'static [&'static str] {
        &[
            "Parameters",
            "Request Bodies",
            "Schema Types",
            "Composition",
            "String Formats",
            "Constraints",
            "Response Codes",
            "HTTP Methods",
            "Content Types",
            "Security",
            "Response Validation",
        ]
    }

    /// Convert a CLI category name (lowercase, hyphenated) to the canonical category name
    pub fn category_from_cli_name(name: &str) -> Option<&'static str> {
        match name.to_lowercase().replace('_', "-").as_str() {
            "parameters" => Some("Parameters"),
            "request-bodies" => Some("Request Bodies"),
            "schema-types" => Some("Schema Types"),
            "composition" => Some("Composition"),
            "string-formats" => Some("String Formats"),
            "constraints" => Some("Constraints"),
            "response-codes" => Some("Response Codes"),
            "http-methods" => Some("HTTP Methods"),
            "content-types" => Some("Content Types"),
            "security" => Some("Security"),
            "response-validation" => Some("Response Validation"),
            _ => None,
        }
    }

    /// All valid CLI category names with their canonical names
    pub fn cli_category_names() -> Vec<(&'static str, &'static str)> {
        vec![
            ("parameters", "Parameters"),
            ("request-bodies", "Request Bodies"),
            ("schema-types", "Schema Types"),
            ("composition", "Composition"),
            ("string-formats", "String Formats"),
            ("constraints", "Constraints"),
            ("response-codes", "Response Codes"),
            ("http-methods", "HTTP Methods"),
            ("content-types", "Content Types"),
            ("security", "Security"),
            ("response-validation", "Response Validation"),
        ]
    }

    /// Related OWASP API Security Top 10 (2023) categories
    pub fn related_owasp(&self) -> &'static [&'static str] {
        match self {
            // Security features → Broken Authentication
            Self::SecurityBearer | Self::SecurityApiKey | Self::SecurityBasic => &["API2:2023"],
            // Path parameters → BOLA + Security Misconfiguration
            Self::PathParamString | Self::PathParamInteger => &["API1:2023", "API8:2023"],
            // Other parameters → Security Misconfiguration
            Self::QueryParamString
            | Self::QueryParamInteger
            | Self::QueryParamArray
            | Self::HeaderParam
            | Self::CookieParam => &["API8:2023"],
            // Request bodies → Resource Consumption + Security Misconfiguration
            Self::BodyJson | Self::BodyFormUrlencoded | Self::BodyMultipart => {
                &["API4:2023", "API8:2023"]
            }
            // Required/Optional constraints → Broken Object Property Auth + Misconfiguration
            Self::ConstraintRequired | Self::ConstraintOptional => &["API3:2023", "API8:2023"],
            // Validation constraints → Resource Consumption + Misconfiguration
            Self::ConstraintMinMax | Self::ConstraintPattern | Self::ConstraintEnum => {
                &["API4:2023", "API8:2023"]
            }
            // Schema types → Security Misconfiguration
            Self::SchemaString
            | Self::SchemaInteger
            | Self::SchemaNumber
            | Self::SchemaBoolean
            | Self::SchemaArray
            | Self::SchemaObject => &["API8:2023"],
            // String formats → Security Misconfiguration
            Self::FormatDate
            | Self::FormatDateTime
            | Self::FormatEmail
            | Self::FormatUuid
            | Self::FormatUri
            | Self::FormatIpv4
            | Self::FormatIpv6 => &["API8:2023"],
            // Composition → Security Misconfiguration
            Self::CompositionOneOf | Self::CompositionAnyOf | Self::CompositionAllOf => {
                &["API8:2023"]
            }
            // Error response codes → Misconfiguration + Improper Inventory
            Self::Response400 | Self::Response404 => &["API8:2023", "API9:2023"],
            // Success response codes — no direct OWASP mapping
            Self::Response200 | Self::Response201 | Self::Response204 => &[],
            // HTTP methods → Broken Function Auth + Improper Inventory
            Self::MethodGet
            | Self::MethodPost
            | Self::MethodPut
            | Self::MethodPatch
            | Self::MethodDelete
            | Self::MethodHead
            | Self::MethodOptions => &["API5:2023", "API9:2023"],
            // Content negotiation → Security Misconfiguration
            Self::ContentNegotiation => &["API8:2023"],
            // Response validation → Misconfiguration + Unsafe Consumption
            Self::ResponseValidation => &["API8:2023", "API10:2023"],
        }
    }

    /// Get an actionable hint for expanding coverage in a category.
    /// Used in the report when a category has 0 tests detected from the spec.
    pub fn category_hint(category: &str) -> &'static str {
        match category {
            "Parameters" => "Add path, query, header, or cookie parameters to your operations",
            "Request Bodies" => "Add requestBody with JSON, form, or multipart content to POST/PUT/PATCH operations",
            "Schema Types" => "Use typed properties (string, integer, number, boolean, array, object) in your schemas",
            "Composition" => "Use oneOf, anyOf, or allOf schema composition in your models",
            "String Formats" => "Add format annotations (date, email, uuid, uri, ipv4, etc.) to string schemas",
            "Constraints" => "Add required fields, min/max, pattern, or enum constraints to your schemas",
            "Response Codes" => "Define explicit 200, 201, 204, 400, and 404 responses on your operations",
            "HTTP Methods" => "Use GET, POST, PUT, PATCH, DELETE, HEAD, and OPTIONS methods in your paths",
            "Content Types" => "Serve multiple content types or add Accept header negotiation",
            "Security" => "Define securitySchemes (bearer, apiKey, basic) in your components",
            "Response Validation" => "Add response schemas so MockForge can validate response structure",
            _ => "Expand your OpenAPI spec to cover this category",
        }
    }

    /// Count the number of features in a given category
    pub fn features_in_category(category: &str) -> usize {
        Self::all().iter().filter(|f| f.category() == category).count()
    }

    /// Get the OpenAPI spec URL for this feature's category (used in SARIF reports)
    pub fn spec_url(&self) -> &'static str {
        match self.category() {
            "Parameters" => "https://spec.openapis.org/oas/v3.0.0#parameter-object",
            "Request Bodies" => "https://spec.openapis.org/oas/v3.0.0#request-body-object",
            "Schema Types" => "https://spec.openapis.org/oas/v3.0.0#schema-object",
            "Composition" => "https://spec.openapis.org/oas/v3.0.0#schema-object",
            "String Formats" => "https://spec.openapis.org/oas/v3.0.0#data-types",
            "Constraints" => "https://spec.openapis.org/oas/v3.0.0#schema-object",
            "Response Codes" => "https://spec.openapis.org/oas/v3.0.0#responses-object",
            "HTTP Methods" => "https://spec.openapis.org/oas/v3.0.0#path-item-object",
            "Content Types" => "https://spec.openapis.org/oas/v3.0.0#media-type-object",
            "Security" => "https://spec.openapis.org/oas/v3.0.0#security-scheme-object",
            "Response Validation" => "https://spec.openapis.org/oas/v3.0.0#response-object",
            _ => "https://spec.openapis.org/oas/v3.0.0",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_all_features_have_categories() {
        for feature in ConformanceFeature::all() {
            assert!(!feature.category().is_empty());
            assert!(!feature.check_name().is_empty());
        }
    }

    #[test]
    fn test_all_categories_covered() {
        let categories: std::collections::HashSet<&str> =
            ConformanceFeature::all().iter().map(|f| f.category()).collect();
        for cat in ConformanceFeature::categories() {
            assert!(categories.contains(cat), "Category '{}' has no features", cat);
        }
    }

    #[test]
    fn test_category_from_cli_name() {
        assert_eq!(ConformanceFeature::category_from_cli_name("parameters"), Some("Parameters"));
        assert_eq!(
            ConformanceFeature::category_from_cli_name("request-bodies"),
            Some("Request Bodies")
        );
        assert_eq!(
            ConformanceFeature::category_from_cli_name("schema-types"),
            Some("Schema Types")
        );
        assert_eq!(ConformanceFeature::category_from_cli_name("PARAMETERS"), Some("Parameters"));
        assert_eq!(
            ConformanceFeature::category_from_cli_name("Request-Bodies"),
            Some("Request Bodies")
        );
        assert_eq!(ConformanceFeature::category_from_cli_name("invalid"), None);
    }

    #[test]
    fn test_cli_category_names_complete() {
        let cli_names = ConformanceFeature::cli_category_names();
        let categories = ConformanceFeature::categories();
        assert_eq!(cli_names.len(), categories.len());
        for (_, canonical) in &cli_names {
            assert!(
                categories.contains(canonical),
                "CLI name maps to unknown category: {}",
                canonical
            );
        }
    }

    #[test]
    fn test_related_owasp_valid_identifiers() {
        let pattern = regex::Regex::new(r"^API\d+:2023$").unwrap();
        for feature in ConformanceFeature::all() {
            for owasp_id in feature.related_owasp() {
                assert!(
                    pattern.is_match(owasp_id),
                    "Feature {:?} has invalid OWASP identifier: {}",
                    feature,
                    owasp_id
                );
            }
        }
    }

    #[test]
    fn test_security_features_map_to_api2() {
        assert!(ConformanceFeature::SecurityBearer.related_owasp().contains(&"API2:2023"));
        assert!(ConformanceFeature::SecurityApiKey.related_owasp().contains(&"API2:2023"));
        assert!(ConformanceFeature::SecurityBasic.related_owasp().contains(&"API2:2023"));
    }

    #[test]
    fn test_spec_urls_not_empty() {
        for feature in ConformanceFeature::all() {
            assert!(!feature.spec_url().is_empty(), "Feature {:?} has empty spec URL", feature);
        }
    }
}