mockforge-core 0.3.116

Shared logic for MockForge - routing, validation, latency, proxy
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
//! Insomnia export import functionality
//!
//! This module handles parsing Insomnia exports and converting them
//! to MockForge routes and configurations.

use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use std::collections::HashMap;

/// Insomnia export structure
#[derive(Debug, Deserialize)]
pub struct InsomniaExport {
    /// Export format version
    #[serde(rename = "__export_format")]
    pub export_format: Option<i32>,
    /// Export type identifier
    #[serde(rename = "_type")]
    pub export_type: Option<String>,
    /// Array of resources (requests, environments, folders, etc.)
    pub resources: Vec<InsomniaResource>,
}

/// Generic Insomnia resource (request, folder, environment, etc.)
#[derive(Debug, Deserialize)]
pub struct InsomniaResource {
    /// Unique resource identifier
    #[serde(rename = "_id")]
    pub id: String,
    /// Resource type (request, folder, environment, etc.)
    #[serde(rename = "_type")]
    pub resource_type: String,
    /// Parent resource ID (for nested resources)
    pub parent_id: Option<String>,
    /// Resource name
    pub name: Option<String>,
    /// Request URL (for request resources)
    pub url: Option<String>,
    /// HTTP method (for request resources)
    pub method: Option<String>,
    /// Request headers (for request resources)
    pub headers: Option<Vec<InsomniaHeader>>,
    /// Request body (for request resources)
    pub body: Option<InsomniaBody>,
    /// Authentication configuration (for request resources)
    pub authentication: Option<InsomniaAuth>,
    /// Query/form parameters (for request resources)
    pub parameters: Option<Vec<InsomniaParameter>>,
    /// Environment variable data (for environment resources)
    pub data: Option<Value>,
    /// Environment name (for environment resources)
    pub environment: Option<String>,
}

/// Insomnia header entry
#[derive(Debug, Deserialize)]
pub struct InsomniaHeader {
    /// Header name
    pub name: String,
    /// Header value
    pub value: String,
    /// Whether this header is disabled
    pub disabled: Option<bool>,
}

/// Insomnia request body structure
#[derive(Debug, Deserialize)]
pub struct InsomniaBody {
    /// MIME type of the body
    pub mime_type: Option<String>,
    /// Raw body text
    pub text: Option<String>,
    /// Form data parameters (for form-data bodies)
    pub params: Option<Vec<InsomniaParameter>>,
}

/// Insomnia parameter (query params, form data, etc.)
#[derive(Debug, Deserialize)]
pub struct InsomniaParameter {
    /// Parameter name
    pub name: String,
    /// Parameter value
    pub value: String,
    /// Whether this parameter is disabled
    pub disabled: Option<bool>,
}

/// Insomnia authentication configuration
#[derive(Debug, Deserialize)]
pub struct InsomniaAuth {
    /// Authentication type (bearer, basic, apikey, etc.)
    #[serde(rename = "type")]
    pub auth_type: String,
    /// Whether authentication is disabled
    pub disabled: Option<bool>,
    /// Username (for basic auth)
    pub username: Option<String>,
    /// Password (for basic auth)
    pub password: Option<String>,
    /// Bearer token (for bearer auth)
    pub token: Option<String>,
    /// Token prefix (for bearer auth)
    pub prefix: Option<String>,
    /// API key name (for apikey auth)
    pub key: Option<String>,
    /// API key value (for apikey auth)
    pub value: Option<String>,
    /// OAuth2 access token URL
    #[serde(rename = "accessTokenUrl")]
    pub access_token_url: Option<String>,
    /// OAuth2 client ID
    #[serde(rename = "clientId")]
    pub client_id: Option<String>,
    /// OAuth2 grant type
    #[serde(rename = "grantType")]
    pub grant_type: Option<String>,
    /// OAuth2 access token value (stored after successful OAuth flow)
    #[serde(rename = "accessToken")]
    pub access_token: Option<String>,
}

/// MockForge route structure for import
#[derive(Debug, Serialize)]
pub struct MockForgeRoute {
    /// HTTP method
    pub method: String,
    /// Request path
    pub path: String,
    /// Request headers
    pub headers: HashMap<String, String>,
    /// Optional request body
    pub body: Option<String>,
    /// Mock response for this route
    pub response: MockForgeResponse,
}

/// MockForge response structure
#[derive(Debug, Serialize)]
pub struct MockForgeResponse {
    /// HTTP status code
    pub status: u16,
    /// Response headers
    pub headers: HashMap<String, String>,
    /// Response body
    pub body: Value,
}

