mockforge-http 0.3.116

HTTP/REST protocol support 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//! Specification Import API
//!
//! Provides REST endpoints for importing OpenAPI and AsyncAPI specifications
//! and automatically generating mock endpoints.

use axum::{
    extract::{Multipart, Query, State},
    http::StatusCode,
    response::Json,
    routing::{delete, get, post},
    Router,
};
use mockforge_core::import::asyncapi_import::{import_asyncapi_spec, AsyncApiImportResult};
use mockforge_core::import::openapi_import::{import_openapi_spec, OpenApiImportResult};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::*;

/// Specification metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpecMetadata {
    /// Unique identifier for the specification
    pub id: String,
    /// Human-readable name of the specification
    pub name: String,
    /// Type of specification (OpenAPI or AsyncAPI)
    pub spec_type: SpecType,
    /// Specification version
    pub version: String,
    /// Optional description from the spec
    pub description: Option<String>,
    /// List of server URLs from the spec
    pub servers: Vec<String>,
    /// ISO 8601 timestamp when the spec was uploaded
    pub uploaded_at: String,
    /// Number of routes/channels generated from this spec
    pub route_count: usize,
}

/// Specification type
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum SpecType {
    /// OpenAPI/Swagger specification
    OpenApi,
    /// AsyncAPI specification
    AsyncApi,
}

/// Import request body for uploading a specification
#[derive(Debug, Serialize, Deserialize)]
pub struct ImportSpecRequest {
    /// Specification content (YAML or JSON)
    pub spec_content: String,
    /// Optional specification type (auto-detected if not provided)
    pub spec_type: Option<SpecType>,
    /// Optional custom name for the specification
    pub name: Option<String>,
    /// Optional base URL override
    pub base_url: Option<String>,
    /// Whether to automatically generate mock endpoints (default: true)
    pub auto_generate_mocks: Option<bool>,
}

/// Response from specification import
#[derive(Debug, Serialize)]
pub struct ImportSpecResponse {
    /// ID of the imported specification
    pub spec_id: String,
    /// Type of specification that was imported
    pub spec_type: SpecType,
    /// Number of routes/channels generated
    pub routes_generated: usize,
    /// Warnings encountered during import
    pub warnings: Vec<String>,
    /// Coverage statistics for the imported spec
    pub coverage: CoverageStats,
}

/// Coverage statistics for imported specification
#[derive(Debug, Serialize)]
pub struct CoverageStats {
    /// Total number of endpoints in the specification
    pub total_endpoints: usize,
    /// Number of endpoints that were successfully mocked
    pub mocked_endpoints: usize,
    /// Coverage percentage (0-100)
    pub coverage_percentage: u32,
    /// Breakdown by HTTP method (for OpenAPI) or operation type (for AsyncAPI)
    pub by_method: HashMap<String, usize>,
}

/// Query parameters for listing specifications
#[derive(Debug, Deserialize)]
pub struct ListSpecsQuery {
    /// Optional filter by specification type
    pub spec_type: Option<SpecType>,
    /// Maximum number of results to return
    pub limit: Option<usize>,
    /// Offset for pagination
    pub offset: Option<usize>,
}

/// Shared state for spec import API
#[derive(Clone)]
pub struct SpecImportState {
    /// Map of spec ID to stored specification
    pub specs: Arc<RwLock<HashMap<String, StoredSpec>>>,
}

/// Stored specification with metadata and routes
#[derive(Debug, Clone)]
pub struct StoredSpec {
    /// Specification metadata
    pub metadata: SpecMetadata,
    /// Original specification content
    pub content: String,
    /// Serialized routes/channels as JSON
    pub routes_json: String,
}

impl SpecImportState {
    /// Create a new specification import state
    pub fn new() -> Self {
        Self {
            specs: Arc::new(RwLock::new(HashMap::new())),
        }
    }
}

impl Default for SpecImportState {
    fn default() -> Self {
        Self::new()
    }
}

/// Create spec import router
pub fn spec_import_router(state: SpecImportState) -> Router {
    Router::new()
        .route("/specs", post(import_spec))
        .route("/specs", get(list_specs))
        .route("/specs/{id}", get(get_spec))
        .route("/specs/{id}", delete(delete_spec))
        .route("/specs/{id}/coverage", get(get_spec_coverage))
        .route("/specs/{id}/routes", get(get_spec_routes))
        .route("/specs/upload", post(upload_spec_file))
        .with_state(state)
}

