foxy-io 0.3.4

A configuration-driven and hyper-extensible HTTP proxy library
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Comprehensive tests for the logging module to achieve 95% coverage

#[cfg(test)]
mod tests {
    use super::super::*;
    use crate::logging::config::LoggingConfig;
    use crate::logging::test_logger;
    use log::LevelFilter;
    use std::sync::atomic::Ordering;

    // Helper function to reset logging state for testing
    fn reset_logging_state() {
        // Reset the atomic flag
        USING_STRUCTURED.store(false, Ordering::SeqCst);
        
        // Note: We can't reset the Once, but we can test different scenarios
        // by creating new processes or using different test strategies
    }

    #[test]
    fn test_init_with_config_structured_logging() {
        let config = LoggingConfig {
            structured: true,
            level: "debug".to_string(),
            format: "json".to_string(),
            include_location: true,
            include_thread_id: true,
            include_trace_id: true,
            propagate_trace_id: true,
            trace_id_header: "X-Trace-ID".to_string(),
            static_fields: std::collections::HashMap::new(),
        };

        // Test initialization with structured logging
        init_with_config(LevelFilter::Debug, &config);

        // Note: Due to the Once guard, we can't reliably test the structured logging state
        // in unit tests since other tests may have already initialized the logger.
        // We just verify the function doesn't panic.
    }

    #[test]
    fn test_init_with_config_env_logger() {
        reset_logging_state();

        let config = LoggingConfig {
            structured: false,
            level: "info".to_string(),
            format: "terminal".to_string(),
            include_location: false,
            include_thread_id: false,
            include_trace_id: false,
            propagate_trace_id: false,
            trace_id_header: "X-Trace-ID".to_string(),
            static_fields: std::collections::HashMap::new(),
        };

        // Test initialization with env_logger
        init_with_config(LevelFilter::Info, &config);

        // Since we can't easily reset the Once, we test the function doesn't panic
        // and that the logging level is set correctly
        assert_eq!(log::max_level(), LevelFilter::Info);
    }

    #[test]
    fn test_init_with_config_different_levels() {
        let levels = vec![
            LevelFilter::Error,
            LevelFilter::Warn,
            LevelFilter::Info,
            LevelFilter::Debug,
            LevelFilter::Trace,
        ];

        for level in levels {
            let config = LoggingConfig {
                structured: false,
                level: level.to_string(),
                format: "terminal".to_string(),
                include_location: true,
                include_thread_id: true,
                include_trace_id: true,
                propagate_trace_id: true,
                trace_id_header: "X-Trace-ID".to_string(),
                static_fields: std::collections::HashMap::new(),
            };

            // Test that initialization doesn't panic with different levels
            init_with_config(level, &config);
        }
    }

    #[test]
    fn test_is_structured_logging() {
        // Test the atomic boolean getter
        let initial_state = is_structured_logging();
        
        // The function should return a boolean without panicking
        assert!(initial_state == true || initial_state == false);
        
        // Test setting the state manually
        USING_STRUCTURED.store(true, Ordering::SeqCst);
        assert!(is_structured_logging());
        
        USING_STRUCTURED.store(false, Ordering::SeqCst);
        assert!(!is_structured_logging());
    }

    #[test]
    fn test_log_error_structured() {
        // Set up a minimal structured logger for testing
        let drain = slog::Discard;
        let logger = slog::Logger::root(drain, slog::o!());
        test_logger::init_test_logger();

        USING_STRUCTURED.store(true, Ordering::SeqCst);

        let test_error = "Test error message";
        let context = "TestContext";

        // Test that log_error returns the error unchanged
        let returned_error = log_error(context, test_error);
        assert_eq!(returned_error, test_error);
    }

    #[test]
    fn test_log_error_non_structured() {
        USING_STRUCTURED.store(false, Ordering::SeqCst);
        
        let test_error = "Test error message";
        let context = "TestContext";
        
        // Test that log_error returns the error unchanged
        let returned_error = log_error(context, test_error);
        assert_eq!(returned_error, test_error);
    }

