Removed pretty-formatting of CORS exposed headers & changed test to work properly

This commit is contained in:
Mathieu Amiot 2018-08-02 12:35:18 +02:00 committed by Mathieu Amiot
parent c71ff65443
commit 68a3654d81
1 changed files with 19 additions and 24 deletions

View File

@ -839,21 +839,7 @@ 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.iter() self.expose_hdrs.iter()
.fold(String::new(), |s, v| { .fold(String::new(), |s, v| format!("{}, {}", s, v.as_str()))[2..]
let header = v.as_str().split('-')
.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() .to_owned()
); );
} }
@ -1086,13 +1072,14 @@ mod tests {
#[test] #[test]
fn test_response() { fn test_response() {
let exposed_headers = vec![header::AUTHORIZATION, header::ACCEPT];
let cors = Cors::build() let cors = Cors::build()
.send_wildcard() .send_wildcard()
.disable_preflight() .disable_preflight()
.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(exposed_headers.clone())
.expose_headers(vec![header::AUTHORIZATION, header::ACCEPT]) .expose_headers(exposed_headers.clone())
.allowed_header(header::CONTENT_TYPE) .allowed_header(header::CONTENT_TYPE)
.finish(); .finish();
@ -1109,18 +1096,26 @@ 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()
); );
{
let headers = resp.headers()
.get(header::ACCESS_CONTROL_EXPOSE_HEADERS)
.unwrap()
.to_str()
.unwrap()
.split(',')
.map(|s| s.trim())
.collect::<Vec<&str>>();
for h in exposed_headers {
assert!(headers.contains(&h.as_str()));
}
}
let resp: HttpResponse = let resp: HttpResponse =
HttpResponse::Ok().header(header::VARY, "Accept").finish(); HttpResponse::Ok().header(header::VARY, "Accept").finish();
let resp = cors.response(&req, resp).unwrap().response(); let resp = cors.response(&req, resp).unwrap().response();