/// Import a specification from JSON body
#[instrument(skip(state, payload))]
async fn import_spec(
    State(state): State<SpecImportState>,
    Json(payload): Json<ImportSpecRequest>,
) -> Result<Json<ImportSpecResponse>, (StatusCode, String)> {
    info!("Importing specification");

    // Auto-detect spec type if not provided
    let spec_type = if let Some(st) = payload.spec_type {
        st
    } else {
        detect_spec_type(&payload.spec_content)
            .map_err(|e| (StatusCode::BAD_REQUEST, format!("Failed to detect spec type: {}", e)))?
    };

    // Convert YAML to JSON if needed
    let json_content = if is_yaml(&payload.spec_content) {
        yaml_to_json(&payload.spec_content)
            .map_err(|e| (StatusCode::BAD_REQUEST, format!("Failed to parse YAML: {}", e)))?
    } else {
        payload.spec_content.clone()
    };

    // Import based on type
    let (metadata, openapi_result, asyncapi_result) = match spec_type {
        SpecType::OpenApi => {
            let result =
                import_openapi_spec(&json_content, payload.base_url.as_deref()).map_err(|e| {
                    (StatusCode::BAD_REQUEST, format!("Failed to import OpenAPI spec: {}", e))
                })?;

            let metadata = SpecMetadata {
                id: generate_spec_id(),
                name: payload.name.unwrap_or_else(|| result.spec_info.title.clone()),
                spec_type: SpecType::OpenApi,
                version: result.spec_info.version.clone(),
                description: result.spec_info.description.clone(),
                servers: result.spec_info.servers.clone(),
                uploaded_at: chrono::Utc::now().to_rfc3339(),
                route_count: result.routes.len(),
            };

            (metadata, Some(result), None)
        }
        SpecType::AsyncApi => {
            let result = import_asyncapi_spec(&payload.spec_content, payload.base_url.as_deref())
                .map_err(|e| {
                (StatusCode::BAD_REQUEST, format!("Failed to import AsyncAPI spec: {}", e))
            })?;

            let metadata = SpecMetadata {
                id: generate_spec_id(),
                name: payload.name.unwrap_or_else(|| result.spec_info.title.clone()),
                spec_type: SpecType::AsyncApi,
                version: result.spec_info.version.clone(),
                description: result.spec_info.description.clone(),
                servers: result.spec_info.servers.clone(),
                uploaded_at: chrono::Utc::now().to_rfc3339(),
                route_count: result.channels.len(),
            };

            (metadata, None, Some(result))
        }
    };

    let spec_id = metadata.id.clone();

    // Build response and serialize routes
    let (routes_generated, warnings, coverage, routes_json) =
        if let Some(ref result) = openapi_result {
            let coverage = calculate_openapi_coverage(result);
            let routes_json = serde_json::to_string(&result.routes).map_err(|e| {
                (StatusCode::INTERNAL_SERVER_ERROR, format!("Failed to serialize routes: {}", e))
            })?;
            (result.routes.len(), result.warnings.clone(), coverage, routes_json)
        } else if let Some(ref result) = asyncapi_result {
            let coverage = calculate_asyncapi_coverage(result);
            let routes_json = serde_json::to_string(&result.channels).map_err(|e| {
                (
                    StatusCode::INTERNAL_SERVER_ERROR,
                    format!("Failed to serialize channels: {}", e),
                )
            })?;
            (result.channels.len(), result.warnings.clone(), coverage, routes_json)
        } else {
            (
                0,
                vec![],
                CoverageStats {
                    total_endpoints: 0,
                    mocked_endpoints: 0,
                    coverage_percentage: 0,
                    by_method: HashMap::new(),
                },
                "[]".to_string(),
            )
        };

    // Store the spec
    let stored_spec = StoredSpec {
        metadata: metadata.clone(),
        content: payload.spec_content,
        routes_json,
    };

    state.specs.write().await.insert(spec_id.clone(), stored_spec);

    info!("Specification imported successfully: {}", spec_id);

    Ok(Json(ImportSpecResponse {
        spec_id,
        spec_type,
        routes_generated,
        warnings,
        coverage,
    }))
}

