mockforge-core 0.3.115

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
439
440
441
442
443
//! Edge case tests for request chaining
//!
//! These tests cover error paths, edge cases, and boundary conditions
//! for request chaining functionality.

use mockforge_core::request_chaining::{ChainContext, ChainResponse, RequestBody};
use serde_json::json;

/// Test ChainContext creation and basic operations
#[test]
fn test_chain_context_new() {
    let context = ChainContext::new();
    assert!(context.responses.is_empty());
    assert!(context.variables.is_empty());
    assert!(context.metadata.is_empty());
}

/// Test ChainContext default
#[test]
fn test_chain_context_default() {
    let context = ChainContext::default();
    assert!(context.responses.is_empty());
}

/// Test storing and retrieving responses
#[test]
fn test_chain_context_store_get_response() {
    let mut context = ChainContext::new();

    let response = ChainResponse {
        status: 200,
        headers: std::collections::HashMap::new(),
        body: Some(json!({"id": 123})),
        duration_ms: 150,
        executed_at: "2023-01-01T00:00:00Z".to_string(),
        error: None,
    };

    context.store_response("login".to_string(), response.clone());

    let retrieved = context.get_response("login");
    assert!(retrieved.is_some());
    assert_eq!(retrieved.unwrap().status, 200);

    // Test non-existent response
    assert!(context.get_response("nonexistent").is_none());
}

/// Test storing and retrieving variables
#[test]
fn test_chain_context_variables() {
    let mut context = ChainContext::new();

    context.set_variable("user_id".to_string(), json!(12345));
    context.set_variable("token".to_string(), json!("abc123"));

    assert_eq!(context.get_variable("user_id"), Some(&json!(12345)));
    assert_eq!(context.get_variable("token"), Some(&json!("abc123")));
    assert!(context.get_variable("nonexistent").is_none());
}

/// Test storing and retrieving metadata
#[test]
fn test_chain_context_metadata() {
    let mut context = ChainContext::new();

    context.set_metadata("chain_id".to_string(), "chain-123".to_string());
    context.set_metadata("execution_mode".to_string(), "sequential".to_string());

    assert_eq!(context.get_metadata("chain_id"), Some(&"chain-123".to_string()));
    assert_eq!(context.get_metadata("execution_mode"), Some(&"sequential".to_string()));
    assert!(context.get_metadata("nonexistent").is_none());
}

/// Test RequestBody JSON creation
#[test]
fn test_request_body_json() {
    let body = RequestBody::json(json!({"name": "test", "value": 42}));

    match body {
        RequestBody::Json(value) => {
            assert_eq!(value.get("name"), Some(&json!("test")));
            assert_eq!(value.get("value"), Some(&json!(42)));
        }
        _ => panic!("Expected Json variant"),
    }
}

/// Test RequestBody binary file creation
#[test]
fn test_request_body_binary_file() {
    let body = RequestBody::binary_file(
        "/path/to/file.bin".to_string(),
        Some("application/octet-stream".to_string()),
    );

    match body {
        RequestBody::BinaryFile { path, content_type } => {
            assert_eq!(path, "/path/to/file.bin");
            assert_eq!(content_type, Some("application/octet-stream".to_string()));
        }
        _ => panic!("Expected BinaryFile variant"),
    }
}

/// Test RequestBody binary file without content type
#[test]
fn test_request_body_binary_file_no_content_type() {
    let body = RequestBody::binary_file("/path/to/file.bin".to_string(), None);

    match body {
        RequestBody::BinaryFile { path, content_type } => {
            assert_eq!(path, "/path/to/file.bin");
            assert!(content_type.is_none());
        }
        _ => panic!("Expected BinaryFile variant"),
    }
}

/// Test RequestBody content type for JSON
#[test]
fn test_request_body_content_type_json() {
    let body = RequestBody::json(json!({}));
    assert_eq!(body.content_type(), Some("application/json"));
}

/// Test RequestBody content type for binary file
#[test]
fn test_request_body_content_type_binary() {
    let body =
        RequestBody::binary_file("/path/to/file.bin".to_string(), Some("image/png".to_string()));
    assert_eq!(body.content_type(), Some("image/png"));
}

/// Test RequestBody content type for binary file without specified type
#[test]
fn test_request_body_content_type_binary_none() {
    let body = RequestBody::binary_file("/path/to/file.bin".to_string(), None);
    assert_eq!(body.content_type(), None);
}

