Fixed wrong formatting of CORS's Access-Control-Expose-Headers

And added a test for it.
This commit is contained in:
Mathieu Amiot 2018-08-01 23:23:01 +02:00 committed by Mathieu Amiot
parent 8c89c90c50
commit c71ff65443
1 changed files with 25 additions and 4 deletions

View File

@ -838,10 +838,23 @@ impl<S: 'static> CorsBuilder<S> {
if !self.expose_hdrs.is_empty() { if !self.expose_hdrs.is_empty() {
cors.expose_hdrs = Some( cors.expose_hdrs = Some(
self.expose_hdrs self.expose_hdrs.iter()
.iter() .fold(String::new(), |s, v| {
.fold(String::new(), |s, v| s + v.as_str())[1..] let header = v.as_str().split('-')
.to_owned(), .fold(String::new(), |h, w| {
let cap = w.chars()
.next()
.unwrap()
.to_uppercase()
.collect::<String>();
format!("{}-{}{}", &h, &cap, &w[1..])
})[1..]
.to_owned();
format!("{}, {}", s, header)
})[2..]
.to_owned()
); );
} }
Cors { Cors {
@ -1079,6 +1092,7 @@ mod tests {
.max_age(3600) .max_age(3600)
.allowed_methods(vec![Method::GET, Method::OPTIONS, Method::POST]) .allowed_methods(vec![Method::GET, Method::OPTIONS, Method::POST])
.allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT]) .allowed_headers(vec![header::AUTHORIZATION, header::ACCEPT])
.expose_headers(vec![header::AUTHORIZATION, header::ACCEPT])
.allowed_header(header::CONTENT_TYPE) .allowed_header(header::CONTENT_TYPE)
.finish(); .finish();
@ -1095,6 +1109,13 @@ mod tests {
.unwrap() .unwrap()
.as_bytes() .as_bytes()
); );
assert_eq!(
&b"Authorization, Accept"[..],
resp.headers()
.get(header::ACCESS_CONTROL_EXPOSE_HEADERS)
.unwrap()
.as_bytes()
);
assert_eq!( assert_eq!(
&b"Origin"[..], &b"Origin"[..],
resp.headers().get(header::VARY).unwrap().as_bytes() resp.headers().get(header::VARY).unwrap().as_bytes()