rustapi-rs 0.1.450

A FastAPI-like web framework for Rust - DX-first, type-safe, batteries included
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
//! Integration tests for RustAPI framework
//!
//! These tests cover cross-cutting concerns that involve multiple crates working together.

#![allow(unused_imports)]
use rustapi_rs::prelude::*;

// ============================================================================
// Router Integration Tests
// ============================================================================

mod router_tests {
    use rustapi_rs::get;

    #[get("/integ-method-test")]
    async fn method_test() -> &'static str {
        "get"
    }

    #[test]
    fn test_router_method_routing() {
        let routes = rustapi_rs::collect_auto_routes();
        let found = routes
            .iter()
            .any(|r| r.path() == "/integ-method-test" && r.method() == "GET");

        assert!(found, "GET /integ-method-test should be registered");
    }

    #[get("/integ-users/{user_id}")]
    async fn user_handler(rustapi_rs::Path(user_id): rustapi_rs::Path<i64>) -> String {
        format!("user={}", user_id)
    }

    #[test]
    fn test_router_path_params() {
        let routes = rustapi_rs::collect_auto_routes();
        let found = routes.iter().any(|r| r.path() == "/integ-users/{user_id}");

        assert!(found, "Path param route should be registered");
    }
}

// ============================================================================
// State Management Tests
// ============================================================================

mod state_tests {
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    #[derive(Clone)]
    struct Counter(Arc<AtomicUsize>);

    impl Counter {
        fn new() -> Self {
            Self(Arc::new(AtomicUsize::new(0)))
        }

        fn increment(&self) -> usize {
            self.0.fetch_add(1, Ordering::SeqCst)
        }

        fn get(&self) -> usize {
            self.0.load(Ordering::SeqCst)
        }
    }

    #[test]
    fn test_state_sharing() {
        let counter = Counter::new();

        // Simulate multiple handlers accessing state
        let c1 = counter.clone();
        let c2 = counter.clone();
        let c3 = counter.clone();

        c1.increment();
        c2.increment();
        c3.increment();

        assert_eq!(counter.get(), 3, "All handlers should share same state");
    }

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

        let counter = Counter::new();
        let mut handles = vec![];

        for _ in 0..10 {
            let c = counter.clone();
            handles.push(thread::spawn(move || {
                for _ in 0..100 {
                    c.increment();
                }
            }));
        }

        for h in handles {
            h.join().unwrap();
        }

        assert_eq!(counter.get(), 1000, "All increments should be counted");
    }
}

// ============================================================================
// JSON Serialization Tests
// ============================================================================

mod json_tests {
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
    struct TestData {
        id: i64,
        name: String,
        tags: Vec<String>,
        active: bool,
    }

    #[test]
    fn test_json_roundtrip() {
        let data = TestData {
            id: 42,
            name: "Test Item".to_string(),
            tags: vec!["tag1".to_string(), "tag2".to_string()],
            active: true,
        };

        let json = serde_json::to_string(&data).unwrap();
        let parsed: TestData = serde_json::from_str(&json).unwrap();

        assert_eq!(data, parsed, "Data should survive JSON roundtrip");
    }

    #[test]
    fn test_json_error_format() {
        // Test that invalid JSON produces expected error
        let bad_json = r#"{"id": "not_a_number"}"#;
        let result: Result<TestData, _> = serde_json::from_str(bad_json);

        assert!(result.is_err(), "Should fail to parse invalid JSON");
        let err = result.unwrap_err();
        assert!(
            err.to_string().contains("invalid type"),
            "Error should mention type mismatch"
        );
    }

    #[test]
    fn test_json_with_special_chars() {
        #[derive(Serialize, Deserialize, PartialEq, Debug)]
        struct TextData {
            content: String,
        }

        let data = TextData {
            content: "Hello \"World\" with\nnewlines\tand\ttabs".to_string(),
        };

        let json = serde_json::to_string(&data).unwrap();
        let parsed: TextData = serde_json::from_str(&json).unwrap();

        assert_eq!(data, parsed, "Special characters should be preserved");
    }
}

// ============================================================================
// Error Handling Tests
// ============================================================================

mod error_tests {
    use rustapi_rs::prelude::*;

    #[test]
    fn test_api_error_not_found() {
        let error = ApiError::not_found("User not found");
        assert_eq!(error.error_type, "not_found");
        assert_eq!(error.message, "User not found");
    }

    #[test]
    fn test_api_error_bad_request() {
        let error = ApiError::bad_request("Invalid input");
        assert_eq!(error.error_type, "bad_request");
        assert_eq!(error.message, "Invalid input");
    }

    #[test]
    fn test_api_error_validation() {
        let error = ApiError::validation(vec![rustapi_rs::FieldError {
            field: "email".to_string(),
            code: "email".to_string(),
            message: "Invalid email format".to_string(),
        }]);

        assert!(error.fields.is_some(), "Should have field errors");
        assert_eq!(error.fields.as_ref().unwrap().len(), 1);
    }