/// Test ChainResponse with all fields
#[test]
fn test_chain_response_complete() {
    let mut headers = std::collections::HashMap::new();
    headers.insert("content-type".to_string(), "application/json".to_string());

    let response = ChainResponse {
        status: 201,
        headers,
        body: Some(json!({"created": true})),
        duration_ms: 250,
        executed_at: "2023-01-01T12:00:00Z".to_string(),
        error: None,
    };

    assert_eq!(response.status, 201);
    assert_eq!(response.headers.len(), 1);
    assert_eq!(response.body, Some(json!({"created": true})));
    assert_eq!(response.duration_ms, 250);
    assert_eq!(response.executed_at, "2023-01-01T12:00:00Z");
    assert!(response.error.is_none());
}

/// Test ChainResponse with error
#[test]
fn test_chain_response_with_error() {
    let response = ChainResponse {
        status: 500,
        headers: std::collections::HashMap::new(),
        body: None,
        duration_ms: 100,
        executed_at: "2023-01-01T12:00:00Z".to_string(),
        error: Some("Connection timeout".to_string()),
    };

    assert_eq!(response.status, 500);
    assert!(response.body.is_none());
    assert_eq!(response.error, Some("Connection timeout".to_string()));
}

/// Test ChainResponse with empty body
#[test]
fn test_chain_response_empty_body() {
    let response = ChainResponse {
        status: 204,
        headers: std::collections::HashMap::new(),
        body: None,
        duration_ms: 50,
        executed_at: "2023-01-01T12:00:00Z".to_string(),
        error: None,
    };

    assert_eq!(response.status, 204);
    assert!(response.body.is_none());
}

/// Test ChainContext overwriting responses
#[test]
fn test_chain_context_overwrite_response() {
    let mut context = ChainContext::new();

    let response1 = ChainResponse {
        status: 200,
        headers: std::collections::HashMap::new(),
        body: Some(json!({"version": 1})),
        duration_ms: 100,
        executed_at: "2023-01-01T00:00:00Z".to_string(),
        error: None,
    };

    let response2 = ChainResponse {
        status: 200,
        headers: std::collections::HashMap::new(),
        body: Some(json!({"version": 2})),
        duration_ms: 150,
        executed_at: "2023-01-01T01:00:00Z".to_string(),
        error: None,
    };

    context.store_response("update".to_string(), response1);
    context.store_response("update".to_string(), response2);

    // Should have the latest response
    let retrieved = context.get_response("update").unwrap();
    assert_eq!(retrieved.body, Some(json!({"version": 2})));
}

/// Test ChainContext overwriting variables
#[test]
fn test_chain_context_overwrite_variable() {
    let mut context = ChainContext::new();

    context.set_variable("counter".to_string(), json!(1));
    context.set_variable("counter".to_string(), json!(2));

    assert_eq!(context.get_variable("counter"), Some(&json!(2)));
}

/// Test ChainContext with multiple responses
#[test]
fn test_chain_context_multiple_responses() {
    let mut context = ChainContext::new();

    let response1 = ChainResponse {
        status: 200,
        headers: std::collections::HashMap::new(),
        body: Some(json!({"step": "login"})),
        duration_ms: 100,
        executed_at: "2023-01-01T00:00:00Z".to_string(),
        error: None,
    };

    let response2 = ChainResponse {
        status: 201,
        headers: std::collections::HashMap::new(),
        body: Some(json!({"step": "create"})),
        duration_ms: 200,
        executed_at: "2023-01-01T00:00:01Z".to_string(),
        error: None,
    };

    context.store_response("step1".to_string(), response1);
    context.store_response("step2".to_string(), response2);

    assert_eq!(context.responses.len(), 2);
    assert_eq!(context.get_response("step1").unwrap().status, 200);
    assert_eq!(context.get_response("step2").unwrap().status, 201);
}

/// Test ChainContext with complex JSON variables
#[test]
fn test_chain_context_complex_variables() {
    let mut context = ChainContext::new();

    let complex_value = json!({
        "user": {
            "id": 123,
            "name": "test",
            "roles": ["admin", "user"]
        },
        "metadata": {
            "created": "2023-01-01",
            "tags": ["important"]
        }
    });

    context.set_variable("user_data".to_string(), complex_value.clone());

    let retrieved = context.get_variable("user_data").unwrap();
    assert_eq!(retrieved, &complex_value);
}

/// Test ChainResponse with various status codes
#[test]
fn test_chain_response_status_codes() {
    let status_codes = vec![200, 201, 204, 400, 401, 403, 404, 500, 502, 503];

    for status in status_codes {
        let response = ChainResponse {
            status,
            headers: std::collections::HashMap::new(),
            body: None,
            duration_ms: 0,
            executed_at: "2023-01-01T00:00:00Z".to_string(),
            error: None,
        };

        assert_eq!(response.status, status);
    }
}

