switchy_web_server 0.2.0

Switchy Web Server package
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![allow(clippy::multiple_crate_versions)]

//! # Extractor Integration Tests
//!
//! Comprehensive integration tests for the `MoosicBox` web server extractor system.
//! These tests validate that all extractors work correctly with both Actix and Simulator backends.
//!
//! ## Test Coverage
//!
//! * **Individual Extractor Tests**: Each extractor type (Query, Json, Path, Header, State)
//! * **Combination Tests**: Multiple extractors in single handlers
//! * **Error Handling**: Extraction failures and error propagation
//! * **Edge Cases**: Empty data, missing values, malformed input
//! * **Backend Consistency**: Identical behavior across Actix and Simulator
//!
//! ## Running Tests
//!
//! ```bash
//! # Test with Actix backend (default)
//! cargo test -p switchy_web_server extractor_integration
//!
//! # Test with Simulator backend
//! cargo test -p switchy_web_server --features simulator extractor_integration
//!
//! # Test with all features
//! cargo test -p switchy_web_server --all-features extractor_integration
//! ```

// No module-level conditional imports - using function-local imports instead

// Basic test utilities (no serde required)
mod basic_test_utils {
    /// Test state container (doesn't need serde)
    #[derive(Debug, Clone)]
    pub struct TestState {
        #[allow(dead_code)]
        pub counter: u64,
        #[allow(dead_code)]
        pub message: String,
    }

    impl TestState {
        #[allow(dead_code)]
        pub fn new() -> Self {
            Self {
                counter: 42,
                message: "test state".to_string(),
            }
        }
    }

    /// Create test state container
    #[cfg(feature = "simulator")]
    #[allow(dead_code)]
    pub fn create_test_state_container() -> switchy_web_server::extractors::StateContainer {
        use switchy_web_server::extractors::StateContainer;
        let mut container = StateContainer::new();
        container.insert(TestState::new());
        container
    }
}

// Serde-dependent test utilities
#[cfg(feature = "serde")]
mod serde_test_utils {
    use serde::{Deserialize, Serialize};

    /// Test data structure for JSON extraction
    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
    pub struct TestJsonData {
        pub name: String,
        pub age: u32,
        pub active: bool,
    }

    impl TestJsonData {
        #[allow(dead_code)]
        pub fn sample() -> Self {
            Self {
                name: "Alice".to_string(),
                age: 30,
                active: true,
            }
        }
    }

    /// Create test query parameters
    #[allow(dead_code)]
    pub fn create_test_query() -> std::collections::BTreeMap<String, String> {
        let mut query = std::collections::BTreeMap::new();
        query.insert("name".to_string(), "Bob".to_string());
        query.insert("age".to_string(), "25".to_string());
        query.insert("active".to_string(), "true".to_string());
        query
    }

    /// Create test headers
    #[allow(dead_code)]
    pub fn create_test_headers() -> std::collections::BTreeMap<String, String> {
        let mut headers = std::collections::BTreeMap::new();
        headers.insert("authorization".to_string(), "Bearer token123".to_string());
        headers.insert("content-type".to_string(), "application/json".to_string());
        headers.insert("x-custom-header".to_string(), "custom-value".to_string());
        headers
    }

    /// Create test path parameters
    #[allow(dead_code)]
    pub fn create_test_path() -> std::collections::BTreeMap<String, String> {
        let mut path = std::collections::BTreeMap::new();
        path.insert("id".to_string(), "123".to_string());
        path.insert("category".to_string(), "music".to_string());
        path
    }

    /// Create comprehensive test request with all data types
    #[cfg(feature = "simulator")]
    #[allow(dead_code)]
    pub fn create_comprehensive_test_request() -> switchy_web_server::simulator::SimulationRequest {
        use bytes::Bytes;
        use switchy_web_server::Method;
        use switchy_web_server::simulator::SimulationRequest;

        SimulationRequest::new(Method::Post, "/test/123/music")
            .with_query_string("name=Bob&age=25&active=true")
            .with_header("authorization", "Bearer token123")
            .with_header("content-type", "application/json")
            .with_header("x-custom-header", "custom-value")
            .with_body(Bytes::from(
                serde_json::to_string(&TestJsonData::sample()).unwrap(),
            ))
    }
}

// Basic Actix tests (State extractor only)
#[cfg(feature = "actix")]
mod basic_actix_tests {
    #[test]
    fn test_state_extractor_compilation() {
        use crate::basic_test_utils::TestState;
        use switchy_web_server::extractors::State;

        // Test that State extractor compiles with Actix backend
        #[allow(clippy::unnecessary_wraps)]
        fn handler(
            _state: State<TestState>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("success".to_string())
        }

        // Compilation test - if this compiles, the extractor works
        std::hint::black_box(handler);
    }
}