    #[test]
    fn test_log_warning_structured() {
        // Set up a minimal structured logger for testing
        let drain = slog::Discard;
        let logger = slog::Logger::root(drain, slog::o!());
        test_logger::init_test_logger();

        USING_STRUCTURED.store(true, Ordering::SeqCst);

        let test_warning = "Test warning message";
        let context = "TestContext";

        // Test that log_warning doesn't panic
        log_warning(context, test_warning);
    }

    #[test]
    fn test_log_warning_non_structured() {
        USING_STRUCTURED.store(false, Ordering::SeqCst);
        
        let test_warning = "Test warning message";
        let context = "TestContext";
        
        // Test that log_warning doesn't panic
        log_warning(context, test_warning);
    }

    #[test]
    fn test_log_debug_structured() {
        // Set up a minimal structured logger for testing
        let drain = slog::Discard;
        let logger = slog::Logger::root(drain, slog::o!());
        test_logger::init_test_logger();

        USING_STRUCTURED.store(true, Ordering::SeqCst);

        let test_message = "Test debug message";
        let context = "TestContext";

        // Test that log_debug doesn't panic
        log_debug(context, test_message);
    }

    #[test]
    fn test_log_debug_non_structured() {
        USING_STRUCTURED.store(false, Ordering::SeqCst);
        
        let test_message = "Test debug message";
        let context = "TestContext";
        
        // Test that log_debug doesn't panic
        log_debug(context, test_message);
    }

    #[test]
    fn test_log_trace_structured() {
        // Set up a minimal structured logger for testing
        let drain = slog::Discard;
        let logger = slog::Logger::root(drain, slog::o!());
        test_logger::init_test_logger();

        USING_STRUCTURED.store(true, Ordering::SeqCst);

        let test_message = "Test trace message";
        let context = "TestContext";

        // Test that log_trace doesn't panic
        log_trace(context, test_message);
    }

    #[test]
    fn test_log_trace_non_structured() {
        USING_STRUCTURED.store(false, Ordering::SeqCst);
        
        let test_message = "Test trace message";
        let context = "TestContext";
        
        // Test that log_trace doesn't panic
        log_trace(context, test_message);
    }

    #[test]
    fn test_log_info_structured() {
        // Set up a minimal structured logger for testing
        let drain = slog::Discard;
        let logger = slog::Logger::root(drain, slog::o!());
        test_logger::init_test_logger();

        USING_STRUCTURED.store(true, Ordering::SeqCst);

        let test_message = "Test info message";
        let context = "TestContext";

        // Test that log_info doesn't panic
        log_info(context, test_message);
    }

    #[test]
    fn test_log_info_non_structured() {
        USING_STRUCTURED.store(false, Ordering::SeqCst);
        
        let test_message = "Test info message";
        let context = "TestContext";
        
        // Test that log_info doesn't panic
        log_info(context, test_message);
    }

    #[test]
    fn test_log_with_context_all_levels_structured() {
        // Set up a minimal structured logger for testing
        let drain = slog::Discard;
        let logger = slog::Logger::root(drain, slog::o!());
        test_logger::init_test_logger();

        USING_STRUCTURED.store(true, Ordering::SeqCst);

        let message = "Test message";
        let context = "TestContext";
        let fields = vec![
            ("field1", "value1".to_string()),
            ("field2", "value2".to_string()),
        ];

        let levels = vec![
            log::Level::Error,
            log::Level::Warn,
            log::Level::Info,
            log::Level::Debug,
            log::Level::Trace,
        ];

        for level in levels {
            // Test that log_with_context doesn't panic for any level
            log_with_context(level, message, context, &fields);
        }
    }

