Skip to content

Commit b63d246

Browse files
rampage644claude
authored andcommitted
Add missing #[test] attribute to test_make_cors_middleware (#1783)
The test function was not being recognized by the test runner because it was missing the #[test] attribute. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude <noreply@anthropic.com>
1 parent eb86f5b commit b63d246

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

crates/api-ui/src/layers.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use axum::{middleware::Next, response::Response};
44
use http::header::{AUTHORIZATION, CONTENT_TYPE};
55
use http::{HeaderValue, Method};
66
use std::str::FromStr;
7-
use tower_http::cors::CorsLayer;
7+
use tower_http::cors::{AllowOrigin, CorsLayer};
88
use uuid::Uuid;
99

1010
#[derive(Clone)]
@@ -56,12 +56,15 @@ pub async fn add_request_metadata(
5656

5757
#[allow(clippy::needless_pass_by_value, clippy::expect_used)]
5858
pub fn make_cors_middleware(origin: &str) -> CorsLayer {
59-
#[allow(clippy::expect_fun_call)]
60-
let origin_value = origin
61-
.parse::<HeaderValue>()
62-
.expect(&format!("Failed to parse origin value: {origin}"));
59+
let origins: Vec<HeaderValue> = origin
60+
.split(|c: char| c == ',' || c.is_ascii_whitespace())
61+
.filter(|part| !part.is_empty())
62+
.map(|part| HeaderValue::from_str(part).expect("Failed to parse origin value"))
63+
.collect();
64+
65+
let allow_origin = AllowOrigin::list(origins);
6366
CorsLayer::new()
64-
.allow_origin(origin_value)
67+
.allow_origin(allow_origin)
6568
.allow_methods(vec![
6669
Method::GET,
6770
Method::POST,
@@ -73,3 +76,19 @@ pub fn make_cors_middleware(origin: &str) -> CorsLayer {
7376
.allow_headers(vec![AUTHORIZATION, CONTENT_TYPE])
7477
.allow_credentials(true)
7578
}
79+
80+
#[cfg(test)]
81+
mod tests {
82+
use super::*;
83+
84+
#[test]
85+
fn test_make_cors_middleware() {
86+
for origin in [
87+
"https://a.example",
88+
"https://b.example",
89+
"https://c.example http://d.example, http://e.example",
90+
] {
91+
let _layer = make_cors_middleware(origin);
92+
}
93+
}
94+
}

0 commit comments

Comments
 (0)