garrison 0.9.0

Garrison - 面向 Rust 生态的一站式身份认证鉴权框架
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
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
// Copyright (c) 2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

//! CORS 跨域资源共享中间件(axum):OPTIONS 预检一律短路 204,仅 Origin 匹配时注入 CORS 头;
//! 实际请求仅 Origin 匹配时注入响应头(必带 `Vary: Origin` 供缓存区分),配置见 [`CorsConfig`](crate::web::cors::CorsConfig)。

use crate::error::{GarrisonError, GarrisonResult};
use axum::extract::State;
use axum::http::{HeaderName, HeaderValue};
use serde::{Deserialize, Serialize};

/// CORS 中间件配置:默认收紧(`allowed_origins` 为空时不放行任何跨域,`allow_credentials` 默认 false)。
///
/// # 配置示例
///
/// ```toml
/// [cors_config]
/// allowed_origins = ["https://example.com"]
/// allowed_methods = ["GET", "POST"]
/// allow_credentials = true
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct CorsConfig {
    /// 允许的跨域来源列表(精确匹配,`"*"` 表示通配允许所有来源)。
    pub allowed_origins: Vec<String>,
    /// 允许的 HTTP 方法列表。
    pub allowed_methods: Vec<String>,
    /// 允许的请求 header 列表。
    pub allowed_headers: Vec<String>,
    /// 允许浏览器读取的响应 header 列表(空时不注入 Expose-Headers)。
    pub exposed_headers: Vec<String>,
    /// 是否允许携带凭证(Cookie / Authorization)。
    pub allow_credentials: bool,
    /// 预检结果缓存秒数。
    pub max_age_secs: u64,
}

impl Default for CorsConfig {
    fn default() -> Self {
        Self {
            allowed_origins: Vec::new(),
            allowed_methods: vec![
                "GET".to_string(),
                "POST".to_string(),
                "PUT".to_string(),
                "DELETE".to_string(),
                "HEAD".to_string(),
                "OPTIONS".to_string(),
            ],
            allowed_headers: vec!["Authorization".to_string(), "Content-Type".to_string()],
            exposed_headers: Vec::new(),
            allow_credentials: false,
            max_age_secs: 86400,
        }
    }
}

impl CorsConfig {
    /// 校验配置:credentials 与通配符 origin 不可同时使用(CORS 规范),冲突返回 `GarrisonError::Config`。
    pub fn validate(&self) -> GarrisonResult<()> {
        if self.allow_credentials && self.allowed_origins.iter().any(|o| o == "*") {
            return Err(GarrisonError::Config(
                "cors-credentials-wildcard::".to_string(),
            ));
        }
        Ok(())
    }
}

/// origin 是否命中允许列表:空列表不匹配,含 `"*"` 通配,否则大小写敏感精确匹配。
pub fn origin_matches(origin: &str, allowed: &[String]) -> bool {
    if allowed.is_empty() {
        return false;
    }
    if allowed.iter().any(|o| o == "*") {
        return true;
    }
    allowed.iter().any(|o| o == origin)
}

/// 计算 `Access-Control-Allow-Origin` 值:配置含 `"*"` 时返回 `"*"`,否则回显 origin。
fn allow_origin_value<'a>(origin: &'a str, allowed: &'a [String]) -> &'a str {
    if allowed.iter().any(|o| o == "*") {
        "*"
    } else {
        origin
    }
}

/// 将字符串列表拼接为逗号分隔的单个 HeaderValue。
fn join_headers(items: &[String]) -> HeaderValue {
    // SAFETY: CORS header 值均为 ASCII token,HeaderName/HeaderValue::from_str 不会失败。
    let joined = items.join(", ");
    HeaderValue::from_str(&joined).unwrap_or(HeaderValue::from_static(""))
}