    #[test]
    fn test_log_with_context_all_levels_non_structured() {
        // Initialize env_logger for non-structured logging
        let _ = env_logger::builder().is_test(true).try_init();

        // Set up a minimal structured logger for testing (even for non-structured test)
        let drain = slog::Discard;
        let logger = slog::Logger::root(drain, slog::o!());

        USING_STRUCTURED.store(false, Ordering::SeqCst);

        let message = "Test message";
        let context = "TestContext";
        let fields = vec![
            ("field1", "value1".to_string()),
            ("field2", "value2".to_string()),
        ];

        let levels = vec![
            log::Level::Error,
            log::Level::Warn,
            log::Level::Info,
            log::Level::Debug,
            log::Level::Trace,
        ];

        // Use scope instead of global logger to avoid conflicts
        slog_scope::scope(&logger, || {
            for level in levels {
                // Test that log_with_context doesn't panic for any level
                log_with_context(level, message, context, &fields);
            }
        });
    }

    #[test]
    fn test_log_with_context_empty_fields() {
        // Set up a minimal structured logger for testing
        let drain = slog::Discard;
        let logger = slog::Logger::root(drain, slog::o!());
        test_logger::init_test_logger();

        USING_STRUCTURED.store(true, Ordering::SeqCst);

        let message = "Test message";
        let context = "TestContext";
        let empty_fields: Vec<(&'static str, String)> = vec![];

        // Test with empty fields array
        log_with_context(log::Level::Info, message, context, &empty_fields);
    }

    #[test]
    fn test_add_fields_to_logger() {
        // Create a test logger (this requires slog to be initialized)
        let drain = slog::Discard;
        let logger = slog::Logger::root(drain, slog::o!());
        
        let fields = vec![
            ("test_field1", "value1".to_string()),
            ("test_field2", "value2".to_string()),
            ("test_field3", "value3".to_string()),
        ];

        // Test that add_fields_to_logger doesn't panic
        let result_logger = add_fields_to_logger(logger, &fields);
        
        // The function should return a logger (we can't easily test the fields without complex setup)
        // But we can verify it doesn't panic and returns a logger
        assert!(std::ptr::addr_of!(result_logger) != std::ptr::null());
    }

    #[test]
    fn test_add_fields_to_logger_empty_fields() {
        let drain = slog::Discard;
        let logger = slog::Logger::root(drain, slog::o!());

        let empty_fields: Vec<(&'static str, String)> = vec![];

        // Test with empty fields
        let result_logger = add_fields_to_logger(logger, &empty_fields);
        assert!(std::ptr::addr_of!(result_logger) != std::ptr::null());
    }

    #[test]
    fn test_add_fields_to_logger_single_field() {
        let drain = slog::Discard;
        let logger = slog::Logger::root(drain, slog::o!());

        let fields = vec![("single_field", "single_value".to_string())];

        // Test with single field
        let result_logger = add_fields_to_logger(logger, &fields);
        assert!(std::ptr::addr_of!(result_logger) != std::ptr::null());
    }

    #[test]
    fn test_add_fields_to_logger_many_fields() {
        let drain = slog::Discard;
        let logger = slog::Logger::root(drain, slog::o!());

        let fields = vec![
            ("field_1", "value_1".to_string()),
            ("field_2", "value_2".to_string()),
            ("field_3", "value_3".to_string()),
            ("field_4", "value_4".to_string()),
            ("field_5", "value_5".to_string()),
        ];

        // Test with many fields
        let result_logger = add_fields_to_logger(logger, &fields);
        assert!(std::ptr::addr_of!(result_logger) != std::ptr::null());
    }

    #[test]
    fn test_logging_functions_with_different_types() {
        USING_STRUCTURED.store(false, Ordering::SeqCst);

        // Test with different Display types
        log_error("context", 42);
        log_error("context", "string error");
        log_error("context", format!("formatted {}", "error"));

        log_warning("context", 3.14);
        log_warning("context", true);

        log_debug("context", "debug message");
        log_info("context", "info message");
        log_trace("context", "trace message");
    }

    #[test]
    fn test_logging_functions_with_empty_context() {
        USING_STRUCTURED.store(false, Ordering::SeqCst);

        // Test with empty context string
        log_error("", "error message");
        log_warning("", "warning message");
        log_debug("", "debug message");
        log_info("", "info message");
        log_trace("", "trace message");
    }

    #[test]
    fn test_logging_functions_with_special_characters() {
        USING_STRUCTURED.store(false, Ordering::SeqCst);

        // Test with special characters in context and messages
        log_error("Context with spaces", "Error with\nnewlines");
        log_warning("Context-with-dashes", "Warning with\ttabs");
        log_debug("Context_with_underscores", "Debug with \"quotes\"");
        log_info("Context.with.dots", "Info with 'single quotes'");
        log_trace("Context/with/slashes", "Trace with unicode: 🦀");
    }

    #[test]
    fn test_log_with_context_special_field_values() {
        // Set up a minimal structured logger for testing
        let drain = slog::Discard;
        let logger = slog::Logger::root(drain, slog::o!());
        test_logger::init_test_logger();

        USING_STRUCTURED.store(true, Ordering::SeqCst);

        let message = "Test message";
        let context = "TestContext";
        let special_fields = vec![
            ("empty_field", "".to_string()),
            ("unicode_field", "🦀 Rust".to_string()),
            ("json_like_field", r#"{"key": "value"}"#.to_string()),
            ("newline_field", "line1\nline2".to_string()),
            ("tab_field", "col1\tcol2".to_string()),
        ];

        // Test with special field values
        log_with_context(log::Level::Info, message, context, &special_fields);
    }

    #[test]
    fn test_init_with_config_multiple_calls() {
        // Test that multiple calls to init_with_config don't cause issues
        // (Due to Once, only the first call should actually initialize)

        let config1 = LoggingConfig {
            structured: false,
            level: "error".to_string(),
            format: "terminal".to_string(),
            include_location: false,
            include_thread_id: false,
            include_trace_id: false,
            propagate_trace_id: false,
            trace_id_header: "X-Trace-ID".to_string(),
            static_fields: std::collections::HashMap::new(),
        };

        let config2 = LoggingConfig {
            structured: true,
            level: "trace".to_string(),
            format: "json".to_string(),
            include_location: true,
            include_thread_id: true,
            include_trace_id: true,
            propagate_trace_id: true,
            trace_id_header: "X-Trace-ID".to_string(),
            static_fields: std::collections::HashMap::new(),
        };

        // Multiple calls should not panic
        init_with_config(LevelFilter::Error, &config1);
        init_with_config(LevelFilter::Trace, &config2);
        init_with_config(LevelFilter::Debug, &config1);
    }

    #[test]
    fn test_logging_config_with_static_fields() {
        let mut static_fields = std::collections::HashMap::new();
        static_fields.insert("service".to_string(), "test-service".to_string());
        static_fields.insert("version".to_string(), "1.0.0".to_string());
        static_fields.insert("environment".to_string(), "test".to_string());

        let config = LoggingConfig {
            structured: true,
            level: "info".to_string(),
            format: "json".to_string(),
            include_location: true,
            include_thread_id: true,
            include_trace_id: true,
            propagate_trace_id: true,
            trace_id_header: "X-Trace-ID".to_string(),
            static_fields,
        };

        // Test initialization with static fields
        init_with_config(LevelFilter::Info, &config);
    }

    #[test]
    fn test_atomic_operations_thread_safety() {
        use std::thread;

        let handles: Vec<_> = (0..10).map(|i| {
            thread::spawn(move || {
                // Test concurrent access to the atomic boolean
                let state = is_structured_logging();
                USING_STRUCTURED.store(i % 2 == 0, Ordering::SeqCst);
                let new_state = is_structured_logging();

                // Both operations should complete without panicking
                (state, new_state)
            })
        }).collect();

        // Wait for all threads to complete
        for handle in handles {
            let _ = handle.join();
        }
    }

    #[test]
    fn test_log_error_return_value_preservation() {
        // Test that log_error preserves the exact error value
        #[derive(Debug, PartialEq)]
        struct CustomError {
            code: i32,
            message: String,
        }

        impl std::fmt::Display for CustomError {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "Error {}: {}", self.code, self.message)
            }
        }

        let original_error = CustomError {
            code: 404,
            message: "Not found".to_string(),
        };

        let returned_error = log_error("TestContext", original_error);

        // The returned error should be identical to the original
        assert_eq!(returned_error.code, 404);
        assert_eq!(returned_error.message, "Not found");
    }
}