/// Result of importing an Insomnia export
#[derive(Debug)]
pub struct InsomniaImportResult {
    /// Converted routes from Insomnia requests
    pub routes: Vec<MockForgeRoute>,
    /// Extracted environment variables
    pub variables: HashMap<String, String>,
    /// Warnings encountered during import
    pub warnings: Vec<String>,
}

/// Import an Insomnia export
pub fn import_insomnia_export(
    content: &str,
    environment: Option<&str>,
) -> Result<InsomniaImportResult, String> {
    let export: InsomniaExport = serde_json::from_str(content)
        .map_err(|e| format!("Failed to parse Insomnia export: {}", e))?;

    // Validate export format
    if let Some(format) = export.export_format {
        if format < 3 {
            return Err("Insomnia export format version 3 or higher is required".to_string());
        }
    }

    let mut routes = Vec::new();
    let mut variables = HashMap::new();
    let mut warnings = Vec::new();

    // Extract environment variables if specified
    if let Some(env_name) = environment {
        extract_environment_variables(&export.resources, env_name, &mut variables);
    } else {
        // Try to find default environment
        extract_environment_variables(&export.resources, "Base Environment", &mut variables);
    }

    // Process all resources to find requests
    for resource in &export.resources {
        if resource.resource_type == "request" {
            match convert_insomnia_request_to_route(resource, &variables) {
                Ok(route) => routes.push(route),
                Err(e) => warnings.push(format!(
                    "Failed to convert request '{}': {}",
                    resource.name.as_deref().unwrap_or("unnamed"),
                    e
                )),
            }
        }
    }

    Ok(InsomniaImportResult {
        routes,
        variables,
        warnings,
    })
}

/// Extract variables from specified environment
fn extract_environment_variables(
    resources: &[InsomniaResource],
    env_name: &str,
    variables: &mut HashMap<String, String>,
) {
    for resource in resources {
        if resource.resource_type == "environment" && resource.name.as_deref() == Some(env_name) {
            if let Some(data) = &resource.data {
                if let Some(obj) = data.as_object() {
                    for (key, value) in obj {
                        if let Some(str_value) = value.as_str() {
                            variables.insert(key.clone(), str_value.to_string());
                        } else if let Some(num_value) = value.as_f64() {
                            variables.insert(key.clone(), num_value.to_string());
                        } else if let Some(bool_value) = value.as_bool() {
                            variables.insert(key.clone(), bool_value.to_string());
                        }
                    }
                }
            }
        }
    }
}

/// Convert an Insomnia request to a MockForge route
fn convert_insomnia_request_to_route(
    resource: &InsomniaResource,
    variables: &HashMap<String, String>,
) -> Result<MockForgeRoute, String> {
    let method = resource.method.as_deref().ok_or("Request missing method")?.to_uppercase();

    let raw_url = resource.url.as_deref().ok_or("Request missing URL")?;

    let url = resolve_variables(raw_url, variables);

    // Extract path from URL
    let path = extract_path_from_url(&url)?;

    // Extract headers
    let mut headers = HashMap::new();
    if let Some(resource_headers) = &resource.headers {
        for header in resource_headers {
            if !header.disabled.unwrap_or(false) && !header.name.is_empty() {
                headers.insert(header.name.clone(), resolve_variables(&header.value, variables));
            }
        }
    }

    // Add authentication headers
    if let Some(auth) = &resource.authentication {
        if !auth.disabled.unwrap_or(false) {
            add_auth_headers(auth, &mut headers, variables);
        }
    }

    // Extract body
    let body = extract_request_body(resource, variables);

    // Generate mock response
    let response = generate_mock_response(&method);

    Ok(MockForgeRoute {
        method,
        path,
        headers,
        body,
        response,
    })
}

/// Extract path from URL, handling full URLs and relative paths
fn extract_path_from_url(url: &str) -> Result<String, String> {
    if let Ok(parsed_url) = url::Url::parse(url) {
        Ok(parsed_url.path().to_string())
    } else if url.starts_with('/') {
        Ok(url.to_string())
    } else {
        // Assume it's a relative path
        Ok(format!("/{}", url))
    }
}