/// Upload a specification file (multipart form data)
#[instrument(skip(state, multipart))]
async fn upload_spec_file(
    State(state): State<SpecImportState>,
    mut multipart: Multipart,
) -> Result<Json<ImportSpecResponse>, (StatusCode, String)> {
    info!("Uploading specification file");

    let mut spec_content = None;
    let mut name = None;
    let mut base_url = None;

    while let Some(field) = multipart
        .next_field()
        .await
        .map_err(|e| (StatusCode::BAD_REQUEST, format!("Failed to read multipart field: {}", e)))?
    {
        let field_name = field.name().unwrap_or("").to_string();

        match field_name.as_str() {
            "file" => {
                let data = field.bytes().await.map_err(|e| {
                    (StatusCode::BAD_REQUEST, format!("Failed to read file: {}", e))
                })?;
                spec_content = Some(
                    String::from_utf8(data.to_vec())
                        .map_err(|e| (StatusCode::BAD_REQUEST, format!("Invalid UTF-8: {}", e)))?,
                );
            }
            "name" => {
                name = Some(field.text().await.map_err(|e| {
                    (StatusCode::BAD_REQUEST, format!("Failed to read name: {}", e))
                })?);
            }
            "base_url" => {
                base_url = Some(field.text().await.map_err(|e| {
                    (StatusCode::BAD_REQUEST, format!("Failed to read base_url: {}", e))
                })?);
            }
            _ => {}
        }
    }

    let spec_content =
        spec_content.ok_or((StatusCode::BAD_REQUEST, "Missing 'file' field".to_string()))?;

    // Call import_spec with the extracted data
    let request = ImportSpecRequest {
        spec_content,
        spec_type: None,
        name,
        base_url,
        auto_generate_mocks: Some(true),
    };

    import_spec(State(state), Json(request)).await
}

/// List all imported specifications
#[instrument(skip(state))]
async fn list_specs(
    State(state): State<SpecImportState>,
    Query(params): Query<ListSpecsQuery>,
) -> Json<Vec<SpecMetadata>> {
    let specs = state.specs.read().await;

    let mut metadata_list: Vec<SpecMetadata> = specs
        .values()
        .filter(|spec| {
            if let Some(ref spec_type) = params.spec_type {
                &spec.metadata.spec_type == spec_type
            } else {
                true
            }
        })
        .map(|spec| spec.metadata.clone())
        .collect();

    // Sort by uploaded_at descending
    metadata_list.sort_by(|a, b| b.uploaded_at.cmp(&a.uploaded_at));

    // Apply pagination
    let offset = params.offset.unwrap_or(0);
    let limit = params.limit.unwrap_or(100);

    let paginated: Vec<SpecMetadata> = metadata_list.into_iter().skip(offset).take(limit).collect();

    Json(paginated)
}

/// Get specification details
#[instrument(skip(state))]
async fn get_spec(
    State(state): State<SpecImportState>,
    axum::extract::Path(id): axum::extract::Path<String>,
) -> Result<Json<SpecMetadata>, StatusCode> {
    let specs = state.specs.read().await;

    specs
        .get(&id)
        .map(|spec| Json(spec.metadata.clone()))
        .ok_or(StatusCode::NOT_FOUND)
}

/// Delete a specification
#[instrument(skip(state))]
async fn delete_spec(
    State(state): State<SpecImportState>,
    axum::extract::Path(id): axum::extract::Path<String>,
) -> Result<StatusCode, StatusCode> {
    let mut specs = state.specs.write().await;

    if specs.remove(&id).is_some() {
        info!("Deleted specification: {}", id);
        Ok(StatusCode::NO_CONTENT)
    } else {
        Err(StatusCode::NOT_FOUND)
    }
}

/// Get coverage statistics for a spec
#[instrument(skip(state))]
async fn get_spec_coverage(
    State(state): State<SpecImportState>,
    axum::extract::Path(id): axum::extract::Path<String>,
) -> Result<Json<CoverageStats>, StatusCode> {
    let specs = state.specs.read().await;

    let spec = specs.get(&id).ok_or(StatusCode::NOT_FOUND)?;

    // Parse routes to calculate coverage with method breakdown
    let by_method =
        if let Ok(routes) = serde_json::from_str::<Vec<serde_json::Value>>(&spec.routes_json) {
            let mut method_counts: HashMap<String, usize> = HashMap::new();
            for route in &routes {
                if let Some(method) = route.get("method").and_then(|m| m.as_str()) {
                    *method_counts.entry(method.to_uppercase()).or_insert(0) += 1;
                } else if let Some(operation) = route.get("operation").and_then(|o| o.as_str()) {
                    // AsyncAPI uses operation types (publish, subscribe)
                    *method_counts.entry(operation.to_string()).or_insert(0) += 1;
                }
            }
            method_counts
        } else {
            HashMap::new()
        };

    let total_endpoints = spec.metadata.route_count;
    let mocked_endpoints = by_method.values().sum::<usize>().max(total_endpoints);
    let coverage_percentage = if total_endpoints > 0 {
        ((mocked_endpoints as f64 / total_endpoints as f64) * 100.0).min(100.0) as u32
    } else {
        0
    };

    let coverage = CoverageStats {
        total_endpoints,
        mocked_endpoints,
        coverage_percentage,
        by_method,
    };

    Ok(Json(coverage))
}