    #[test]
    fn test_result_type_ok() {
        fn handler() -> Result<String, ApiError> {
            Ok("success".to_string())
        }

        let result = handler();
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "success");
    }

    #[test]
    fn test_result_type_err() {
        fn handler(fail: bool) -> Result<String, ApiError> {
            if fail {
                Err(ApiError::bad_request("Failed"))
            } else {
                Ok("success".to_string())
            }
        }

        assert!(handler(true).is_err());
        assert!(handler(false).is_ok());
    }
}

// ============================================================================
// OpenAPI Schema Tests
// ============================================================================

mod openapi_tests {
    use rustapi_openapi::schema::RustApiSchema;
    use rustapi_rs::prelude::*;

    #[derive(Debug, Clone, Serialize, Schema)]
    struct IntegApiResponse {
        success: bool,
        data: Option<String>,
        count: i32,
    }

    #[test]
    fn test_schema_generation() {
        let name = <IntegApiResponse as RustApiSchema>::component_name().unwrap();

        assert_eq!(
            name, "IntegApiResponse",
            "Schema name should match struct name"
        );
    }

    #[test]
    fn test_auto_collects_schemas() {
        let app = RustApi::auto();
        let spec = app.openapi_spec();

        // Should have schemas section
        let components = spec.components.as_ref().expect("Components should exist");
        assert!(
            !components.schemas.is_empty(),
            "OpenAPI spec should have schemas"
        );
    }
}

// ============================================================================
// Extractor Tests
// ============================================================================

mod extractor_tests {
    #[test]
    fn test_path_parsing() {
        // Simulate path parameter parsing
        let path = "/users/123/posts/456";
        let segments: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();

        assert_eq!(segments.len(), 4);
        assert_eq!(segments[1].parse::<i64>().unwrap(), 123);
        assert_eq!(segments[3].parse::<i64>().unwrap(), 456);
    }

    #[test]
    fn test_path_parsing_uuid() {
        let uuid = "550e8400-e29b-41d4-a716-446655440000";
        assert_eq!(uuid.len(), 36);
        assert_eq!(uuid.chars().filter(|c| *c == '-').count(), 4);
    }
}

// ============================================================================
// Compression Tests (basic - does not require feature flag)
// ============================================================================

mod compression_tests {
    #[test]
    fn test_accept_encoding_parsing() {
        let accept_encoding = "gzip, deflate, br";
        let encodings: Vec<&str> = accept_encoding.split(',').map(|s| s.trim()).collect();

        assert!(encodings.contains(&"gzip"));
        assert!(encodings.contains(&"deflate"));
        assert!(encodings.contains(&"br"));
    }

    #[test]
    fn test_content_type_check() {
        let compressible = [
            "text/html",
            "application/json",
            "text/css",
            "text/javascript",
        ];
        let not_compressible = ["image/png", "video/mp4", "application/zip"];

        for ct in &compressible {
            assert!(
                ct.starts_with("text/") || ct.contains("json") || ct.contains("xml"),
                "{} should be compressible",
                ct
            );
        }

        for ct in &not_compressible {
            assert!(
                !ct.starts_with("text/") && !ct.contains("json"),
                "{} should not be compressible",
                ct
            );
        }
    }
}

// ============================================================================
// Rate Limiting Tests (basic concepts)
// ============================================================================

mod rate_limit_tests {
    use std::collections::HashMap;
    use std::sync::{Arc, Mutex};

    struct SimpleRateLimiter {
        counts: Arc<Mutex<HashMap<String, usize>>>,
        limit: usize,
    }

    impl SimpleRateLimiter {
        fn new(limit: usize) -> Self {
            Self {
                counts: Arc::new(Mutex::new(HashMap::new())),
                limit,
            }
        }

        fn check(&self, key: &str) -> bool {
            let mut counts = self.counts.lock().unwrap();
            let count = counts.entry(key.to_string()).or_insert(0);
            if *count < self.limit {
                *count += 1;
                true
            } else {
                false
            }
        }
    }

    #[test]
    fn test_rate_limiter_allows_within_limit() {
        let limiter = SimpleRateLimiter::new(5);
        let ip = "192.168.1.1";

        for i in 0..5 {
            assert!(limiter.check(ip), "Request {} should be allowed", i + 1);
        }
    }

    #[test]
    fn test_rate_limiter_blocks_over_limit() {
        let limiter = SimpleRateLimiter::new(3);
        let ip = "192.168.1.2";

        // Use up the limit
        for _ in 0..3 {
            limiter.check(ip);
        }

        // Next request should be blocked
        assert!(!limiter.check(ip), "Request over limit should be blocked");
    }

    #[test]
    fn test_rate_limiter_multiple_ips() {
        let limiter = SimpleRateLimiter::new(2);

        let ip1 = "192.168.1.1";
        let ip2 = "192.168.1.2";

        // Each IP should have independent limit
        assert!(limiter.check(ip1));
        assert!(limiter.check(ip1));
        assert!(!limiter.check(ip1)); // Over limit

        assert!(limiter.check(ip2)); // Different IP, should work
        assert!(limiter.check(ip2));
        assert!(!limiter.check(ip2)); // Now over limit
    }
}