/// Test ChainResponse with large body
#[test]
fn test_chain_response_large_body() {
    let large_array: Vec<serde_json::Value> = (0..1000).map(|i| json!({"id": i})).collect();
    let body = json!(large_array);

    let response = ChainResponse {
        status: 200,
        headers: std::collections::HashMap::new(),
        body: Some(body.clone()),
        duration_ms: 500,
        executed_at: "2023-01-01T00:00:00Z".to_string(),
        error: None,
    };

    assert_eq!(response.body, Some(body));
}

/// Test ChainContext with empty string keys
#[test]
fn test_chain_context_empty_keys() {
    let mut context = ChainContext::new();

    let response = ChainResponse {
        status: 200,
        headers: std::collections::HashMap::new(),
        body: None,
        duration_ms: 0,
        executed_at: "2023-01-01T00:00:00Z".to_string(),
        error: None,
    };

    context.store_response("".to_string(), response);
    context.set_variable("".to_string(), json!("empty"));
    context.set_metadata("".to_string(), "empty".to_string());

    // Empty keys should work (though not recommended)
    assert!(context.get_response("").is_some());
    assert!(context.get_variable("").is_some());
    assert!(context.get_metadata("").is_some());
}

/// Test ChainContext with unicode in keys
#[test]
fn test_chain_context_unicode_keys() {
    let mut context = ChainContext::new();

    let response = ChainResponse {
        status: 200,
        headers: std::collections::HashMap::new(),
        body: None,
        duration_ms: 0,
        executed_at: "2023-01-01T00:00:00Z".to_string(),
        error: None,
    };

    context.store_response("测试".to_string(), response);
    context.set_variable("ユーザー".to_string(), json!("value"));

    assert!(context.get_response("测试").is_some());
    assert_eq!(context.get_variable("ユーザー"), Some(&json!("value")));
}

/// Test RequestBody JSON with various value types
#[test]
fn test_request_body_json_types() {
    // Test with string
    let body1 = RequestBody::json(json!("string value"));
    assert_eq!(body1.content_type(), Some("application/json"));

    // Test with number
    let body2 = RequestBody::json(json!(42));
    assert_eq!(body2.content_type(), Some("application/json"));

    // Test with boolean
    let body3 = RequestBody::json(json!(true));
    assert_eq!(body3.content_type(), Some("application/json"));

    // Test with array
    let body4 = RequestBody::json(json!([1, 2, 3]));
    assert_eq!(body4.content_type(), Some("application/json"));

    // Test with null
    let body5 = RequestBody::json(json!(null));
    assert_eq!(body5.content_type(), Some("application/json"));
}

/// Test ChainResponse with various header combinations
#[test]
fn test_chain_response_headers() {
    let mut headers = std::collections::HashMap::new();
    headers.insert("content-type".to_string(), "application/json".to_string());
    headers.insert("x-request-id".to_string(), "req-123".to_string());
    headers.insert("cache-control".to_string(), "no-cache".to_string());

    let response = ChainResponse {
        status: 200,
        headers: headers.clone(),
        body: None,
        duration_ms: 0,
        executed_at: "2023-01-01T00:00:00Z".to_string(),
        error: None,
    };

    assert_eq!(response.headers.len(), 3);
    assert_eq!(response.headers.get("content-type"), Some(&"application/json".to_string()));
    assert_eq!(response.headers.get("x-request-id"), Some(&"req-123".to_string()));
    assert_eq!(response.headers.get("cache-control"), Some(&"no-cache".to_string()));
}

/// Test ChainContext variable types
#[test]
fn test_chain_context_variable_types() {
    let mut context = ChainContext::new();

    context.set_variable("string".to_string(), json!("text"));
    context.set_variable("number".to_string(), json!(42));
    context.set_variable("float".to_string(), json!(3.125));
    context.set_variable("boolean".to_string(), json!(true));
    context.set_variable("null".to_string(), json!(null));
    context.set_variable("array".to_string(), json!([1, 2, 3]));
    context.set_variable("object".to_string(), json!({"key": "value"}));

    assert_eq!(context.get_variable("string"), Some(&json!("text")));
    assert_eq!(context.get_variable("number"), Some(&json!(42)));
    assert_eq!(context.get_variable("float"), Some(&json!(3.125)));
    assert_eq!(context.get_variable("boolean"), Some(&json!(true)));
    assert_eq!(context.get_variable("null"), Some(&json!(null)));
    assert_eq!(context.get_variable("array"), Some(&json!([1, 2, 3])));
    assert_eq!(context.get_variable("object"), Some(&json!({"key": "value"})));
}