/// CORS 中间件:OPTIONS 一律短路 204——Origin 匹配时注入 Allow-Origin/Methods/Headers/Max-Age
/// (`allow_credentials` 时加 Allow-Credentials),否则 204 无 CORS 头;实际请求仅 Origin 匹配时注入
/// Allow-Origin/Expose-Headers/Credentials 并继续,否则透传。
///
/// # 使用
///
/// ```ignore
/// use garrison::web::cors::{garrison_cors_middleware, CorsConfig};
/// use std::sync::Arc;
/// use axum::Router;
///
/// let config = CorsConfig {
/// allowed_origins: vec!["https://example.com".to_string()],
/// ..Default::default()
/// };
/// let app = Router::new()
/// .route("/api", axum::routing::get(|| async { "ok" }))
/// .layer(axum::middleware::from_fn_with_state(
/// Arc::new(config),
/// garrison_cors_middleware,
/// ));
/// ```
pub async fn garrison_cors_middleware(
    State(config): State<std::sync::Arc<CorsConfig>>,
    req: axum::extract::Request,
    next: axum::middleware::Next,
) -> axum::response::Response {
    use axum::http::StatusCode;
    use axum::response::IntoResponse;

    // OPTIONS 预检请求:无论 Origin 是否匹配均短路返回 204
    if req.method() == axum::http::Method::OPTIONS {
        let origin = req
            .headers()
            .get(axum::http::header::ORIGIN)
            .and_then(|v| v.to_str().ok())
            .unwrap_or("");
        // 仅 Origin 非空且匹配时注入 CORS 头;无 Origin / 空 / 不匹配 → 204 无 CORS 头
        if !origin.is_empty() && origin_matches(origin, &config.allowed_origins) {
            let allow_origin = allow_origin_value(origin, &config.allowed_origins);
            let mut headers = axum::http::HeaderMap::new();
            // Vary: Origin —— 响应随 Origin 变化,缓存层必须按 Origin 区分(RFC 6454 §7.6)
            headers.insert(
                HeaderName::from_static("vary"),
                HeaderValue::from_static("Origin"),
            );
            headers.insert(
                axum::http::header::ACCESS_CONTROL_ALLOW_ORIGIN,
                HeaderValue::from_str(allow_origin).unwrap_or(HeaderValue::from_static("*")),
            );
            headers.insert(
                HeaderName::from_static("access-control-allow-methods"),
                join_headers(&config.allowed_methods),
            );
            headers.insert(
                HeaderName::from_static("access-control-allow-headers"),
                join_headers(&config.allowed_headers),
            );
            if config.allow_credentials {
                headers.insert(
                    HeaderName::from_static("access-control-allow-credentials"),
                    HeaderValue::from_static("true"),
                );
            }
            headers.insert(
                HeaderName::from_static("access-control-max-age"),
                HeaderValue::from_str(&config.max_age_secs.to_string())
                    .unwrap_or(HeaderValue::from_static("86400")),
            );
            return (StatusCode::NO_CONTENT, headers).into_response();
        }
        return StatusCode::NO_CONTENT.into_response();
    }

    let origin = match req.headers().get(axum::http::header::ORIGIN) {
        Some(v) => v.to_str().unwrap_or("").to_string(),
        None => return next.run(req).await,
    };

    if origin.is_empty() {
        return next.run(req).await;
    }

    if !origin_matches(&origin, &config.allowed_origins) {
        return next.run(req).await;
    }

    let allow_origin = allow_origin_value(&origin, &config.allowed_origins);

    let mut resp = next.run(req).await;
    let headers = resp.headers_mut();
    // Vary: Origin —— 响应随 Origin 变化,缓存必须按 Origin 区分,否则 A 源缓存可能误发给 B 源(RFC 6454 §7.6)
    headers.insert(
        HeaderName::from_static("vary"),
        HeaderValue::from_static("Origin"),
    );
    headers.insert(
        axum::http::header::ACCESS_CONTROL_ALLOW_ORIGIN,
        HeaderValue::from_str(allow_origin).unwrap_or(HeaderValue::from_static("*")),
    );
    if !config.exposed_headers.is_empty() {
        headers.insert(
            HeaderName::from_static("access-control-expose-headers"),
            join_headers(&config.exposed_headers),
        );
    }
    if config.allow_credentials {
        headers.insert(
            HeaderName::from_static("access-control-allow-credentials"),
            HeaderValue::from_static("true"),
        );
    }
    resp
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::body::Body;
    use axum::http::{Request, StatusCode};
    use axum::routing::get;
    use axum::Router;
    use std::sync::Arc;
    use tower::ServiceExt;

    // 辅助函数

    fn make_app(config: CorsConfig) -> Router {
        Router::new()
            .route("/api/test", get(|| async { "ok" }))
            .layer(axum::middleware::from_fn_with_state(
                Arc::new(config),
                garrison_cors_middleware,
            ))
    }

    fn make_request(method: &str, path: &str) -> Request<Body> {
        Request::builder()
            .method(method)
            .uri(path)
            .body(Body::empty())
            .unwrap()
    }

    fn make_request_with_origin(method: &str, path: &str, origin: &str) -> Request<Body> {
        Request::builder()
            .method(method)
            .uri(path)
            .header("origin", origin)
            .body(Body::empty())
            .unwrap()
    }

    // origin_matches 单元测试

    #[test]
    fn origin_matches_exact_match() {
        let allowed = vec!["https://example.com".to_string()];
        assert!(origin_matches("https://example.com", &allowed));
    }

    #[test]
    fn origin_matches_wildcard() {
        let allowed = vec!["*".to_string()];
        assert!(origin_matches("https://anything.com", &allowed));
        assert!(origin_matches("http://localhost:3000", &allowed));
    }

    #[test]
    fn origin_matches_multiple_origins() {
        let allowed = vec![
            "https://a.com".to_string(),
            "https://b.com".to_string(),
            "https://c.com".to_string(),
        ];
        assert!(origin_matches("https://a.com", &allowed));
        assert!(origin_matches("https://b.com", &allowed));
        assert!(origin_matches("https://c.com", &allowed));
    }

    #[test]
    fn origin_matches_no_match() {
        let allowed = vec!["https://example.com".to_string()];
        assert!(!origin_matches("https://evil.com", &allowed));
    }

    #[test]
    fn origin_matches_empty_list() {
        let allowed: Vec<String> = vec![];
        assert!(!origin_matches("https://example.com", &allowed));
    }

    #[test]
    fn origin_matches_with_port() {
        let allowed = vec!["http://localhost:3000".to_string()];
        assert!(origin_matches("http://localhost:3000", &allowed));
        assert!(!origin_matches("http://localhost:8080", &allowed));
    }

    #[test]
    fn origin_matches_with_protocol() {
        let allowed = vec!["https://example.com".to_string()];
        assert!(origin_matches("https://example.com", &allowed));
        assert!(!origin_matches("http://example.com", &allowed));
    }

    #[test]
    fn origin_matches_case_sensitive() {
        let allowed = vec!["https://Example.COM".to_string()];
        assert!(origin_matches("https://Example.COM", &allowed));
        assert!(!origin_matches("https://example.com", &allowed));
    }

    #[test]
    fn origin_matches_wildcard_with_credentials_scenario() {
        // 通配符下 origin_matches 仍返回 true;credentials 冲突由 validate() 单独处理
        let allowed = vec!["*".to_string()];
        assert!(origin_matches("https://example.com", &allowed));
    }

    #[test]
    fn origin_matches_mixed_exact_and_wildcard() {
        let allowed = vec!["https://example.com".to_string(), "*".to_string()];
        assert!(origin_matches("https://anything.com", &allowed));
        assert!(origin_matches("https://example.com", &allowed));
    }

    // OPTIONS 预检集成测试

    #[tokio::test]
    async fn preflight_matching_origin_returns_204() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            ..Default::default()
        };
        let app = make_app(config);
        let resp = app
            .oneshot(make_request_with_origin(
                "OPTIONS",
                "/api/test",
                "https://example.com",
            ))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::NO_CONTENT);
        assert_eq!(
            resp.headers().get("access-control-allow-origin").unwrap(),
            "https://example.com"
        );
    }

    #[tokio::test]
    async fn preflight_non_matching_origin_returns_204_no_cors_headers() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            ..Default::default()
        };
        let app = make_app(config);
        // 非匹配 origin → 204 无 CORS 头(短路,不透传到 handler)
        let resp = app
            .oneshot(make_request_with_origin(
                "OPTIONS",
                "/api/test",
                "https://evil.com",
            ))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::NO_CONTENT);
        assert!(resp.headers().get("access-control-allow-origin").is_none());
    }

    #[tokio::test]
    async fn preflight_with_credentials_injects_header() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            allow_credentials: true,
            ..Default::default()
        };
        let app = make_app(config);
        let resp = app
            .oneshot(make_request_with_origin(
                "OPTIONS",
                "/api/test",
                "https://example.com",
            ))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::NO_CONTENT);
        assert_eq!(
            resp.headers()
                .get("access-control-allow-credentials")
                .unwrap(),
            "true"
        );
    }

    #[tokio::test]
    async fn preflight_no_origin_header_returns_204() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            ..Default::default()
        };
        let app = make_app(config);
        // 无 Origin header → 204 无 CORS 头(短路,不透传到 handler)
        let resp = app
            .oneshot(make_request("OPTIONS", "/api/test"))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::NO_CONTENT);
        assert!(resp.headers().get("access-control-allow-origin").is_none());
    }

    #[tokio::test]
    async fn preflight_wildcard_origin_returns_star() {
        let config = CorsConfig {
            allowed_origins: vec!["*".to_string()],
            ..Default::default()
        };
        let app = make_app(config);
        let resp = app
            .oneshot(make_request_with_origin(
                "OPTIONS",
                "/api/test",
                "https://anything.com",
            ))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::NO_CONTENT);
        assert_eq!(
            resp.headers().get("access-control-allow-origin").unwrap(),
            "*"
        );
    }

    #[tokio::test]
    async fn preflight_verifies_allow_methods() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            allowed_methods: vec!["GET".to_string(), "POST".to_string()],
            ..Default::default()
        };
        let app = make_app(config);
        let resp = app
            .oneshot(make_request_with_origin(
                "OPTIONS",
                "/api/test",
                "https://example.com",
            ))
            .await
            .unwrap();
        let methods = resp
            .headers()
            .get("access-control-allow-methods")
            .unwrap()
            .to_str()
            .unwrap();
        assert!(methods.contains("GET"));
        assert!(methods.contains("POST"));
    }

    #[tokio::test]
    async fn preflight_verifies_allow_headers() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            allowed_headers: vec!["Authorization".to_string(), "X-Custom".to_string()],
            ..Default::default()
        };
        let app = make_app(config);
        let resp = app
            .oneshot(make_request_with_origin(
                "OPTIONS",
                "/api/test",
                "https://example.com",
            ))
            .await
            .unwrap();
        let headers = resp
            .headers()
            .get("access-control-allow-headers")
            .unwrap()
            .to_str()
            .unwrap();
        assert!(headers.contains("Authorization"));
        assert!(headers.contains("X-Custom"));
    }

    #[tokio::test]
    async fn preflight_verifies_max_age() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            max_age_secs: 3600,
            ..Default::default()
        };
        let app = make_app(config);
        let resp = app
            .oneshot(make_request_with_origin(
                "OPTIONS",
                "/api/test",
                "https://example.com",
            ))
            .await
            .unwrap();
        let max_age = resp
            .headers()
            .get("access-control-max-age")
            .unwrap()
            .to_str()
            .unwrap();
        assert_eq!(max_age, "3600");
    }

    // 实际请求(非 OPTIONS)集成测试

    #[tokio::test]
    async fn actual_request_matching_origin_injects_headers() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            ..Default::default()
        };
        let app = make_app(config);
        let resp = app
            .oneshot(make_request_with_origin(
                "GET",
                "/api/test",
                "https://example.com",
            ))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(
            resp.headers().get("access-control-allow-origin").unwrap(),
            "https://example.com"
        );
    }

    #[tokio::test]
    async fn actual_request_non_matching_origin_no_headers() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            ..Default::default()
        };
        let app = make_app(config);
        let resp = app
            .oneshot(make_request_with_origin(
                "GET",
                "/api/test",
                "https://evil.com",
            ))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        assert!(resp.headers().get("access-control-allow-origin").is_none());
    }

    #[tokio::test]
    async fn actual_request_exposed_headers_injection() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            exposed_headers: vec!["X-Total-Count".to_string(), "X-Page".to_string()],
            ..Default::default()
        };
        let app = make_app(config);
        let resp = app
            .oneshot(make_request_with_origin(
                "GET",
                "/api/test",
                "https://example.com",
            ))
            .await
            .unwrap();
        let exposed = resp
            .headers()
            .get("access-control-expose-headers")
            .unwrap()
            .to_str()
            .unwrap();
        assert!(exposed.contains("X-Total-Count"));
        assert!(exposed.contains("X-Page"));
    }

    #[tokio::test]
    async fn actual_request_credentials_true() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            allow_credentials: true,
            ..Default::default()
        };
        let app = make_app(config);
        let resp = app
            .oneshot(make_request_with_origin(
                "GET",
                "/api/test",
                "https://example.com",
            ))
            .await
            .unwrap();
        assert_eq!(
            resp.headers()
                .get("access-control-allow-credentials")
                .unwrap(),
            "true"
        );
    }

    #[tokio::test]
    async fn actual_request_no_origin_passes_through() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            ..Default::default()
        };
        let app = make_app(config);
        let resp = app.oneshot(make_request("GET", "/api/test")).await.unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        assert!(resp.headers().get("access-control-allow-origin").is_none());
    }

    #[tokio::test]
    async fn actual_request_wildcard_origin() {
        let config = CorsConfig {
            allowed_origins: vec!["*".to_string()],
            ..Default::default()
        };
        let app = make_app(config);
        let resp = app
            .oneshot(make_request_with_origin(
                "GET",
                "/api/test",
                "https://anything.com",
            ))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(
            resp.headers().get("access-control-allow-origin").unwrap(),
            "*"
        );
    }

    // Vary: Origin

    /// 实际请求(注入 ACAO)必须带 Vary: Origin,防止缓存跨 Origin 串用。
    #[tokio::test]
    async fn actual_request_includes_vary_origin() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            ..Default::default()
        };
        let app = make_app(config);
        let resp = app
            .oneshot(make_request_with_origin(
                "GET",
                "/api/test",
                "https://example.com",
            ))
            .await
            .unwrap();
        assert_eq!(resp.headers().get("vary").unwrap(), "Origin");
    }

    /// 预检请求(注入 CORS 头)必须带 Vary: Origin。
    #[tokio::test]
    async fn preflight_includes_vary_origin() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            ..Default::default()
        };
        let app = make_app(config);
        let resp = app
            .oneshot(make_request_with_origin(
                "OPTIONS",
                "/api/test",
                "https://example.com",
            ))
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::NO_CONTENT);
        assert_eq!(resp.headers().get("vary").unwrap(), "Origin");
    }

    // CorsConfig::validate() 单元测试

    #[test]
    fn validate_valid_config_passes() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            ..Default::default()
        };
        assert!(config.validate().is_ok());
    }

    #[test]
    fn validate_credentials_with_wildcard_fails() {
        let config = CorsConfig {
            allowed_origins: vec!["*".to_string()],
            allow_credentials: true,
            ..Default::default()
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn validate_credentials_with_exact_origin_passes() {
        let config = CorsConfig {
            allowed_origins: vec!["https://example.com".to_string()],
            allow_credentials: true,
            ..Default::default()
        };
        assert!(config.validate().is_ok());
    }

    #[test]
    fn validate_empty_origins_passes() {
        let config = CorsConfig::default();
        assert!(config.validate().is_ok());
    }

    #[test]
    fn validate_after_setting_fields() {
        let mut config = CorsConfig::default();
        assert!(config.validate().is_ok());
        // 设置通配符 + credentials 后应失败
        config.allowed_origins = vec!["*".to_string()];
        config.allow_credentials = true;
        assert!(config.validate().is_err());
        // 改回精确 origin 后应通过
        config.allowed_origins = vec!["https://safe.com".to_string()];
        assert!(config.validate().is_ok());
    }
}