/// Get routes generated from a spec
#[instrument(skip(state))]
async fn get_spec_routes(
    State(state): State<SpecImportState>,
    axum::extract::Path(id): axum::extract::Path<String>,
) -> Result<Json<serde_json::Value>, StatusCode> {
    let specs = state.specs.read().await;

    let spec = specs.get(&id).ok_or(StatusCode::NOT_FOUND)?;

    let routes: serde_json::Value =
        serde_json::from_str(&spec.routes_json).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;

    Ok(Json(routes))
}

// Helper functions

fn generate_spec_id() -> String {
    use std::time::{SystemTime, UNIX_EPOCH};
    let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis();
    format!("spec-{}", timestamp)
}

fn detect_spec_type(content: &str) -> Result<SpecType, String> {
    // Try parsing as JSON
    if let Ok(json) = serde_json::from_str::<serde_json::Value>(content) {
        if json.get("openapi").is_some() {
            return Ok(SpecType::OpenApi);
        } else if json.get("asyncapi").is_some() {
            return Ok(SpecType::AsyncApi);
        }
    }

    // Try parsing as YAML
    if let Ok(yaml) = serde_yaml::from_str::<serde_json::Value>(content) {
        if yaml.get("openapi").is_some() {
            return Ok(SpecType::OpenApi);
        } else if yaml.get("asyncapi").is_some() {
            return Ok(SpecType::AsyncApi);
        }
    }

    Err("Unable to detect specification type".to_string())
}

fn is_yaml(content: &str) -> bool {
    // Simple heuristic: if it doesn't start with '{' or '[', assume YAML
    let trimmed = content.trim_start();
    !trimmed.starts_with('{') && !trimmed.starts_with('[')
}

fn yaml_to_json(yaml_content: &str) -> Result<String, String> {
    let yaml_value: serde_json::Value =
        serde_yaml::from_str(yaml_content).map_err(|e| format!("Failed to parse YAML: {}", e))?;
    serde_json::to_string(&yaml_value).map_err(|e| format!("Failed to convert to JSON: {}", e))
}

fn calculate_openapi_coverage(result: &OpenApiImportResult) -> CoverageStats {
    let total_endpoints = result.routes.len();
    let mocked_endpoints = result.routes.len(); // All routes have mocks

    let mut by_method = HashMap::new();
    for route in &result.routes {
        *by_method.entry(route.method.clone()).or_insert(0) += 1;
    }

    CoverageStats {
        total_endpoints,
        mocked_endpoints,
        coverage_percentage: 100,
        by_method,
    }
}

fn calculate_asyncapi_coverage(result: &AsyncApiImportResult) -> CoverageStats {
    let total_endpoints = result.channels.len();
    let mocked_endpoints = result.channels.len();

    let mut by_method = HashMap::new();
    for channel in &result.channels {
        let protocol = format!("{:?}", channel.protocol);
        *by_method.entry(protocol).or_insert(0) += 1;
    }

    CoverageStats {
        total_endpoints,
        mocked_endpoints,
        coverage_percentage: 100,
        by_method,
    }
}

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

    #[test]
    fn test_detect_openapi_json() {
        let content = r#"{"openapi": "3.0.0", "info": {"title": "Test", "version": "1.0.0"}}"#;
        assert_eq!(detect_spec_type(content).unwrap(), SpecType::OpenApi);
    }

    #[test]
    fn test_detect_asyncapi_json() {
        let content = r#"{"asyncapi": "2.0.0", "info": {"title": "Test", "version": "1.0.0"}}"#;
        assert_eq!(detect_spec_type(content).unwrap(), SpecType::AsyncApi);
    }

    #[test]
    fn test_is_yaml() {
        assert!(is_yaml("openapi: 3.0.0"));
        assert!(!is_yaml("{\"openapi\": \"3.0.0\"}"));
        assert!(!is_yaml("[1, 2, 3]"));
    }
}