Skip to content

Commit

Permalink
fix cors test and vary logic
Browse files Browse the repository at this point in the history
based on feedback given by jplatte in
tower-rs/tower-http#473
  • Loading branch information
glendc committed Feb 22, 2024
1 parent 92ad17f commit 690f2f5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/http/layer/cors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,14 +634,15 @@ where
let req = Request::from_parts(parts, body);

let mut response: Response<ResBody> = self.inner.serve(ctx, req).await?;
let response_headers = response.headers_mut();

// vary header can have multiple values, don't overwrite
// previously-set value(s).
if let Some(vary) = response.headers_mut().remove(header::VARY) {
headers.append(header::VARY, vary);
if let Some(vary) = headers.remove(header::VARY) {
response_headers.append(header::VARY, vary);
}
// extend will overwrite previous headers of remaining names
response.headers_mut().extend(headers.drain());
response_headers.extend(headers.drain());

Ok(response)
}
Expand Down
2 changes: 1 addition & 1 deletion src/http/layer/cors/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ async fn vary_set_by_inner_service() {
.await
.unwrap();
let mut vary_headers = res.headers().get_all(header::VARY).into_iter();
assert_eq!(vary_headers.next(), Some(&PERMISSIVE_CORS_VARY_HEADERS));
assert_eq!(vary_headers.next(), Some(&CUSTOM_VARY_HEADERS));
assert_eq!(vary_headers.next(), Some(&PERMISSIVE_CORS_VARY_HEADERS));
assert_eq!(vary_headers.next(), None);
}

0 comments on commit 690f2f5

Please sign in to comment.