// Serde-dependent Actix tests
#[cfg(all(feature = "actix", feature = "serde"))]
mod serde_actix_tests {
    #[test]
    fn test_query_extractor_compilation() {
        use switchy_web_server::extractors::Query;

        // Test that Query extractor compiles with Actix backend
        #[allow(clippy::unnecessary_wraps)]
        fn handler(
            _query: Query<String>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("success".to_string())
        }

        // Compilation test - if this compiles, the extractor works
        std::hint::black_box(handler);
    }

    #[test]
    fn test_json_extractor_compilation() {
        use crate::serde_test_utils::TestJsonData;
        use switchy_web_server::extractors::Json;

        // Test that Json extractor compiles with Actix backend
        #[allow(clippy::unnecessary_wraps)]
        fn handler(
            _json: Json<TestJsonData>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("success".to_string())
        }

        // Compilation test - if this compiles, the extractor works
        std::hint::black_box(handler);
    }

    #[test]
    fn test_path_extractor_compilation() {
        use switchy_web_server::extractors::Path;

        // Test that Path extractor compiles with Actix backend
        #[allow(clippy::unnecessary_wraps)]
        fn handler(
            _path: Path<String>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("success".to_string())
        }

        // Compilation test - if this compiles, the extractor works
        std::hint::black_box(handler);
    }

    #[test]
    fn test_header_extractor_compilation() {
        use switchy_web_server::extractors::Header;

        // Test that Header extractor compiles with Actix backend
        #[allow(clippy::unnecessary_wraps)]
        fn handler(
            _header: Header<String>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("success".to_string())
        }

        // Compilation test - if this compiles, the extractor works
        std::hint::black_box(handler);
    }

    #[test]
    fn test_multiple_extractors_compilation() {
        use crate::basic_test_utils::TestState;
        use crate::serde_test_utils::TestJsonData;
        use switchy_web_server::extractors::{Header, Json, Path, Query, State};

        // Test that multiple extractors work together with Actix backend
        #[allow(clippy::unnecessary_wraps)]
        fn handler(
            _query: Query<String>,
            _json: Json<TestJsonData>,
            _path: Path<String>,
            _header: Header<String>,
            _state: State<TestState>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("success".to_string())
        }

        // Compilation test - if this compiles, multiple extractors work
        std::hint::black_box(handler);
    }

    #[test]
    fn test_extractor_error_handling_compilation() {
        use switchy_web_server::extractors::Query;

        // Test that error handling works with extractors
        fn handler(
            _query: Query<String>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Err("test error".into())
        }

        // Compilation test - if this compiles, error handling works
        std::hint::black_box(handler);
    }
}

// Basic Simulator tests (State extractor only)
#[cfg(feature = "simulator")]
mod basic_simulator_tests {
    #[test]
    fn test_state_extractor_compilation() {
        use crate::basic_test_utils::TestState;
        use switchy_web_server::extractors::State;

        // Test that State extractor compiles with Simulator backend
        async fn handler(
            _state: State<TestState>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("success".to_string())
        }

        // Compilation test - if this compiles, the extractor works
        std::hint::black_box(handler);
    }

    #[test]
    fn test_extractor_with_state_container() {
        use crate::basic_test_utils::TestState;
        use switchy_web_server::extractors::State;

        // Test that State extractor works with StateContainer
        async fn handler(
            _state: State<TestState>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("success".to_string())
        }

        // Compilation test - if this compiles, StateContainer integration works
        std::hint::black_box(handler);
    }
}

// Serde-dependent Simulator tests
#[cfg(all(feature = "simulator", feature = "serde"))]
mod serde_simulator_tests {
    #[test]
    fn test_query_extractor_compilation() {
        use switchy_web_server::extractors::Query;

        // Test that Query extractor compiles with Simulator backend
        async fn handler(
            _query: Query<String>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("success".to_string())
        }

        // Compilation test - if this compiles, the extractor works
        std::hint::black_box(handler);
    }

    #[test]
    fn test_json_extractor_compilation() {
        use crate::serde_test_utils::TestJsonData;
        use switchy_web_server::extractors::Json;

        // Test that Json extractor compiles with Simulator backend
        async fn handler(
            _json: Json<TestJsonData>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("success".to_string())
        }

        // Compilation test - if this compiles, the extractor works
        std::hint::black_box(handler);
    }

    #[test]
    fn test_path_extractor_compilation() {
        use switchy_web_server::extractors::Path;

        // Test that Path extractor compiles with Simulator backend
        async fn handler(
            _path: Path<String>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("success".to_string())
        }

        // Compilation test - if this compiles, the extractor works
        std::hint::black_box(handler);
    }

    #[test]
    fn test_header_extractor_compilation() {
        use switchy_web_server::extractors::Header;

        // Test that Header extractor compiles with Simulator backend
        async fn handler(
            _header: Header<String>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("success".to_string())
        }

        // Compilation test - if this compiles, the extractor works
        std::hint::black_box(handler);
    }

