Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable cors/tests.rs and make it pass #473

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions tower-http/src/cors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ mod expose_headers;
mod max_age;
mod vary;

#[cfg(test)]
mod tests;

pub use self::{
allow_credentials::AllowCredentials, allow_headers::AllowHeaders, allow_methods::AllowMethods,
allow_origin::AllowOrigin, allow_private_network::AllowPrivateNetwork,
Expand Down Expand Up @@ -682,13 +685,15 @@ where
KindProj::CorsCall { future, headers } => {
let mut response: Response<B> = ready!(future.poll(cx))?;

let response_headers = response.headers_mut();

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

Poll::Ready(Ok(response))
}
Expand Down
4 changes: 2 additions & 2 deletions tower-http/src/cors/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::convert::Infallible;

use crate::test_helpers::Body;
use http::{header, HeaderValue, Request, Response};
use hyper::Body;
use tower::{service_fn, util::ServiceExt, Layer};

use crate::cors::CorsLayer;
Expand All @@ -27,7 +27,7 @@ async fn vary_set_by_inner_service() {
let svc = CorsLayer::permissive().layer(service_fn(inner_svc));
let res = svc.oneshot(Request::new(Body::empty())).await.unwrap();
let mut vary_headers = res.headers().get_all(header::VARY).into_iter();
assert_eq!(vary_headers.next(), Some(&CUSTOM_VARY_HEADERS));
assert_eq!(vary_headers.next(), Some(&PERMISSIVE_CORS_VARY_HEADERS));
assert_eq!(vary_headers.next(), Some(&CUSTOM_VARY_HEADERS));
assert_eq!(vary_headers.next(), None);
}
Loading