/// Add authentication headers based on Insomnia auth configuration
fn add_auth_headers(
    auth: &InsomniaAuth,
    headers: &mut HashMap<String, String>,
    variables: &HashMap<String, String>,
) {
    match auth.auth_type.as_str() {
        "bearer" => {
            if let Some(token) = &auth.token {
                let resolved_token = resolve_variables(token, variables);
                headers.insert("Authorization".to_string(), format!("Bearer {}", resolved_token));
            }
        }
        "basic" => {
            if let (Some(username), Some(password)) = (&auth.username, &auth.password) {
                let user = resolve_variables(username, variables);
                let pass = resolve_variables(password, variables);
                use base64::{engine::general_purpose, Engine as _};
                let credentials = general_purpose::STANDARD.encode(format!("{}:{}", user, pass));
                headers.insert("Authorization".to_string(), format!("Basic {}", credentials));
            }
        }
        "apikey" => {
            if let (Some(key), Some(value)) = (&auth.key, &auth.value) {
                let resolved_key = resolve_variables(key, variables);
                let resolved_value = resolve_variables(value, variables);
                headers.insert(resolved_key, resolved_value);
            }
        }
        "oauth2" => {
            // OAuth2: use the stored access token if available, otherwise fall back to token field
            if let Some(access_token) = &auth.access_token {
                let resolved = resolve_variables(access_token, variables);
                if !resolved.is_empty() {
                    headers.insert("Authorization".to_string(), format!("Bearer {}", resolved));
                }
            } else if let Some(token) = &auth.token {
                let resolved = resolve_variables(token, variables);
                if !resolved.is_empty() {
                    headers.insert("Authorization".to_string(), format!("Bearer {}", resolved));
                }
            }
        }
        _ => {
            // Unsupported auth types are silently skipped
        }
    }
}

/// Extract request body from Insomnia resource
fn extract_request_body(
    resource: &InsomniaResource,
    variables: &HashMap<String, String>,
) -> Option<String> {
    if let Some(body) = &resource.body {
        if let Some(text) = &body.text {
            return Some(resolve_variables(text, variables));
        }
    }
    None
}

/// Resolve variables in a string (similar to Postman)
fn resolve_variables(input: &str, variables: &HashMap<String, String>) -> String {
    let mut result = input.to_string();
    for (key, value) in variables {
        let pattern = format!("{{{{{}}}}}", key);
        result = result.replace(&pattern, value);
    }
    result
}

/// Generate a mock response for the request
fn generate_mock_response(method: &str) -> MockForgeResponse {
    let mut headers = HashMap::new();
    headers.insert("Content-Type".to_string(), "application/json".to_string());

    let body = match method {
        "GET" => json!({"message": "Mock GET response", "method": "GET"}),
        "POST" => json!({"message": "Mock POST response", "method": "POST", "created": true}),
        "PUT" => json!({"message": "Mock PUT response", "method": "PUT", "updated": true}),
        "DELETE" => json!({"message": "Mock DELETE response", "method": "DELETE", "deleted": true}),
        "PATCH" => json!({"message": "Mock PATCH response", "method": "PATCH", "patched": true}),
        _ => json!({"message": "Mock response", "method": method}),
    };

    MockForgeResponse {
        status: 200,
        headers,
        body,
    }
}

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

    #[test]
    fn test_parse_insomnia_export() {
        let export_json = r#"{
            "__export_format": 4,
            "_type": "export",
            "resources": [
                {
                    "_id": "req_1",
                    "_type": "request",
                    "name": "Get Users",
                    "method": "GET",
                    "url": "{{baseUrl}}/users",
                    "headers": [
                        {"name": "Authorization", "value": "Bearer {{token}}"}
                    ],
                    "authentication": {
                        "type": "bearer",
                        "token": "{{token}}"
                    }
                },
                {
                    "_id": "env_1",
                    "_type": "environment",
                    "name": "Base Environment",
                    "data": {
                        "baseUrl": "https://api.example.com",
                        "token": "test-token"
                    }
                }
            ]
        }"#;

        let result = import_insomnia_export(export_json, Some("Base Environment")).unwrap();

        assert_eq!(result.routes.len(), 1);
        assert_eq!(result.routes[0].method, "GET");
        assert_eq!(result.routes[0].path, "/users");
        assert!(result.routes[0].headers.contains_key("Authorization"));
        assert!(result.variables.contains_key("baseUrl"));
    }

    #[test]
    fn test_insomnia_format_validation() {
        let old_format = r#"{
            "__export_format": 2,
            "resources": []
        }"#;

        let result = import_insomnia_export(old_format, None);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("version 3 or higher"));
    }
}