    #[test]
    fn test_multiple_extractors_compilation() {
        use crate::basic_test_utils::TestState;
        use crate::serde_test_utils::TestJsonData;
        use switchy_web_server::extractors::{Header, Json, Path, Query, State};

        // Test that multiple extractors work together with Simulator backend
        async fn handler(
            _query: Query<String>,
            _json: Json<TestJsonData>,
            _path: Path<String>,
            _header: Header<String>,
            _state: State<TestState>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("success".to_string())
        }

        // Compilation test - if this compiles, multiple extractors work
        std::hint::black_box(handler);
    }

    #[test]
    fn test_extractor_error_handling_compilation() {
        use switchy_web_server::extractors::Query;

        // Test that error handling works with extractors
        async fn handler(
            _query: Query<String>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Err("test error".into())
        }

        // Compilation test - if this compiles, error handling works
        std::hint::black_box(handler);
    }
}

// Basic cross-backend consistency tests
mod basic_consistency_tests {
    #[test]
    fn test_state_extractor_consistency() {
        // Test that State extractor signatures are identical across backends

        #[cfg(feature = "actix")]
        {
            use crate::basic_test_utils::TestState;
            use switchy_web_server::extractors::State;

            #[allow(clippy::unnecessary_wraps)]
            fn actix_handler(
                _state: State<TestState>,
            ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
                Ok("actix".to_string())
            }

            std::hint::black_box(actix_handler);
        }

        #[cfg(feature = "simulator")]
        {
            use crate::basic_test_utils::TestState;
            use switchy_web_server::extractors::State;

            async fn simulator_handler(
                _state: State<TestState>,
            ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
                Ok("simulator".to_string())
            }

            std::hint::black_box(simulator_handler);
        }
    }
}

// Serde-dependent cross-backend consistency tests
#[cfg(feature = "serde")]
mod serde_consistency_tests {
    #[test]
    fn test_extractor_signatures_consistency() {
        // Test that extractor signatures are identical across backends

        #[cfg(feature = "actix")]
        {
            use crate::basic_test_utils::TestState;
            use crate::serde_test_utils::TestJsonData;
            use switchy_web_server::extractors::{Header, Json, Path, Query, State};

            #[allow(clippy::unnecessary_wraps)]
            fn actix_handler(
                _query: Query<String>,
                _json: Json<TestJsonData>,
                _path: Path<String>,
                _header: Header<String>,
                _state: State<TestState>,
            ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
                Ok("actix".to_string())
            }

            std::hint::black_box(actix_handler);
        }

        #[cfg(feature = "simulator")]
        {
            use crate::basic_test_utils::TestState;
            use crate::serde_test_utils::TestJsonData;
            use switchy_web_server::extractors::{Header, Json, Path, Query, State};

            async fn simulator_handler(
                _query: Query<String>,
                _json: Json<TestJsonData>,
                _path: Path<String>,
                _header: Header<String>,
                _state: State<TestState>,
            ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
                Ok("simulator".to_string())
            }

            std::hint::black_box(simulator_handler);
        }
    }

    #[test]
    fn test_error_handling_consistency() {
        // Test that error handling is consistent across backends

        #[cfg(feature = "actix")]
        {
            use switchy_web_server::extractors::Query;

            fn actix_error_handler(
                _query: Query<String>,
            ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
                Err("actix error".into())
            }

            std::hint::black_box(actix_error_handler);
        }

        #[cfg(feature = "simulator")]
        {
            use switchy_web_server::extractors::Query;

            async fn simulator_error_handler(
                _query: Query<String>,
            ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
                Err("simulator error".into())
            }

            std::hint::black_box(simulator_error_handler);
        }
    }
}

// Edge case tests (serde-dependent)
#[cfg(all(test, feature = "simulator", feature = "serde"))]
mod edge_case_tests {
    #[test]
    fn test_empty_query_extraction() {
        use switchy_web_server::extractors::Query;

        // Test extraction with empty query parameters
        async fn handler(
            _query: Query<Option<String>>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("handled empty query".to_string())
        }

        // Compilation test - if this compiles, empty query handling works
        std::hint::black_box(handler);
    }

    #[test]
    fn test_missing_header_extraction() {
        use switchy_web_server::extractors::Header;

        // Test extraction with missing headers
        async fn handler(
            _header: Header<Option<String>>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("handled missing header".to_string())
        }

        // Compilation test - if this compiles, missing header handling works
        std::hint::black_box(handler);
    }

    #[test]
    fn test_optional_path_extraction() {
        use switchy_web_server::extractors::Path;

        // Test extraction with optional path parameters
        async fn handler(
            _path: Path<Option<String>>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("handled optional path".to_string())
        }

        // Compilation test - if this compiles, optional path handling works
        std::hint::black_box(handler);
    }

    #[test]
    fn test_complex_json_extraction() {
        use serde::Deserialize;
        use switchy_web_server::extractors::Json;

        // Test extraction with complex JSON structures
        #[derive(Deserialize)]
        struct ComplexData {
            #[allow(dead_code)]
            nested: NestedData,
            #[allow(dead_code)]
            list: Vec<String>,
            #[allow(dead_code)]
            optional: Option<String>,
        }

        #[derive(Deserialize)]
        struct NestedData {
            #[allow(dead_code)]
            value: u64,
        }

        async fn handler(
            _json: Json<ComplexData>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("handled complex json".to_string())
        }

        // Compilation test - if this compiles, complex JSON handling works
        std::hint::black_box(handler);
    }
}

// Performance and stress tests (serde-dependent)
#[cfg(all(test, feature = "simulator", feature = "serde"))]
mod performance_tests {
    #[test]
    fn test_large_json_extraction() {
        use crate::serde_test_utils::TestJsonData;
        use serde::Deserialize;
        use switchy_web_server::extractors::Json;

        // Test extraction with large JSON payloads
        #[derive(Deserialize)]
        struct LargeData {
            #[allow(dead_code)]
            items: Vec<TestJsonData>,
            #[allow(dead_code)]
            metadata: std::collections::BTreeMap<String, String>,
        }

        async fn handler(
            _json: Json<LargeData>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("handled large json".to_string())
        }

        // Compilation test - if this compiles, large JSON handling works
        std::hint::black_box(handler);
    }

    #[test]
    fn test_many_query_parameters() {
        use serde::Deserialize;
        use switchy_web_server::extractors::Query;

        // Test extraction with many query parameters
        #[derive(Deserialize)]
        struct ManyParams {
            #[allow(dead_code)]
            param1: Option<String>,
            #[allow(dead_code)]
            param2: Option<String>,
            #[allow(dead_code)]
            param3: Option<String>,
            #[allow(dead_code)]
            param4: Option<String>,
            #[allow(dead_code)]
            param5: Option<String>,
            #[allow(dead_code)]
            param6: Option<String>,
            #[allow(dead_code)]
            param7: Option<String>,
            #[allow(dead_code)]
            param8: Option<String>,
            #[allow(dead_code)]
            param9: Option<String>,
            #[allow(dead_code)]
            param10: Option<String>,
        }

        async fn handler(
            _query: Query<ManyParams>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("handled many params".to_string())
        }

        // Compilation test - if this compiles, many parameter handling works
        std::hint::black_box(handler);
    }

    #[test]
    fn test_many_headers() {
        use switchy_web_server::extractors::Header;

        // Test extraction with many headers
        async fn handler(
            _h1: Header<Option<String>>,
            _h2: Header<Option<String>>,
            _h3: Header<Option<String>>,
            _h4: Header<Option<String>>,
            _h5: Header<Option<String>>,
        ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
            Ok("handled many headers".to_string())
        }

        // Compilation test - if this compiles, many header handling works
        std::hint::black_box(handler);
    }
}

// Basic benchmarks module (compilation-time benchmarks)
#[cfg(test)]
mod basic_benchmarks {
    #[test]
    fn benchmark_basic_extractor_compilation_time() {
        use crate::basic_test_utils::TestState;
        use switchy_web_server::extractors::State;

        // This test measures compilation time for basic extractor combinations
        // The actual benchmark is the compilation itself

        // Simple State extractor
        fn simple_state(_state: State<TestState>) -> String {
            "state".to_string()
        }

        // If this compiles quickly, our basic extractor system is efficient
        std::hint::black_box(simple_state);
    }
}

// Serde-dependent benchmarks module (compilation-time benchmarks)
#[cfg(all(test, feature = "serde"))]
mod serde_benchmarks {
    #[test]
    fn benchmark_serde_extractor_compilation_time() {
        use crate::basic_test_utils::TestState;
        use crate::serde_test_utils::TestJsonData;
        use switchy_web_server::extractors::{Header, Json, Path, Query, State};

        // This test measures compilation time for serde extractor combinations
        // The actual benchmark is the compilation itself

        // Simple extractors
        fn simple_query(_query: Query<String>) -> String {
            "query".to_string()
        }

        fn simple_json(_json: Json<TestJsonData>) -> String {
            "json".to_string()
        }

        // Complex combinations
        fn complex_handler(
            _query: Query<String>,
            _json: Json<TestJsonData>,
            _path: Path<String>,
            _header: Header<String>,
            _state: State<TestState>,
        ) -> String {
            "complex".to_string()
        }

        // If these compile quickly, our extractor system is efficient
        std::hint::black_box(simple_query);
        std::hint::black_box(simple_json);
        std::hint::black_box(complex_handler);
    }
}